1 #import "iTunesRemote.h"
3 @implementation iTunesRemote
7 return [[[iTunesRemote alloc] init] autorelease];
10 - (NSString *)remoteTitle
12 return @"iTunes Remote";
15 - (NSString *)remoteInformation
17 return @"Default MenuTunes plugin to control iTunes, by iThink Software.";
20 - (NSImage *)remoteIcon
27 savedPSN = [self iTunesPSN];
36 - (NSString *)playerFullName
41 - (NSString *)playerSimpleName
46 - (NSDictionary *)capabilities
48 return [NSDictionary dictionaryWithObjectsAndKeys:
49 [NSNumber numberWithBool: YES], @"Remote",
50 [NSNumber numberWithBool: YES], @"Basic Track Control",
51 [NSNumber numberWithBool: YES], @"Track Information",
52 [NSNumber numberWithBool: YES], @"Track Navigation",
53 [NSNumber numberWithBool: YES], @"Upcoming Songs",
54 [NSNumber numberWithBool: YES], @"Playlists",
55 [NSNumber numberWithBool: YES], @"Volume",
56 [NSNumber numberWithBool: YES], @"Shuffle",
57 [NSNumber numberWithBool: YES], @"Repeat Modes",
58 [NSNumber numberWithBool: YES], @"Equalizer",
59 [NSNumber numberWithBool: YES], @"Track Rating",
63 - (BOOL)showPrimaryInterface
65 // Make this into AppleEvents... shouldn't be too hard, I'm just too tired to do it right now.
66 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(1), '----':obj { form:'prop', want:type('prop'), seld:type('pisf'), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
67 // Still have to convert these to AEs:
68 // set visible of browser window 1 to true
69 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(1), ----:obj { form:'prop', want:'prop', seld:'pvis', from:obj { form:'indx', want:'cBrW', seld:1, from:'null'() } }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
70 // set minimized of browser window 1 to false
71 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(0), ----:obj { form:'prop', want:'prop', seld:'pMin', from:obj { form:'indx', want:'cBrW', seld:1, from:'null'() } }" eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
76 - (ITMTRemotePlayerRunningState)playerRunningState
78 NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
80 int count = [apps count];
82 for (i = 0; i < count; i++) {
83 if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
84 return ITMTRemotePlayerRunning;
87 return ITMTRemotePlayerNotRunning;
90 - (ITMTRemotePlayerPlayingState)playerPlayingState
92 long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
98 return ITMTRemotePlayerStopped;
100 return ITMTRemotePlayerPlaying;
102 return ITMTRemotePlayerPaused;
104 return ITMTRemotePlayerRewinding;
106 return ITMTRemotePlayerForwarding;
109 return ITMTRemotePlayerStopped;
112 - (NSArray *)playlists
115 const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':()" eventClass:@"core" eventID:@"cnte" appPSN:savedPSN];
116 NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
118 for (i = 1; i <= numPlaylists; i++) {
120 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];
121 NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
122 [playlists addObject:theObj];
124 return [playlists autorelease];
127 - (int)numberOfSongsInPlaylistAtIndex:(int)index
129 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:savedPSN];
132 - (ITMTRemotePlayerPlaylistClass)currentPlaylistClass
134 int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
140 return ITMTRemotePlayerLibraryPlaylist;
143 return ITMTRemotePlayerRadioPlaylist;
146 return ITMTRemotePlayerPlaylist;
150 - (int)currentPlaylistIndex
152 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
155 - (NSString *)songTitleAtIndex:(int)index
157 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:savedPSN];
160 - (int)currentAlbumTrackCount
162 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pTrC" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
165 - (int)currentSongTrack
167 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pTrN" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
170 - (NSString *)playerStateUniqueIdentifier
172 return [NSString stringWithFormat:@"%i-%i", [self currentPlaylistIndex], [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pDID" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN]];
175 - (int)currentSongIndex
177 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
180 - (NSString *)currentSongTitle
182 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
185 - (NSString *)currentSongArtist
187 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
190 - (NSString *)currentSongAlbum
192 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
195 - (NSString *)currentSongGenre
197 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
200 - (NSString *)currentSongLength
202 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
205 - (NSString *)currentSongRemaining
207 long duration = [[ITAppleEventCenter sharedCenter]
208 sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
209 long current = [[ITAppleEventCenter sharedCenter]
210 sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
212 return [[NSNumber numberWithLong:duration - current] stringValue];
215 - (float)currentSongRating
217 return (float)[[ITAppleEventCenter sharedCenter]
218 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:savedPSN] / 100.0;
221 - (BOOL)setCurrentSongRating:(float)rating
223 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pRte'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",(long)(rating*100),[self currentSongIndex]] eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
227 /* - (BOOL)equalizerEnabled
229 int thingy = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:type('prop'), want:type('prop'), seld:type('pEQ '), from:() }" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
230 NSLog(@"Debug equalizerEnabled: %i", thingy);
234 - (BOOL)setEqualizerEnabled:(BOOL)enabled
236 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }",enabled] eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
240 - (NSArray *)eqPresets
243 long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:savedPSN];
244 NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
246 for (i = 1; i <= numPresets; i++) {
247 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:savedPSN];
249 [presets addObject:theObj];
252 return [presets autorelease];
255 - (int)currentEQPresetIndex
258 result = [[ITAppleEventCenter sharedCenter]
259 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
265 return (float)[[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:savedPSN] / 100;
268 - (BOOL)setVolume:(float)volume
270 [[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:savedPSN];
274 - (BOOL)shuffleEnabled
276 int result = [[ITAppleEventCenter sharedCenter]
277 sendTwoTierAEWithRequestedKeyForNumber:@"pShf" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
281 - (BOOL)setShuffleEnabled:(BOOL)enabled
283 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu) ----:obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",(unsigned long)enabled] eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
287 - (ITMTRemotePlayerRepeatMode)repeatMode
289 FourCharCode m00f = 0;
291 m00f = [[ITAppleEventCenter sharedCenter]
292 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:savedPSN];
297 result = ITMTRemotePlayerRepeatOff;
300 result = ITMTRemotePlayerRepeatOne;
303 result = ITMTRemotePlayerRepeatAll;
310 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
315 case ITMTRemotePlayerRepeatOff:
318 case ITMTRemotePlayerRepeatOne:
321 case ITMTRemotePlayerRepeatAll:
326 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:type('%s') ----:obj { form:'prop', want:type('pRpt'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",m00f] eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
332 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:savedPSN];
338 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:savedPSN];
344 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:savedPSN];
348 - (BOOL)goToPreviousSong
350 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:savedPSN];
356 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:savedPSN];
362 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:savedPSN];
366 - (BOOL)switchToPlaylistAtIndex:(int)index
368 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:savedPSN];
372 - (BOOL)switchToSongAtIndex:(int)index
374 [[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:savedPSN];
378 - (BOOL)switchToEQAtIndex:(int)index
380 // index should count from 0, but itunes counts from 1, so let's add 1.
381 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pEQP'), from:'null'() }, data:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() }",(index+1)] eventClass:@"core" eventID:@"setd" appPSN:savedPSN];
385 - (ProcessSerialNumber)iTunesPSN
387 /*NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
388 ProcessSerialNumber number;
390 int count = [apps count];
392 number.highLongOfPSN = kNoProcess;
394 for (i = 0; i < count; i++)
396 NSDictionary *curApp = [apps objectAtIndex:i];
398 if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
400 number.highLongOfPSN = [[curApp objectForKey:
401 @"NSApplicationProcessSerialNumberHigh"] intValue];
402 number.lowLongOfPSN = [[curApp objectForKey:
403 @"NSApplicationProcessSerialNumberLow"] intValue];
407 ProcessSerialNumber number;
408 number.highLongOfPSN = kNoProcess;
409 number.lowLongOfPSN = 0;
411 while ( (GetNextProcess(&number) == noErr) )
414 if ( (CopyProcessName(&number, &name) == noErr) )
416 if ([(NSString *)name isEqualToString:@"iTunes"])
420 [(NSString *)name release];