1 #import "iTunesRemote.h"
3 @implementation iTunesRemote
7 return [[[iTunesRemote alloc] init] autorelease];
15 - (NSString *)information;
17 return @"Default MenuTunes plugin to control iTunes. Written by iThink Software.";
27 iTunesPSN = [self iTunesPSN];
29 //Register for application termination in NSWorkspace
30 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
31 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
38 iTunesPSN.highLongOfPSN = kNoProcess;
40 //Unregister for application termination in NSWorkspace
41 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
46 - (ITMTRemotePlayerRunningStatus)playerRunningStatus
48 NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
50 int count = [apps count];
52 for (i = 0; i < count; i++) {
53 if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
54 isEqualToString:@"iTunes"]) {
55 return ITMTRemotePlayerRunning;
58 return ITMTRemotePlayerNotRunning;
61 - (ITMTRemotePlayerState)playerState
63 long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
69 return ITMTRemotePlayerStopped;
71 return ITMTRemotePlayerPlaying;
73 return ITMTRemotePlayerPaused;
75 return ITMTRemotePlayerRewinding;
77 return ITMTRemotePlayerForwarding;
83 - (NSArray *)playlists
86 const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
87 NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
90 for (i = 1; i <= numPlaylists; i++) {
92 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];
93 NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
94 NSLog(@"sent event cur %d max %d",i,numPlaylists);
95 [playlists addObject:theObj];
97 return [playlists autorelease];
100 - (int)numberOfSongsInPlaylistAtIndex:(int)index
102 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];
105 - (NSString *)classOfPlaylistAtIndex:(int)index
107 int realResult = [[ITAppleEventCenter sharedCenter]
108 sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
110 if (realResult == 'cRTP') return @"radio tuner playlist";
111 else return @"playlist";
114 - (int)currentPlaylistIndex
117 result = [[ITAppleEventCenter sharedCenter]
118 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
122 - (NSString *)songTitleAtIndex:(int)index
124 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];
127 - (int)currentSongIndex
130 result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
134 - (NSString *)currentSongTitle
136 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
139 - (NSString *)currentSongArtist
141 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
144 - (NSString *)currentSongAlbum
146 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
149 - (NSString *)currentSongGenre
151 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
154 - (NSString *)currentSongLength
156 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
159 - (NSString *)currentSongRemaining
161 long duration = [[ITAppleEventCenter sharedCenter]
162 sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
163 long current = [[ITAppleEventCenter sharedCenter]
164 sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
166 return [[NSNumber numberWithLong:duration - current] stringValue];
169 - (float)currentSongRating
174 - (BOOL)setCurrentSongRating:(float)rating
184 - (BOOL)setVolume:(float)volume
189 - (NSArray *)eqPresets;
192 long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
193 NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
195 for (i = 1; i <= numPresets; i++) {
196 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];
197 if (theObj) [presets addObject:theObj];
199 return [presets autorelease];
202 - (int)currentEQPresetIndex
205 result = [[ITAppleEventCenter sharedCenter]
206 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"appPSN:iTunesPSN];
212 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
218 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
224 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
228 - (BOOL)goToPreviousSong
230 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
236 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
242 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
247 - (BOOL)switchToPlaylistAtIndex:(int)index
249 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
253 - (BOOL)switchToSongAtIndex:(int)index
255 [[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];
259 - (BOOL)switchToEQAtIndex:(int)index
261 [[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];
262 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:1, '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
266 - (ProcessSerialNumber)iTunesPSN
268 NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
269 ProcessSerialNumber number;
271 int count = [apps count];
273 number.highLongOfPSN = kNoProcess;
275 for (i = 0; i < count; i++)
277 NSDictionary *curApp = [apps objectAtIndex:i];
279 if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
281 number.highLongOfPSN = [[curApp objectForKey:
282 @"NSApplicationProcessSerialNumberHigh"] intValue];
283 number.lowLongOfPSN = [[curApp objectForKey:
284 @"NSApplicationProcessSerialNumberLow"] intValue];
290 - (void)applicationLaunched:(NSNotification *)note
292 NSDictionary *info = [note userInfo];
294 if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
295 iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
296 iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
298 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
302 - (void)applicationTerminated:(NSNotification *)note
304 NSDictionary *info = [note userInfo];
306 if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
307 iTunesPSN.highLongOfPSN = kNoProcess;
308 [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];