Added stuff for current song rating. Right now in the submenu there are
[MenuTunes.git] / iTunesRemote.m
1 #import "iTunesRemote.h"
2
3 @implementation iTunesRemote
4
5 + (id)remote
6 {
7     return [[[iTunesRemote alloc] init] autorelease];
8 }
9
10 - (NSString *)title
11 {
12     return @"iTunes Plug-in";
13 }
14
15 - (NSString *)information;
16 {
17     return @"Default MenuTunes plugin to control iTunes. Written by iThink Software.";
18 }
19
20 - (NSImage *)icon
21 {
22     return nil;
23 }
24
25 - (BOOL)begin
26 {
27     iTunesPSN = [self iTunesPSN];
28
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];
32
33     return YES;
34 }
35
36 - (BOOL)halt
37 {
38     iTunesPSN.highLongOfPSN = kNoProcess;
39
40     //Unregister for application termination in NSWorkspace
41     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
42
43     return YES;
44 }
45
46 - (BOOL)isAppRunning
47 {
48     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
49     int i;
50     int count = [apps count];
51
52     for (i = 0; i < count; i++) {
53         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
54                 isEqualToString:@"iTunes"]) {
55             return YES;
56         }
57     }
58     return NO;
59 }
60
61 - (PlayerState)playerState
62 {
63     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
64     
65     switch (result)
66     {
67         default:
68         case 'kPSS':
69             return stopped;
70         case 'kPSP':
71             return playing;
72         case 'kPSp':
73             return paused;
74         case 'kPSR':
75             return rewinding;
76         case 'kPSF':
77             return forwarding;
78     }
79     
80     return stopped;
81 }
82
83 - (NSArray *)playlists
84 {
85     long i = 0;
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];
88
89
90            for (i = 1; i <= numPlaylists; i++) {
91                   const long j = 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];
96            }
97            return [playlists autorelease];
98 }
99
100 - (int)numberOfSongsInPlaylistAtIndex:(int)index
101 {
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];
103 }
104
105 - (NSString *)classOfPlaylistAtIndex:(int)index
106 {
107     int realResult = [[ITAppleEventCenter sharedCenter]
108                 sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
109
110     if (realResult == 'cRTP') return @"radio tuner playlist";
111     else return @"playlist";
112 }
113
114 - (int)currentPlaylistIndex
115 {
116     int result;
117     result = [[ITAppleEventCenter sharedCenter]
118                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
119     return result;
120 }
121
122 - (NSString *)songTitleAtIndex:(int)index
123 {
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];
125 }
126
127 - (int)currentSongIndex
128 {
129     int result;
130     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
131     return result;
132 }
133
134 - (NSString *)currentSongTitle
135 {
136     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
137 }
138
139 - (NSString *)currentSongArtist
140 {
141     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
142 }
143
144 - (NSString *)currentSongAlbum
145 {
146     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
147 }
148
149 - (NSString *)currentSongGenre
150 {
151     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
152 }
153
154 - (NSString *)currentSongLength
155 {
156     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
157 }
158
159 - (NSString *)currentSongRemaining
160 {
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];
165
166     return [[NSNumber numberWithLong:duration - current] stringValue];
167 }
168
169 - (int)currentSongRating
170 {
171     return 0;
172 }
173
174 - (BOOL)setCurrentSongRating:(int)rating
175 {
176     return NO;
177 }
178
179 - (NSArray *)eqPresets;
180 {
181     int i;
182     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
183     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
184
185            for (i = 1; i <= numPresets; i++) {
186                   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];
187                   if (theObj) [presets addObject:theObj];
188            }
189            return [presets autorelease];
190 }
191
192 - (int)currentEQPresetIndex
193 {
194     int result;
195     result = [[ITAppleEventCenter sharedCenter]
196                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"appPSN:iTunesPSN];
197     return result;
198 }
199
200 - (BOOL)play
201 {
202     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
203     return YES;
204 }
205
206 - (BOOL)pause
207 {
208     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
209     return YES;
210 }
211
212 - (BOOL)goToNextSong
213 {
214     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
215     return YES;
216 }
217
218 - (BOOL)goToPreviousSong
219 {
220     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
221     return YES;
222 }
223
224 - (BOOL)fastForward
225 {
226     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
227     return YES;
228 }
229
230 - (BOOL)rewind
231 {
232     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
233     return YES;
234 }
235
236
237 - (BOOL)switchToPlaylistAtIndex:(int)index
238 {
239     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
240     return YES;
241 }
242
243 - (BOOL)switchToSongAtIndex:(int)index
244 {
245     [[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];
246     return YES;
247 }
248
249 - (BOOL)switchToEQAtIndex:(int)index
250 {
251     [[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];
252     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:1, '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
253     return YES;
254 }
255
256 - (ProcessSerialNumber)iTunesPSN
257 {
258     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
259     ProcessSerialNumber number;
260     int i;
261     int count = [apps count];
262     
263     number.highLongOfPSN = kNoProcess;
264     
265     for (i = 0; i < count; i++)
266     {
267         NSDictionary *curApp = [apps objectAtIndex:i];
268         
269         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
270         {
271             number.highLongOfPSN = [[curApp objectForKey:
272                 @"NSApplicationProcessSerialNumberHigh"] intValue];
273             number.lowLongOfPSN = [[curApp objectForKey:
274                 @"NSApplicationProcessSerialNumberLow"] intValue];
275         }
276     }
277     return number;
278 }
279
280 - (void)applicationLaunched:(NSNotification *)note
281 {
282     NSDictionary *info = [note userInfo];
283
284     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
285         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
286         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
287
288         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
289     }
290 }
291
292 - (void)applicationTerminated:(NSNotification *)note
293 {
294     NSDictionary *info = [note userInfo];
295
296     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
297         iTunesPSN.highLongOfPSN = kNoProcess;
298         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
299     }
300 }
301
302 @end