X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/2ecc77b9a1880b6dd341c445aa6509b6acd6d633..74d6135e4db4cf9a70994d5bc2062be30311d0f3:/iTunesRemote.m diff --git a/iTunesRemote.m b/iTunesRemote.m index 23e6094..e479c63 100755 --- a/iTunesRemote.m +++ b/iTunesRemote.m @@ -9,7 +9,7 @@ - (NSString *)title { - return @"iTunes Plug-in"; + return @"iTunes"; } - (NSString *)information; @@ -25,35 +25,30 @@ - (BOOL)begin { iTunesPSN = [self iTunesPSN]; - - //We won't need this once we're pure AEs - asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype); - + //Register for application termination in NSWorkspace [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil]; [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil]; - + return YES; } - (BOOL)halt { iTunesPSN.highLongOfPSN = kNoProcess; - - //We won't need this once we're pure AEs - CloseComponent(asComponent); - + //Unregister for application termination in NSWorkspace [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; - + return YES; } - (BOOL)isAppRunning { NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications]; - int i,count = [apps count]; - + int i; + int count = [apps count]; + for (i = 0; i < count; i++) { if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) { @@ -63,20 +58,23 @@ return NO; } -- (PlayerState)playerState +- (ITMTRemotePlayerState)playerState { - NSString *result = [self runScriptAndReturnResult:@"get player state"]; + long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; - if ([result isEqualToString:@"playing"]) { - return playing; - } else if ([result isEqualToString:@"paused"]) { - return paused; - } else if ([result isEqualToString:@"stopped"]) { - return stopped; - } else if ([result isEqualToString:@"rewinding"]) { - return rewinding; - } else if ([result isEqualToString:@"fast forwarding"]) { - return forwarding; + switch (result) + { + default: + case 'kPSS': + return stopped; + case 'kPSP': + return playing; + case 'kPSp': + return paused; + case 'kPSR': + return rewinding; + case 'kPSF': + return forwarding; } return stopped; @@ -84,195 +82,185 @@ - (NSArray *)playlists { - int i; - int numPresets = [[self runScriptAndReturnResult:@"get number of playlists"] intValue]; - NSMutableArray *presets = [[NSMutableArray alloc] init]; - for (i = 1; i <= numPresets; i++) { - [presets addObject:[self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of playlist %i", i]]]; - } - - return [presets autorelease]; + long i = 0; + const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN]; + NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists]; + + + for (i = 1; i <= numPlaylists; i++) { + const long j = i; + NSString *sendStr = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() } }",(unsigned long)j]; + NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + NSLog(@"sent event cur %d max %d",i,numPlaylists); + [playlists addObject:theObj]; + } + return [playlists autorelease]; } - (int)numberOfSongsInPlaylistAtIndex:(int)index { - NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get number of tracks in playlist %i", index]]; - return [result intValue]; + return [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:[NSString stringWithFormat:@"kocl:type('cTrk'), '----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() }",index] eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN]; } - (NSString *)classOfPlaylistAtIndex:(int)index { - //Not working yet. It returns the 4 character code instead of a name. - /*NSString *result; - result = [[ITAppleEventCenter sharedCenter] - sendTwoTierAEWithRequestedKey:@"pcls" - fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]];*/ - NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get class of playlist %i", index]]; - return result; + int realResult = [[ITAppleEventCenter sharedCenter] + sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + + if (realResult == 'cRTP') return @"radio tuner playlist"; + else return @"playlist"; } - (int)currentPlaylistIndex { int result; result = [[ITAppleEventCenter sharedCenter] - sendTwoTierAEWithRequestedKeyForNumber:@"pidx" - fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; return result; } - (NSString *)songTitleAtIndex:(int)index { - NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of track %i of current playlist", index]]; - return result; + return [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",index] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (int)currentSongIndex { int result; - result = [[ITAppleEventCenter sharedCenter] - sendTwoTierAEWithRequestedKeyForNumber:@"pidx" - fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; return result; } - (NSString *)currentSongTitle { - return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" - fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongArtist { - return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" - fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongAlbum { - return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" - fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongGenre { - return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" - fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongLength { - return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" - fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongRemaining { long duration = [[ITAppleEventCenter sharedCenter] - sendTwoTierAEWithRequestedKeyForNumber:@"pDur" - fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; long current = [[ITAppleEventCenter sharedCenter] - sendAEWithRequestedKeyForNumber:@"pPos" - eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; - + sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + return [[NSNumber numberWithLong:duration - current] stringValue]; } +- (float)currentSongRating +{ + return 0.00; +} + +- (BOOL)setCurrentSongRating:(float)rating +{ + return NO; +} + +- (float)volume +{ + return 1.00; +} + +- (BOOL)setVolume:(float)volume +{ + return NO; +} + - (NSArray *)eqPresets; { int i; - int numPresets = [[self runScriptAndReturnResult:@"get number of EQ presets"] intValue]; - NSMutableArray *presets = [[NSMutableArray alloc] init]; - - for (i = 1; i <= numPresets; i++) { - [presets addObject:[self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of EQ preset %i", i]]]; - } - - return [presets autorelease]; + long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN]; + NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets]; + + for (i = 1; i <= numPresets; i++) { + NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() } }",i] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + if (theObj) [presets addObject:theObj]; + } + return [presets autorelease]; } - (int)currentEQPresetIndex { int result; result = [[ITAppleEventCenter sharedCenter] - sendTwoTierAEWithRequestedKeyForNumber:@"pidx" - fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" - appPSN:[self iTunesPSN]]; + sendTwoTierAEWithRequestedKeyForNumber:@"pidx"fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"appPSN:iTunesPSN]; return result; } - (BOOL)play { - [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" - appPSN:[self iTunesPSN]]; + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN]; return YES; } - (BOOL)pause { - [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" - appPSN:[self iTunesPSN]]; + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN]; return YES; } - (BOOL)goToNextSong { - [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" - appPSN:[self iTunesPSN]]; + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN]; return YES; } - (BOOL)goToPreviousSong { - [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" - appPSN:[self iTunesPSN]]; + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN]; return YES; } - (BOOL)fastForward { - [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" - appPSN:[self iTunesPSN]]; + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN]; return YES; } - (BOOL)rewind { - [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" - appPSN:[self iTunesPSN]]; + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN]; return YES; } - (BOOL)switchToPlaylistAtIndex:(int)index { - [self runScriptAndReturnResult:[NSString stringWithFormat: - @"play playlist %i", index]]; - return NO; + [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN]; + return YES; } - (BOOL)switchToSongAtIndex:(int)index { - [self runScriptAndReturnResult:[NSString stringWithFormat: - @"play track %i of current playlist", index]]; - return NO; + [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:() } }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN]; + return YES; } - (BOOL)switchToEQAtIndex:(int)index { - [self runScriptAndReturnResult:[NSString stringWithFormat: - @"set current EQ preset to EQ preset %i", index]]; - [self runScriptAndReturnResult:@"set EQ enabled to 1"]; - return NO; + [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() }, '----':obj { form:'prop', want:type('prop'), seld:type('pEQP'), from:'null'() }",index] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN]; + [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:1, '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN]; + return YES; } - (ProcessSerialNumber)iTunesPSN @@ -280,17 +268,20 @@ NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications]; ProcessSerialNumber number; int i; + int count = [apps count]; number.highLongOfPSN = kNoProcess; - for (i = 0; i < [apps count]; i++) + for (i = 0; i < count; i++) { NSDictionary *curApp = [apps objectAtIndex:i]; if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) { - number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue]; - number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue]; + number.highLongOfPSN = [[curApp objectForKey: + @"NSApplicationProcessSerialNumberHigh"] intValue]; + number.lowLongOfPSN = [[curApp objectForKey: + @"NSApplicationProcessSerialNumberLow"] intValue]; } } return number; @@ -299,11 +290,11 @@ - (void)applicationLaunched:(NSNotification *)note { NSDictionary *info = [note userInfo]; - + if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) { iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue]; iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue]; - + [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil]; } } @@ -311,43 +302,11 @@ - (void)applicationTerminated:(NSNotification *)note { NSDictionary *info = [note userInfo]; - + if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) { iTunesPSN.highLongOfPSN = kNoProcess; [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil]; } } -//This is just temporary -- (NSString *)runScriptAndReturnResult:(NSString *)script -{ - AEDesc scriptDesc, resultDesc; - Size length; - NSString *result; - Ptr buffer; - - script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script]; - - AECreateDesc(typeChar, [script cString], [script cStringLength], -&scriptDesc); - - OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc); - - length = AEGetDescDataSize(&resultDesc); - buffer = malloc(length); - - AEGetDescData(&resultDesc, buffer, length); - AEDisposeDesc(&scriptDesc); - AEDisposeDesc(&resultDesc); - result = [NSString stringWithCString:buffer length:length]; - if ( (! [result isEqualToString:@""]) && - ([result characterAtIndex:0] == '\"') && - ([result characterAtIndex:[result length] - 1] == '\"') ) { - result = [result substringWithRange:NSMakeRange(1, [result length] - 2)]; - } - free(buffer); - buffer = nil; - return result; -} - @end