3 ¥ Make preferences window pretty
5 ¥ Apple Events! Apple Events! Apple Events!
9 #import "PreferencesController.h"
10 #import "HotKeyCenter.h"
11 #import "StatusWindowController.h"
13 @interface MenuTunes(Private)
14 - (void)registerDefaultsIfNeeded;
16 - (void)rebuildUpcomingSongsMenu;
17 - (void)rebuildPlaylistMenu;
18 - (void)rebuildEQPresetsMenu;
20 - (NSString *)runScriptAndReturnResult:(NSString *)script;
22 - (void)sendAEWithEventClass:(AEEventClass)eventClass andEventID:(AEEventID)eventID;
26 @implementation MenuTunes
28 /*************************************************************************/
30 #pragma mark INITIALIZATION METHODS
31 /*************************************************************************/
33 - (void)applicationDidFinishLaunching:(NSNotification *)note
35 asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
37 [self registerDefaultsIfNeeded];
39 menu = [[NSMenu alloc] initWithTitle:@""];
40 iTunesPSN = [self iTunesPSN]; //Get PSN of iTunes if it's running
42 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
45 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
47 selector:@selector(timerUpdate)
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];
61 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
62 withLength:NSVariableStatusItemLength];
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 XXX) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com)."];
72 /*************************************************************************/
74 #pragma mark INSTANCE METHODS
75 /*************************************************************************/
77 - (void)registerDefaultsIfNeeded
79 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
80 if (![defaults objectForKey:@"menu"]) {
82 [NSArray arrayWithObjects:
95 @"Current Track Info",
99 if (![defaults objectForKey:@"showName"]) {
100 [defaults setBool:YES forKey:@"showName"];
103 if (![defaults objectForKey:@"showArtist"]) {
104 [defaults setBool:YES forKey:@"showArtist"];
107 if (![defaults objectForKey:@"showAlbum"]) {
108 [defaults setBool:NO forKey:@"showAlbum"];
111 if (![defaults objectForKey:@"showTime"]) {
112 [defaults setBool:NO forKey:@"showTime"];
116 //Recreate the status item menu
119 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
123 didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
124 didHaveArtistName = ([[self runScriptAndReturnResult:@"return artist of current track"] length] > 0);
127 while ([menu numberOfItems] > 0) {
128 [menu removeItemAtIndex:0];
131 playPauseMenuItem = nil;
132 upcomingSongsItem = nil;
134 [playlistMenu release];
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:)
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"
167 } else if ([item isEqualToString:@"Playlists"]) {
168 playlistItem = [menu addItemWithTitle:@"Playlists"
171 } else if ([item isEqualToString:@"EQ Presets"]) {
172 eqItem = [menu addItemWithTitle:@"EQ Presets"
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"
188 } else if ([item isEqualToString:@"<separator>"]) {
189 [menu addItem:[NSMenuItem separatorItem]];
193 curTrackIndex = -1; //Force update of everything
194 [self timerUpdate]; //Updates dynamic info in the menu
200 //Updates the menu with current player state, song, and upcoming songs
203 NSMenuItem *menuItem;
204 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
206 if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
210 if (upcomingSongsItem) {
211 [self rebuildUpcomingSongsMenu];
215 [self rebuildPlaylistMenu];
219 [self rebuildEQPresetsMenu];
222 if (trackInfoIndex > -1)
224 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
225 curSongName = [self runScriptAndReturnResult:@"return name of current track"];
227 if ([defaults boolForKey:@"showArtist"]) {
228 curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
231 if ([defaults boolForKey:@"showAlbum"]) {
232 curArtistName = [self runScriptAndReturnResult:@"return artist of current track"];
235 if ([curSongName length] > 0) {
236 int index = [menu indexOfItemWithTitle:@"Now Playing"];
239 if ([defaults boolForKey:@"showName"]) {
240 [menu removeItemAtIndex:index + 1];
242 if (didHaveAlbumName) {
243 [menu removeItemAtIndex:index + 1];
245 if (didHaveArtistName) {
246 [menu removeItemAtIndex:index + 1];
248 if ([defaults boolForKey:@"showTime"]) {
249 [menu removeItemAtIndex:index + 1];
252 if (!isPlayingRadio) {
253 if ([defaults boolForKey:@"showTime"]) {
254 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [self runScriptAndReturnResult:@"return time of current track"]]
257 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
261 if ([curArtistName length] > 0) {
262 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
265 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
269 if ([curAlbumName length] > 0) {
270 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
273 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
277 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
280 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
284 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
285 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
286 [menu insertItem:menuItem atIndex:trackInfoIndex];
290 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
291 [menu removeItemAtIndex:trackInfoIndex];
293 if ([defaults boolForKey:@"showName"] == YES) {
294 [menu removeItemAtIndex:trackInfoIndex];
297 if ([defaults boolForKey:@"showTime"] == YES) {
298 [menu removeItemAtIndex:trackInfoIndex];
301 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
302 [menu removeItemAtIndex:trackInfoIndex];
305 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
306 [menu removeItemAtIndex:trackInfoIndex];
309 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
310 [menu insertItem:menuItem atIndex:trackInfoIndex];
314 if ([defaults boolForKey:@"showArtist"]) {
315 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
318 if ([defaults boolForKey:@"showAlbum"]) {
319 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
324 //Rebuild the upcoming songs submenu. Can be improved a lot.
325 - (void)rebuildUpcomingSongsMenu
327 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
328 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
329 if (!isPlayingRadio) {
331 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
334 [upcomingSongsMenu release];
335 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
336 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
337 [upcomingSongsItem setEnabled:YES];
339 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
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];
354 [upcomingSongsItem setSubmenu:nil];
355 [upcomingSongsItem setEnabled:NO];
359 - (void)rebuildPlaylistMenu
361 int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
362 int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
369 if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
372 [playlistMenu release];
373 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
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];
384 [playlistItem setSubmenu:playlistMenu];
387 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
391 //Build a menu with the list of all available EQ presets
392 - (void)rebuildEQPresetsMenu
394 int numSets = [[self runScriptAndReturnResult:@"return number of EQ presets"] intValue];
397 if (eqMenu && (numSets == [eqMenu numberOfItems]))
401 eqMenu = [[NSMenu alloc] initWithTitle:@""];
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];
412 [eqItem setSubmenu:eqMenu];
414 [[eqMenu itemAtIndex:[[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue] - 1] setState:NSOnState];
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"];
428 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
430 if ([defaults objectForKey:@"PlayPause"] != nil) {
431 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
432 combo:[defaults keyComboForKey:@"PlayPause"]
433 target:self action:@selector(playPause:)];
436 if ([defaults objectForKey:@"NextTrack"] != nil) {
437 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
438 combo:[defaults keyComboForKey:@"NextTrack"]
439 target:self action:@selector(nextSong:)];
442 if ([defaults objectForKey:@"PrevTrack"] != nil) {
443 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
444 combo:[defaults keyComboForKey:@"PrevTrack"]
445 target:self action:@selector(prevSong:)];
448 if ([defaults objectForKey:@"TrackInfo"] != nil) {
449 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
450 combo:[defaults keyComboForKey:@"TrackInfo"]
451 target:self action:@selector(showCurrentTrackInfo)];
454 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
455 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
456 combo:[defaults keyComboForKey:@"UpcomingSongs"]
457 target:self action:@selector(showUpcomingSongs)];
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
464 AEDesc scriptDesc, resultDesc;
469 script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
471 AECreateDesc(typeChar, [script cString], [script cStringLength],
474 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
476 length = AEGetDescDataSize(&resultDesc);
477 buffer = malloc(length);
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)];
493 //Called when the timer fires.
497 if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
498 int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
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) {
505 for (i = 0; i < [playlistMenu numberOfItems]; i++)
507 [[playlistMenu itemAtIndex:i] setState:NSOffState];
510 if (wasPlayingRadio) {
511 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
512 [menu insertItem:temp atIndex:trackInfoIndex + 1];
516 curTrackIndex = trackPlayingIndex;
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) {
526 for (i = 0; i < [playlistMenu numberOfItems]; i++)
528 [[playlistMenu itemAtIndex:i] setState:NSOffState];
531 if (wasPlayingRadio) {
532 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
533 [menu insertItem:temp atIndex:trackInfoIndex + 1];
537 curTrackIndex = trackPlayingIndex;
538 curPlaylistIndex = playlist;
541 //Update Play/Pause menu item
542 if (playPauseMenuItem){
543 if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
544 [playPauseMenuItem setTitle:@"Pause"];
546 [playPauseMenuItem setTitle:@"Play"];
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];
557 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
558 [refreshTimer invalidate];
564 - (void)iTunesLaunched:(NSNotification *)note
566 NSDictionary *info = [note userInfo];
568 iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
569 iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
572 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
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];
579 //Return the PSN of iTunes, if it's running
580 - (ProcessSerialNumber)iTunesPSN
582 ProcessSerialNumber procNum;
583 procNum.highLongOfPSN = kNoProcess;
584 procNum.lowLongOfPSN = 0;
586 while ( (GetNextProcess(&procNum) == noErr) ) {
587 CFStringRef procName;
588 if ( (CopyProcessName(&procNum, &procName) == noErr) ) {
589 if ([(NSString *)procName isEqualToString:@"iTunes"]) {
592 [(NSString *)procName release];
598 //Send an AppleEvent with a given event ID
599 - (void)sendAEWithEventClass:(AEEventClass)eventClass
600 andEventID:(AEEventID)eventID
602 OSType iTunesType = 'hook';
603 AppleEvent event, reply;
605 AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
607 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
608 AEDisposeDesc(&event);
609 AEDisposeDesc(&reply);
613 // Selectors - called from status item menu
616 - (void)playTrack:(id)sender
618 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play track %i of current playlist", [[sender representedObject] intValue]]];
622 - (void)selectPlaylist:(id)sender
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];
629 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
630 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
633 - (void)selectEQPreset:(id)sender
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];
643 - (void)playPause:(id)sender
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'];
654 [self sendAEWithEventClass:'hook' andEventID:'Play'];
655 [playPauseMenuItem setTitle:@"Pause"];
659 - (void)nextSong:(id)sender
661 [self sendAEWithEventClass:'hook' andEventID:'Next'];
664 - (void)prevSong:(id)sender
666 [self sendAEWithEventClass:'hook' andEventID:'Prev'];
669 - (void)fastForward:(id)sender
671 [self sendAEWithEventClass:'hook' andEventID:'Fast'];
674 - (void)rewind:(id)sender
676 [self sendAEWithEventClass:'hook' andEventID:'Rwnd'];
679 - (void)quitMenuTunes:(id)sender
681 [NSApp terminate:self];
684 - (void)openiTunes:(id)sender
686 [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
689 - (void)showPreferences:(id)sender
691 if (!prefsController) {
692 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
698 - (void)closePreferences
700 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))) {
703 [prefsController release];
704 prefsController = nil;
709 // Show Current Track Info And Show Upcoming Songs Floaters
713 - (void)showCurrentTrackInfo
715 NSString *trackName = [self runScriptAndReturnResult:@"return name of current track"];
716 if (!statusController && [trackName length]) {
717 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
718 NSString *stringToShow = @"";
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];
726 stringToShow = [stringToShow stringByAppendingString:trackName];
727 stringToShow = [stringToShow stringByAppendingString:@"\n"];
728 if ([trackName length] > 38) {
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"];
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];
752 int trackTimeLeft = [[self runScriptAndReturnResult:@"return (duration of current track) - player position"] intValue];
753 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
755 stringToShow = [stringToShow stringByAppendingString:
756 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
758 stringToShow = [stringToShow stringByAppendingString:
759 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
763 statusController = [[StatusWindowController alloc] init];
764 [statusController setTrackInfo:stringToShow lines:lines];
765 [NSTimer scheduledTimerWithTimeInterval:3.0
767 selector:@selector(fadeAndCloseStatusWindow)
773 - (void)showUpcomingSongs
775 if (!statusController) {
776 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
779 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
780 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
782 NSString *songs = @"";
784 statusController = [[StatusWindowController alloc] init];
785 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
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"];
793 [statusController setUpcomingSongs:songs numSongs:numSongsInAdvance];
794 [NSTimer scheduledTimerWithTimeInterval:3.0
796 selector:@selector(fadeAndCloseStatusWindow)
803 - (void)fadeAndCloseStatusWindow
805 [statusController fadeWindowOut];
806 [statusController release];
807 statusController = nil;
810 /*************************************************************************/
812 #pragma mark NSApplication DELEGATE METHODS
813 /*************************************************************************/
815 - (void)applicationWillTerminate:(NSNotification *)note
818 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
822 /*************************************************************************/
824 #pragma mark DEALLOCATION METHODS
825 /*************************************************************************/
830 [refreshTimer invalidate];
833 CloseComponent(asComponent);
834 [statusItem release];