X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/31ab86a87f78edfdd32810db1ff708ca8f5eaaca..4b426dd2e4cb2a9efccdba130dfaf86f735f58b5:/iTunesRemote.m?ds=sidebyside diff --git a/iTunesRemote.m b/iTunesRemote.m index 36a38db..a5a1f82 100755 --- a/iTunesRemote.m +++ b/iTunesRemote.m @@ -1,6 +1,5 @@ #import "iTunesRemote.h" - @implementation iTunesRemote + (id)remote @@ -8,144 +7,334 @@ return [[[iTunesRemote alloc] init] autorelease]; } -- (NSString *)title +- (NSString *)remoteTitle { - return nil; + return @"iTunes Remote"; } -- (NSString *)information; +- (NSString *)remoteInformation { - return nil; + return @"Default MenuTunes plugin to control iTunes, by iThink Software."; } -- (NSImage *)icon +- (NSImage *)remoteIcon { return nil; } - (BOOL)begin { - return NO; + iTunesPSN = [self iTunesPSN]; + + [[[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 { - return NO; + iTunesPSN.highLongOfPSN = kNoProcess; + + //Unregister for application termination in NSWorkspace + [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; + + return YES; } -- (NSArray *)sources +- (NSString *)playerFullName { - return nil; + return @"iTunes"; } -- (int)currentSourceIndex +- (NSString *)playerSimpleName { - return nil; + return @"iTunes"; } -- (NSArray *)playlistsForCurrentSource +- (NSDictionary *)capabilities { return nil; } +- (ITMTRemotePlayerRunningState)playerRunningState +{ + NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications]; + int i; + int count = [apps count]; + + for (i = 0; i < count; i++) { + if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) { + return ITMTRemotePlayerRunning; + } + } + return ITMTRemotePlayerNotRunning; +} + +- (ITMTRemotePlayerPlayingState)playerPlayingState +{ + long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + + switch (result) + { + default: + case 'kPSS': + return ITMTRemotePlayerStopped; + case 'kPSP': + return ITMTRemotePlayerPlaying; + case 'kPSp': + return ITMTRemotePlayerPaused; + case 'kPSR': + return ITMTRemotePlayerRewinding; + case 'kPSF': + return ITMTRemotePlayerForwarding; + } + + return ITMTRemotePlayerStopped; +} + +- (NSArray *)playlists +{ + 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]; + [playlists addObject:theObj]; + } + return [playlists autorelease]; +} + +- (int)numberOfSongsInPlaylistAtIndex:(int)index +{ + 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 +{ + 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 { - return nil; + int result; + result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + return result; } -- (NSString *)songTitleAtIndex +- (NSString *)songTitleAtIndex:(int)index { - return nil; + 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 { - return nil; + int result; + result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + return result; } - (NSString *)currentSongTitle { - return nil; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongArtist { - return nil; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongAlbum { - return nil; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongGenre { - return nil; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongLength { - return nil; + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; } - (NSString *)currentSongRemaining { - return nil; + long duration = [[ITAppleEventCenter sharedCenter] + sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + long current = [[ITAppleEventCenter sharedCenter] + sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + + return [[NSNumber numberWithLong:duration - current] stringValue]; } -- (NSArray *)eqPresets; +- (float)currentSongRating { - return nil; + int realResult = [[ITAppleEventCenter sharedCenter] + sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + + return realResult / 100; } -- (BOOL)play +- (BOOL)setCurrentSongRating:(float)rating { - return NO; + [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), ----:obj { form:'prop', want:type('prop'), seld:type('pRte'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } ",(long)rating*100] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN]; + return YES; } -- (BOOL)pause +- (BOOL)equalizerEnabled { return NO; } -- (BOOL)goToNextSong +- (BOOL)setEqualizerEnabled:(BOOL)enabled { return NO; } -- (BOOL)goToPreviousSong +- (NSArray *)eqPresets { - return NO; + int i; + 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]; } -- (BOOL)goToNextPlaylist +- (int)currentEQPresetIndex { - return NO; + int result; + result = [[ITAppleEventCenter sharedCenter] + sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + return result; } -- (BOOL)goToPreviousPlaylist +- (float)volume { - return NO; + long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]; + return vol / 100; } -- (BOOL)switchToSourceAtIndex:(int)index +- (BOOL)setVolume:(float)volume { + [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), ----:obj { form:'prop', want:type('prop'), seld:type('pVol'), from:'null'() }",(long)volume*100] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN]; return NO; } +- (BOOL)play +{ + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN]; + return YES; +} + +- (BOOL)pause +{ + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN]; + return YES; +} + +- (BOOL)goToNextSong +{ + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN]; + return YES; +} + +- (BOOL)goToPreviousSong +{ + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN]; + return YES; +} + +- (BOOL)forward +{ + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN]; + return YES; +} + +- (BOOL)rewind +{ + [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN]; + return YES; +} + - (BOOL)switchToPlaylistAtIndex:(int)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 { - 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 { - return NO; + [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:obj { form:'ID ', 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]; + return YES; +} + +- (ProcessSerialNumber)iTunesPSN +{ + NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications]; + ProcessSerialNumber number; + int i; + int count = [apps count]; + + number.highLongOfPSN = kNoProcess; + + 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]; + } + } + return number; +} + +- (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]; + } +} + +- (void)applicationTerminated:(NSNotification *)note +{ + NSDictionary *info = [note userInfo]; + + if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) { + iTunesPSN.highLongOfPSN = kNoProcess; + [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil]; + } } @end