1 #import "iTunesRemote.h"
3 @implementation iTunesRemote
7 return [[[iTunesRemote alloc] init] autorelease];
12 return @"iTunes Plug-in";
15 - (NSString *)information;
17 return @"Default MenuTunes plugin to control iTunes.";
27 iTunesPSN = [self iTunesPSN];
28 asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
30 //Register for application termination in NSWorkspace
32 NSLog(@"iTunes Plugin loaded");
38 iTunesPSN.highLongOfPSN = kNoProcess;
39 CloseComponent(asComponent);
41 //Unregister for application termination in NSWorkspace
45 - (int)numberOfPlaylists
47 NSString *result = [self runScriptAndReturnResult:@"get number of playlists"];
48 return [result intValue];
51 - (int)numberOfSongsInPlaylistAtIndex:(int)index
53 NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get number of tracks in playlist %i", index]];
54 return [result intValue];
57 - (NSString *)classOfPlaylistAtIndex:(int)index
59 //Not working yet. It returns the 4 character code instead of a name.
61 result = [[ITAppleEventCenter sharedCenter]
62 sendTwoTierAEWithRequestedKey:@"pcls"
63 fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd"
64 appPSN:[self iTunesPSN]];*/
65 NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get class of playlist %i", index]];
69 - (int)currentPlaylistIndex
72 result = [[ITAppleEventCenter sharedCenter]
73 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"
74 fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd"
75 appPSN:[self iTunesPSN]];
79 - (NSString *)songTitleAtIndex:(int)index
81 NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i", index]];
85 - (int)currentSongIndex
88 result = [[ITAppleEventCenter sharedCenter]
89 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"
90 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
91 appPSN:[self iTunesPSN]];
95 - (NSString *)currentSongTitle
97 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam"
98 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
99 appPSN:[self iTunesPSN]];
102 - (NSString *)currentSongArtist
104 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt"
105 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
106 appPSN:[self iTunesPSN]];
109 - (NSString *)currentSongAlbum
111 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb"
112 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
113 appPSN:[self iTunesPSN]];
116 - (NSString *)currentSongGenre
118 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen"
119 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
120 appPSN:[self iTunesPSN]];
123 - (NSString *)currentSongLength
125 return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pDur"
126 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
127 appPSN:[self iTunesPSN]];
130 - (NSString *)currentSongRemaining
132 long duration = [[ITAppleEventCenter sharedCenter]
133 sendTwoTierAEWithRequestedKeyForNumber:@"pDur"
134 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
135 appPSN:[self iTunesPSN]];
136 long current = [[ITAppleEventCenter sharedCenter]
137 sendAEWithRequestedKeyForNumber:@"pPos"
138 eventClass:@"core" eventID:@"getd"
139 appPSN:[self iTunesPSN]];
141 return [[NSNumber numberWithLong:duration - current] stringValue];
144 - (NSArray *)eqPresets;
147 int numPresets = [[self runScriptAndReturnResult:@"get number of EQ presets"] intValue];
148 NSMutableArray *presets = [[NSMutableArray alloc] init];
150 for (i = 0; i < numPresets; i++) {
151 [presets addObject:[self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of EQ preset %i", i]]];
154 return [NSArray arrayWithArray:presets];
159 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play"
160 appPSN:[self iTunesPSN]];
166 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus"
167 appPSN:[self iTunesPSN]];
173 NSLog(@"%@", [self classOfPlaylistAtIndex:3]);
174 //[[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next"
175 // appPSN:[self iTunesPSN]];
179 - (BOOL)goToPreviousSong
181 [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev"
182 appPSN:[self iTunesPSN]];
186 - (BOOL)switchToPlaylistAtIndex:(int)index
188 [self runScriptAndReturnResult:[NSString stringWithFormat:
189 @"set current playlist to playlist %i", index]];
193 - (BOOL)switchToSongAtIndex:(int)index
195 [self runScriptAndReturnResult:[NSString stringWithFormat:
196 @"play track %i of current playlist", index]];
200 - (BOOL)switchToEQAtIndex:(int)index
202 [self runScriptAndReturnResult:[NSString stringWithFormat:
203 @"set current EQ preset to EQ preset %i", index]];
207 - (ProcessSerialNumber)iTunesPSN
209 NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
210 ProcessSerialNumber number;
213 number.highLongOfPSN = kNoProcess;
215 for (i = 0; i < [apps count]; i++)
217 NSDictionary *curApp = [apps objectAtIndex:i];
219 if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
221 number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
222 number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
228 - (NSString *)runScriptAndReturnResult:(NSString *)script
230 AEDesc scriptDesc, resultDesc;
235 script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
237 AECreateDesc(typeChar, [script cString], [script cStringLength],
240 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
242 length = AEGetDescDataSize(&resultDesc);
243 buffer = malloc(length);
245 AEGetDescData(&resultDesc, buffer, length);
246 AEDisposeDesc(&scriptDesc);
247 AEDisposeDesc(&resultDesc);
248 result = [NSString stringWithCString:buffer length:length];
249 if ( (! [result isEqualToString:@""]) &&
250 ([result characterAtIndex:0] == '\"') &&
251 ([result characterAtIndex:[result length] - 1] == '\"') ) {
252 result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];