Number returning methods now work, for the most part.
[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.";
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     
31     NSLog(@"iTunes Plugin loaded");
32     return YES;
33 }
34
35 - (BOOL)halt
36 {
37     iTunesPSN.highLongOfPSN = kNoProcess;
38     
39     //Unregister for application termination in NSWorkspace
40     return YES;
41 }
42
43 - (NSArray *)sources
44 {
45     //This is probably unneeded
46     return nil;
47 }
48
49 - (int)currentSourceIndex
50 {
51     //This is probably unneeded
52     return nil;
53 }
54
55 - (NSArray *)playlistsForCurrentSource
56 {
57     //This is probably unneeded
58     return nil;
59 }
60
61 - (NSString *)sourceTypeOfCurrentPlaylist
62 {
63     //Not working yet. It returns the 4 character code instead of a name.
64     NSString *result;
65     result = [[ITAppleEventCenter sharedCenter]
66                 sendTwoTierAEWithRequestedKey:@"pcls"
67                 fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd"
68                 appPSN:[self iTunesPSN]];
69     return result;
70 }
71
72 - (int)currentPlaylistIndex
73 {
74     int result;
75     result = [[ITAppleEventCenter sharedCenter]
76                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"
77                 fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd"
78                 appPSN:[self iTunesPSN]];
79     return result;
80 }
81
82 - (NSString *)songTitleAtIndex
83 {
84     return nil; 
85 }
86
87 - (int)currentSongIndex
88 {
89     int result;
90     result = [[ITAppleEventCenter sharedCenter]
91                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"
92                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
93                 appPSN:[self iTunesPSN]];
94     return result;
95 }
96
97 - (NSString *)currentSongTitle
98 {
99     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam"
100                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
101                 appPSN:[self iTunesPSN]];
102 }
103
104 - (NSString *)currentSongArtist
105 {
106     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt"
107                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
108                 appPSN:[self iTunesPSN]];
109 }
110
111 - (NSString *)currentSongAlbum
112 {
113     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb"
114                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
115                 appPSN:[self iTunesPSN]];
116 }
117
118 - (NSString *)currentSongGenre
119 {
120     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen"
121                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
122                 appPSN:[self iTunesPSN]];
123 }
124
125 - (NSString *)currentSongLength
126 {
127     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pDur"
128                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
129                 appPSN:[self iTunesPSN]];
130 }
131
132 - (NSString *)currentSongRemaining
133 {
134     long duration = [[ITAppleEventCenter sharedCenter]
135                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur"
136                         fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
137                         appPSN:[self iTunesPSN]];
138     long current = [[ITAppleEventCenter sharedCenter]
139                         sendAEWithRequestedKeyForNumber:@"pPos"
140                         eventClass:@"core" eventID:@"getd"
141                         appPSN:[self iTunesPSN]];
142     
143     return [[NSNumber numberWithLong:duration - current] stringValue];
144 }
145
146 - (NSArray *)eqPresets;
147 {
148     return nil;
149 }
150
151 - (BOOL)play
152 {
153     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play"
154             appPSN:[self iTunesPSN]];
155     return YES;
156 }
157
158 - (BOOL)pause
159 {
160     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus"
161             appPSN:[self iTunesPSN]];
162     return YES;
163 }
164
165 - (BOOL)goToNextSong
166 {
167     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next"
168             appPSN:[self iTunesPSN]];
169     return YES;
170 }
171
172 - (BOOL)goToPreviousSong
173 {
174     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev"
175             appPSN:[self iTunesPSN]];
176     return YES;
177 }
178
179 - (BOOL)goToNextPlaylist
180 {
181     //This is probably unneeded
182     return NO;
183 }
184
185 - (BOOL)goToPreviousPlaylist
186 {
187     //This is probably unneeded
188     return NO;
189 }
190
191 - (BOOL)switchToSourceAtIndex:(int)index
192 {
193     //This is probably unneeded
194     return NO;
195 }
196
197 - (BOOL)switchToPlaylistAtIndex:(int)index
198 {
199     return NO;
200 }
201
202 - (BOOL)switchToSongAtIndex:(int)index
203 {
204     return NO;
205 }
206
207 - (BOOL)switchToEQAtIndex:(int)index
208 {
209     return NO;
210 }
211
212 - (ProcessSerialNumber)iTunesPSN
213 {
214     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
215     ProcessSerialNumber number;
216     int i;
217     
218     number.highLongOfPSN = kNoProcess;
219     
220     for (i = 0; i < [apps count]; i++)
221     {
222         NSDictionary *curApp = [apps objectAtIndex:i];
223         
224         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
225         {
226             number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
227             number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
228         }
229     }
230     return number;
231 }
232
233 @end