Making MenuTunes use the ITKit, for ITStatusItem. There are a few notes:
[MenuTunes.git] / MenuTunes.m
1 /*
2 Things to do:
3 ¥ Make preferences window pretty
4 ¥ Optimize
5 ¥ Apple Events! Apple Events! Apple Events!
6 */
7
8 #import "MenuTunes.h"
9 #import "PreferencesController.h"
10 #import "HotKeyCenter.h"
11 #import "StatusWindowController.h"
12
13 @interface MenuTunes(Private)
14 - (void)registerDefaultsIfNeeded;
15 - (void)updateMenu;
16 - (void)rebuildUpcomingSongsMenu;
17 - (void)rebuildPlaylistMenu;
18 - (void)rebuildEQPresetsMenu;
19 - (void)setupHotKeys;
20 - (NSString *)runScriptAndReturnResult:(NSString *)script;
21 - (void)timerUpdate;
22 - (void)sendAEWithEventClass:(AEEventClass)eventClass andEventID:(AEEventID)eventID;
23
24 @end
25
26 @implementation MenuTunes
27
28 /*************************************************************************/
29 #pragma mark -
30 #pragma mark INITIALIZATION METHODS
31 /*************************************************************************/
32
33 - (void)applicationDidFinishLaunching:(NSNotification *)note
34 {
35     asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
36
37     [self registerDefaultsIfNeeded];
38     
39     menu = [[NSMenu alloc] initWithTitle:@""];
40     iTunesPSN = [self iTunesPSN]; //Get PSN of iTunes if it's running
41     
42     if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
43     {
44         [self rebuildMenu];
45         refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
46                                                         target:self
47                                                       selector:@selector(timerUpdate)
48                                                       userInfo:nil
49                                                        repeats:YES];
50     }
51     else
52     {
53         menu = [[NSMenu alloc] initWithTitle:@""];
54         [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
55         [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
56         [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
57         [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
58         refreshTimer = nil;
59     }
60
61     statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
62                                               withLength:NSVariableStatusItemLength];
63     
64     [statusItem setImage:[NSImage imageNamed:@"menu"]];
65     [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
66     [statusItem setMenu:menu];
67     // Below line of code is for creating builds for Beta Testers
68     // [statusItem setToolTip:@"This Nontransferable Beta (Built on __DATE__) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com)."];
69 }
70
71
72 /*************************************************************************/
73 #pragma mark -
74 #pragma mark INSTANCE METHODS
75 /*************************************************************************/
76
77 - (void)registerDefaultsIfNeeded
78 {
79     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
80     if (![defaults objectForKey:@"menu"]) {
81         [defaults setObject:
82             [NSArray arrayWithObjects:
83                 @"Play/Pause",
84                 @"Next Track",
85                 @"Previous Track",
86                 @"Fast Forward",
87                 @"Rewind",
88                 @"<separator>",
89                 @"Upcoming Songs",
90                 @"Playlists",
91                 @"<separator>",
92                 @"PreferencesÉ",
93                 @"Quit",
94                 @"<separator>",
95                 @"Current Track Info",
96                 nil] forKey:@"menu"];
97     }
98     
99     if (![defaults objectForKey:@"showName"]) {
100         [defaults setBool:YES forKey:@"showName"];
101     }
102     
103     if (![defaults objectForKey:@"showArtist"]) {
104         [defaults setBool:YES forKey:@"showArtist"];
105     }
106     
107     if (![defaults objectForKey:@"showAlbum"]) {
108         [defaults setBool:NO forKey:@"showAlbum"];
109     }
110     
111     if (![defaults objectForKey:@"showTime"]) {
112         [defaults setBool:NO forKey:@"showTime"];
113     }
114 }
115
116 //Recreate the status item menu
117 - (void)rebuildMenu
118 {
119     NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
120     int i;
121     
122     trackInfoIndex = -1;
123     didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
124     didHaveArtistName = ([[self runScriptAndReturnResult:@"return artist of current track"] length] > 0);
125     
126     
127     while ([menu numberOfItems] > 0) {
128         [menu removeItemAtIndex:0];
129     }
130     
131     playPauseMenuItem = nil;
132     upcomingSongsItem = nil;
133     playlistItem = nil;
134     [playlistMenu release];
135     playlistMenu = nil;
136     eqItem = nil;
137     [eqMenu release];
138     eqMenu = nil;
139     
140     for (i = 0; i < [myMenu count]; i++) {
141         NSString *item = [myMenu objectAtIndex:i];
142         if ([item isEqualToString:@"Play/Pause"]) {
143             playPauseMenuItem = [menu addItemWithTitle:@"Play"
144                                                 action:@selector(playPause:)
145                                          keyEquivalent:@""];
146             [playPauseMenuItem setTarget:self];
147         } else if ([item isEqualToString:@"Next Track"]) {
148             [[menu addItemWithTitle:@"Next Track"
149                              action:@selector(nextSong:)
150                       keyEquivalent:@""] setTarget:self];
151         } else if ([item isEqualToString:@"Previous Track"]) {
152             [[menu addItemWithTitle:@"Previous Track"
153                              action:@selector(prevSong:)
154                       keyEquivalent:@""] setTarget:self];
155         } else if ([item isEqualToString:@"Fast Forward"]) {
156             [[menu addItemWithTitle:@"Fast Forward"
157                              action:@selector(fastForward:)
158                       keyEquivalent:@""] setTarget:self];
159         } else if ([item isEqualToString:@"Rewind"]) {
160             [[menu addItemWithTitle:@"Rewind"
161                              action:@selector(rewind:)
162                       keyEquivalent:@""] setTarget:self];
163         } else if ([item isEqualToString:@"Upcoming Songs"]) {
164             upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
165                                                 action:nil
166                                          keyEquivalent:@""];
167         } else if ([item isEqualToString:@"Playlists"]) {
168             playlistItem = [menu addItemWithTitle:@"Playlists"
169                                            action:nil
170                                     keyEquivalent:@""];
171         } else if ([item isEqualToString:@"EQ Presets"]) {
172             eqItem = [menu addItemWithTitle:@"EQ Presets"
173                                      action:nil
174                               keyEquivalent:@""];
175         } else if ([item isEqualToString:@"PreferencesÉ"]) {
176             [[menu addItemWithTitle:@"PreferencesÉ"
177                              action:@selector(showPreferences:)
178                       keyEquivalent:@""] setTarget:self];
179         } else if ([item isEqualToString:@"Quit"]) {
180             [[menu addItemWithTitle:@"Quit"
181                              action:@selector(quitMenuTunes:)
182                       keyEquivalent:@""] setTarget:self];
183         } else if ([item isEqualToString:@"Current Track Info"]) {
184             trackInfoIndex = [menu numberOfItems];
185             [menu addItemWithTitle:@"No Song"
186                             action:nil
187                      keyEquivalent:@""];
188         } else if ([item isEqualToString:@"<separator>"]) {
189             [menu addItem:[NSMenuItem separatorItem]];
190         }
191     }
192     
193     curTrackIndex = -1; //Force update of everything
194     [self timerUpdate]; //Updates dynamic info in the menu
195     
196     [self clearHotKeys];
197     [self setupHotKeys];
198 }
199
200 //Updates the menu with current player state, song, and upcoming songs
201 - (void)updateMenu
202 {
203     NSMenuItem *menuItem;
204     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
205     
206     if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
207         return;
208     }
209     
210     if (upcomingSongsItem) {
211         [self rebuildUpcomingSongsMenu];
212     }
213     
214     if (playlistItem) {
215         [self rebuildPlaylistMenu];
216     }
217     
218     if (eqItem) {
219         [self rebuildEQPresetsMenu];
220     }
221     
222     if (trackInfoIndex > -1)
223     {
224         NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
225         curSongName = [self runScriptAndReturnResult:@"return name of current track"];
226         
227         if ([defaults boolForKey:@"showArtist"]) {
228             curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
229         }
230         
231         if ([defaults boolForKey:@"showAlbum"]) {
232             curArtistName = [self runScriptAndReturnResult:@"return artist of current track"];
233         }
234         
235         if ([curSongName length] > 0) {
236             int index = [menu indexOfItemWithTitle:@"Now Playing"];
237             
238             if (index > -1) {
239                 if ([defaults boolForKey:@"showName"]) {
240                     [menu removeItemAtIndex:index + 1];
241                 }
242                 if (didHaveAlbumName) {
243                     [menu removeItemAtIndex:index + 1];
244                 }
245                 if (didHaveArtistName) {
246                     [menu removeItemAtIndex:index + 1];
247                 }
248                 if ([defaults boolForKey:@"showTime"]) {
249                     [menu removeItemAtIndex:index + 1];
250                 }
251             }
252             if (!isPlayingRadio) {
253                 if ([defaults boolForKey:@"showTime"]) {
254                     menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", [self runScriptAndReturnResult:@"return time of current track"]]
255                                                         action:nil
256                                                         keyEquivalent:@""];
257                     [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
258                     [menuItem release];
259                 }
260                 
261                 if ([curArtistName length] > 0) {
262                     menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curArtistName]
263                                                         action:nil
264                                                         keyEquivalent:@""];
265                     [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
266                     [menuItem release];
267                 }
268                 
269                 if ([curAlbumName length] > 0) {
270                     menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curAlbumName]
271                                                         action:nil
272                                                         keyEquivalent:@""];
273                     [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
274                     [menuItem release];
275                 }
276             }
277             menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curSongName]
278                                                 action:nil
279                                                 keyEquivalent:@""];
280             [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
281             [menuItem release];
282             
283             if (index == -1) {
284                 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
285                 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
286                 [menu insertItem:menuItem atIndex:trackInfoIndex];
287                 [menuItem release];
288             }
289             
290         } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
291             [menu removeItemAtIndex:trackInfoIndex];
292             
293             if ([defaults boolForKey:@"showName"] == YES) {
294                 [menu removeItemAtIndex:trackInfoIndex];
295             }
296             
297             if ([defaults boolForKey:@"showTime"] == YES) {
298                 [menu removeItemAtIndex:trackInfoIndex];
299             }
300             
301             if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
302                 [menu removeItemAtIndex:trackInfoIndex];
303             }
304             
305             if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
306                 [menu removeItemAtIndex:trackInfoIndex];
307             }
308             
309             menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
310             [menu insertItem:menuItem atIndex:trackInfoIndex];
311             [menuItem release];
312         }
313         
314         if ([defaults boolForKey:@"showArtist"]) {
315                 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
316             }
317             
318             if ([defaults boolForKey:@"showAlbum"]) {
319                 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
320             }
321     }
322 }
323
324 //Rebuild the upcoming songs submenu. Can be improved a lot.
325 - (void)rebuildUpcomingSongsMenu
326 {
327     int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
328     int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
329     if (!isPlayingRadio) {
330         if (numSongs > 0) {
331             int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
332             int i;
333             
334             [upcomingSongsMenu release];
335             upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
336             [upcomingSongsItem setSubmenu:upcomingSongsMenu];
337             [upcomingSongsItem setEnabled:YES];
338             
339             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
340                 if (i <= numSongs) {
341                     NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
342                     NSMenuItem *songItem;
343                     songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
344                     [songItem setTarget:self];
345                     [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
346                     [upcomingSongsMenu addItem:songItem];
347                     [songItem release];
348                 } else {
349                     break;
350                 }
351             }
352         }
353     } else {
354         [upcomingSongsItem setSubmenu:nil];
355         [upcomingSongsItem setEnabled:NO];
356     }
357 }
358
359 - (void)rebuildPlaylistMenu
360 {
361     int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
362     int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
363     
364     if (isPlayingRadio)
365     {
366         curPlaylist = 0;
367     }
368     
369     if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
370         return;
371     
372     [playlistMenu release];
373     playlistMenu = [[NSMenu alloc] initWithTitle:@""];
374     
375     for (i = 1; i <= numPlaylists; i++) {
376         NSString *playlistName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of playlist %i", i]];
377         NSMenuItem *tempItem;
378         tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
379         [tempItem setTarget:self];
380         [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
381         [playlistMenu addItem:tempItem];
382         [tempItem release];
383     }
384     [playlistItem setSubmenu:playlistMenu];
385     
386     if (curPlaylist) {
387         [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
388     }
389 }
390
391 //Build a menu with the list of all available EQ presets
392 - (void)rebuildEQPresetsMenu
393 {
394     int numSets = [[self runScriptAndReturnResult:@"return number of EQ presets"] intValue];
395     int i;
396     
397     if (eqMenu && (numSets == [eqMenu numberOfItems]))
398         return;
399     
400     [eqMenu release];
401     eqMenu = [[NSMenu alloc] initWithTitle:@""];
402     
403     for (i = 1; i <= numSets; i++) {
404         NSString *setName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of EQ preset %i", i]];
405         NSMenuItem *tempItem;
406         tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
407         [tempItem setTarget:self];
408         [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
409         [eqMenu addItem:tempItem];
410         [tempItem release];
411     }
412     [eqItem setSubmenu:eqMenu];
413     
414     [[eqMenu itemAtIndex:[[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue] - 1] setState:NSOnState];
415 }
416
417 - (void)clearHotKeys
418 {
419     [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
420     [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
421     [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
422     [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
423     [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
424 }
425
426 - (void)setupHotKeys
427 {
428     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
429     
430     if ([defaults objectForKey:@"PlayPause"] != nil) {
431         [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
432                 combo:[defaults keyComboForKey:@"PlayPause"]
433                 target:self action:@selector(playPause:)];
434     }
435     
436     if ([defaults objectForKey:@"NextTrack"] != nil) {
437         [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
438                 combo:[defaults keyComboForKey:@"NextTrack"]
439                 target:self action:@selector(nextSong:)];
440     }
441     
442     if ([defaults objectForKey:@"PrevTrack"] != nil) {
443         [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
444                 combo:[defaults keyComboForKey:@"PrevTrack"]
445                 target:self action:@selector(prevSong:)];
446     }
447     
448     if ([defaults objectForKey:@"TrackInfo"] != nil) {
449         [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
450                 combo:[defaults keyComboForKey:@"TrackInfo"]
451                 target:self action:@selector(showCurrentTrackInfo)];
452     }
453     
454     if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
455         [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
456                combo:[defaults keyComboForKey:@"UpcomingSongs"]
457                target:self action:@selector(showUpcomingSongs)];
458     }
459 }
460
461 //Runs an AppleScript and returns the result as an NSString after stripping quotes, if needed. It takes in script and automatically adds the tell iTunes and end tell statements.
462 - (NSString *)runScriptAndReturnResult:(NSString *)script
463 {
464     AEDesc scriptDesc, resultDesc;
465     Size length;
466     NSString *result;
467     Ptr buffer;
468     
469     script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
470     
471     AECreateDesc(typeChar, [script cString], [script cStringLength], 
472 &scriptDesc);
473     
474     OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
475     
476     length = AEGetDescDataSize(&resultDesc);
477     buffer = malloc(length);
478     
479     AEGetDescData(&resultDesc, buffer, length);
480     AEDisposeDesc(&scriptDesc);
481     AEDisposeDesc(&resultDesc);
482     result = [NSString stringWithCString:buffer length:length];
483     if ( (! [result isEqualToString:@""])      &&
484          ([result characterAtIndex:0] == '\"') &&
485          ([result characterAtIndex:[result length] - 1] == '\"') ) {
486         result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
487     }
488     free(buffer);
489     buffer = nil;
490     return result;
491 }
492
493 //Called when the timer fires.
494 - (void)timerUpdate
495 {
496     int pid;
497     if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
498         int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
499         
500         if (trackPlayingIndex != curTrackIndex) {
501             bool wasPlayingRadio = isPlayingRadio;
502             isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
503             if (isPlayingRadio && !wasPlayingRadio) {
504                 int i;
505                 for (i = 0; i < [playlistMenu numberOfItems]; i++)
506                 {
507                     [[playlistMenu itemAtIndex:i] setState:NSOffState];
508                 }
509             }
510             if (wasPlayingRadio) {
511                 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
512                 [menu insertItem:temp atIndex:trackInfoIndex + 1];
513                 [temp release];
514             }
515             [self updateMenu];
516             curTrackIndex = trackPlayingIndex;
517         }
518         else
519         {
520             int playlist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
521             if (playlist != curPlaylistIndex) {
522                 bool wasPlayingRadio = isPlayingRadio;
523                 isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
524                 if (isPlayingRadio && !wasPlayingRadio) {
525                     int i;
526                     for (i = 0; i < [playlistMenu numberOfItems]; i++)
527                     {
528                         [[playlistMenu itemAtIndex:i] setState:NSOffState];
529                     }
530                 }
531                 if (wasPlayingRadio) {
532                     NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
533                     [menu insertItem:temp atIndex:trackInfoIndex + 1];
534                     [temp release];
535                 }
536                 [self updateMenu];
537                 curTrackIndex = trackPlayingIndex;
538                 curPlaylistIndex = playlist;
539             }
540         }
541         //Update Play/Pause menu item
542         if (playPauseMenuItem){
543             if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
544                 [playPauseMenuItem setTitle:@"Pause"];
545             } else {
546                 [playPauseMenuItem setTitle:@"Play"];
547             }
548         }
549     } else {
550         [menu release];
551         menu = [[NSMenu alloc] initWithTitle:@""];
552         [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
553         [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
554         [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
555         [statusItem setMenu:menu];
556         
557         [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
558         [refreshTimer invalidate];
559         refreshTimer = nil;
560         [self clearHotKeys];
561     }
562 }
563
564 - (void)iTunesLaunched:(NSNotification *)note
565 {
566     NSDictionary *info = [note userInfo];
567     
568     iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
569     iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
570     
571     //Restart the timer
572     refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES]; 
573     
574     [self rebuildMenu]; //Rebuild the menu since no songs will be playing
575     [statusItem setMenu:menu]; //Set the menu back to the main one
576     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
577 }
578
579 //Return the PSN of iTunes, if it's running
580 - (ProcessSerialNumber)iTunesPSN
581 {
582     ProcessSerialNumber procNum;
583     procNum.highLongOfPSN = kNoProcess;
584     procNum.lowLongOfPSN = 0;
585     
586     while ( (GetNextProcess(&procNum) == noErr) ) {
587         CFStringRef procName;
588         if ( (CopyProcessName(&procNum, &procName) == noErr) ) {
589             if ([(NSString *)procName isEqualToString:@"iTunes"]) {
590                 return procNum;
591             }
592             [(NSString *)procName release];
593         }
594     }
595     return procNum;
596 }
597
598 //Send an AppleEvent with a given event ID
599 - (void)sendAEWithEventClass:(AEEventClass)eventClass 
600 andEventID:(AEEventID)eventID
601 {
602     OSType iTunesType = 'hook';
603     AppleEvent event, reply;
604     
605     AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
606     
607     AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
608     AEDisposeDesc(&event);
609     AEDisposeDesc(&reply);
610 }
611
612 //
613 // Selectors - called from status item menu
614 //
615
616 - (void)playTrack:(id)sender
617 {
618     [self runScriptAndReturnResult:[NSString stringWithFormat:@"play track %i of current playlist", [[sender representedObject] intValue]]];
619     [self updateMenu];
620 }
621
622 - (void)selectPlaylist:(id)sender
623 {
624     int playlist = [[sender representedObject] intValue];
625     if (!isPlayingRadio) {
626         int curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
627         [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
628     }
629     [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
630     [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
631 }
632
633 - (void)selectEQPreset:(id)sender
634 {
635     int curSet = [[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue];
636     int item = [[sender representedObject] intValue];
637     [self runScriptAndReturnResult:[NSString stringWithFormat:@"set current EQ preset to EQ preset %i", item]];
638     [self runScriptAndReturnResult:@"set EQ enabled to 1"];
639     [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
640     [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
641 }
642
643 - (void)playPause:(id)sender
644 {
645     NSString *state = [self runScriptAndReturnResult:@"return player state"];
646     if ([state isEqualToString:@"playing"]) {
647         [self sendAEWithEventClass:'hook' andEventID:'Paus'];
648         [playPauseMenuItem setTitle:@"Play"];
649     } else if ([state isEqualToString:@"fast forwarding"] || [state 
650 isEqualToString:@"rewinding"]) {
651         [self sendAEWithEventClass:'hook' andEventID:'Paus'];
652         [self sendAEWithEventClass:'hook' andEventID:'Play'];
653     } else {
654         [self sendAEWithEventClass:'hook' andEventID:'Play'];
655         [playPauseMenuItem setTitle:@"Pause"];
656     }
657 }
658
659 - (void)nextSong:(id)sender
660 {
661     [self sendAEWithEventClass:'hook' andEventID:'Next'];
662 }
663
664 - (void)prevSong:(id)sender
665 {
666     [self sendAEWithEventClass:'hook' andEventID:'Prev'];
667 }
668
669 - (void)fastForward:(id)sender
670 {
671     [self sendAEWithEventClass:'hook' andEventID:'Fast'];
672 }
673
674 - (void)rewind:(id)sender
675 {
676     [self sendAEWithEventClass:'hook' andEventID:'Rwnd'];
677 }
678
679 - (void)quitMenuTunes:(id)sender
680 {
681     [NSApp terminate:self];
682 }
683
684 - (void)openiTunes:(id)sender
685 {
686     [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
687 }
688
689 - (void)showPreferences:(id)sender
690 {
691     if (!prefsController) {
692         prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
693         [self clearHotKeys];
694     }
695 }
696
697
698 - (void)closePreferences
699 {
700     if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))) {
701         [self setupHotKeys];
702     }
703     [prefsController release];
704     prefsController = nil;
705 }
706
707 //
708 //
709 // Show Current Track Info And Show Upcoming Songs Floaters
710 //
711 //
712
713 - (void)showCurrentTrackInfo
714 {
715     NSString *trackName = [self runScriptAndReturnResult:@"return name of current track"];
716     if (!statusController && [trackName length]) {
717         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
718         NSString *stringToShow = @"";
719         int lines = 1;
720         
721         if ([defaults boolForKey:@"showName"]) {
722             if ([defaults boolForKey:@"showArtist"]) {
723                 NSString *trackArtist = [self runScriptAndReturnResult:@"return artist of current track"];
724                 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
725             }
726             stringToShow = [stringToShow stringByAppendingString:trackName];
727             stringToShow = [stringToShow stringByAppendingString:@"\n"];
728             if ([trackName length] > 38) {
729                 lines++;
730             }
731             lines++;
732         }
733         
734         if ([defaults boolForKey:@"showAlbum"]) {
735             NSString *trackAlbum = [self runScriptAndReturnResult:@"return album of current track"];
736             if ([trackAlbum length]) {
737                 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
738                 stringToShow = [stringToShow stringByAppendingString:@"\n"];
739                 lines++;
740             }
741         }
742         
743         if ([defaults boolForKey:@"showTime"]) {
744             NSString *trackTime = [self runScriptAndReturnResult:@"return time of current track"];
745             if ([trackTime length]) {
746                 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
747                 lines++;
748             }
749         }
750         
751         {
752             int trackTimeLeft = [[self runScriptAndReturnResult:@"return (duration of current track) - player position"] intValue];
753             int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
754             if (seconds < 10) {
755                 stringToShow = [stringToShow stringByAppendingString:
756                             [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
757             } else {
758                 stringToShow = [stringToShow stringByAppendingString:
759                             [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
760             }
761         }
762         
763         statusController = [[StatusWindowController alloc] init];
764         [statusController setTrackInfo:stringToShow lines:lines];
765         [NSTimer scheduledTimerWithTimeInterval:3.0
766                                          target:self
767                                        selector:@selector(fadeAndCloseStatusWindow)
768                                        userInfo:nil
769                                         repeats:NO];
770     }
771 }
772
773 - (void)showUpcomingSongs
774 {
775     if (!statusController) {
776         int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
777         
778         if (numSongs > 0) {
779             int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
780             int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
781             int i;
782             NSString *songs = @"";
783             
784             statusController = [[StatusWindowController alloc] init];
785             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
786                 if (i <= numSongs) {
787                     NSString *curSong = [self runScriptAndReturnResult:
788                         [NSString stringWithFormat:@"return name of track %i of current playlist", i]];
789                     songs = [songs stringByAppendingString:curSong];
790                     songs = [songs stringByAppendingString:@"\n"];
791                 }
792             }
793             [statusController setUpcomingSongs:songs numSongs:numSongsInAdvance];
794             [NSTimer scheduledTimerWithTimeInterval:3.0
795                                              target:self
796                                            selector:@selector(fadeAndCloseStatusWindow)
797                                            userInfo:nil
798                                             repeats:NO];
799         }
800     }
801 }
802
803 - (void)fadeAndCloseStatusWindow
804 {
805     [statusController fadeWindowOut];
806     [statusController release];
807     statusController = nil;
808 }
809
810 /*************************************************************************/
811 #pragma mark -
812 #pragma mark NSApplication DELEGATE METHODS
813 /*************************************************************************/
814
815 - (void)applicationWillTerminate:(NSNotification *)note
816 {
817     [self clearHotKeys];
818     [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
819 }
820
821
822 /*************************************************************************/
823 #pragma mark -
824 #pragma mark DEALLOCATION METHODS
825 /*************************************************************************/
826
827 - (void)dealloc
828 {
829     if (refreshTimer) {
830         [refreshTimer invalidate];
831         refreshTimer = nil;
832     }
833     CloseComponent(asComponent);
834     [statusItem release];
835     [menu release];
836 //  [view release];
837     [super dealloc];
838 }
839
840
841 @end