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