3 ¥ Make preferences window pretty
5 ¥ Apple Events! Apple Events! Apple Events!
7 ¥ Finish up registration frontend
11 #import "PreferencesController.h"
12 #import "HotKeyCenter.h"
13 #import "StatusWindowController.h"
15 @interface MenuTunes(Private)
17 - (void)rebuildUpcomingSongsMenu;
18 - (void)rebuildPlaylistMenu;
19 - (void)rebuildEQPresetsMenu;
21 - (NSString *)runScriptAndReturnResult:(NSString *)script;
23 - (void)sendAEWithEventClass:(AEEventClass)eventClass andEventID:(AEEventID)eventID;
27 @implementation MenuTunes
29 /*************************************************************************/
31 #pragma mark INITIALIZATION METHODS
32 /*************************************************************************/
34 - (void)applicationDidFinishLaunching:(NSNotification *)note
36 asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
38 [self registerDefaultsIfNeeded];
40 menu = [[NSMenu alloc] initWithTitle:@""];
41 iTunesPSN = [self iTunesPSN]; //Get PSN of iTunes if it's running
43 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
46 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
48 selector:@selector(timerUpdate)
54 menu = [[NSMenu alloc] initWithTitle:@""];
55 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
56 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
57 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
58 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
62 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
63 withLength:NSSquareStatusItemLength];
65 [statusItem setImage:[NSImage imageNamed:@"menu"]];
66 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
67 [statusItem setMenu:menu];
68 // Below line of code is for creating builds for Beta Testers
69 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
73 /*************************************************************************/
75 #pragma mark INSTANCE METHODS
76 /*************************************************************************/
78 - (void)registerDefaultsIfNeeded
80 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
81 if (![defaults objectForKey:@"menu"]) {
83 NSMutableDictionary *loginwindow;
84 NSMutableArray *loginarray;
88 [NSArray arrayWithObjects:
101 @"Current Track Info",
102 nil] forKey:@"menu"];
104 [defaults synchronize];
105 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
106 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
108 for (i = 0; i < [loginarray count]; i++) {
109 NSDictionary *tempDict = [loginarray objectAtIndex:i];
110 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
116 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
117 AEDesc scriptDesc, resultDesc;
118 NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
120 AECreateDesc(typeChar, [script cString], [script cStringLength],
123 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
125 AEDisposeDesc(&scriptDesc);
126 AEDisposeDesc(&resultDesc);
131 if (![defaults integerForKey:@"SongsInAdvance"])
133 [defaults setInteger:5 forKey:@"SongsInAdvance"];
136 if (![defaults objectForKey:@"showName"]) {
137 [defaults setBool:YES forKey:@"showName"];
140 if (![defaults objectForKey:@"showArtist"]) {
141 [defaults setBool:YES forKey:@"showArtist"];
144 if (![defaults objectForKey:@"showAlbum"]) {
145 [defaults setBool:NO forKey:@"showAlbum"];
148 if (![defaults objectForKey:@"showTime"]) {
149 [defaults setBool:NO forKey:@"showTime"];
153 //Recreate the status item menu
156 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
160 didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
161 didHaveArtistName = ([[self runScriptAndReturnResult:@"return artist of current track"] length] > 0);
164 while ([menu numberOfItems] > 0) {
165 [menu removeItemAtIndex:0];
168 playPauseMenuItem = nil;
169 upcomingSongsItem = nil;
171 [playlistMenu release];
177 for (i = 0; i < [myMenu count]; i++) {
178 NSString *item = [myMenu objectAtIndex:i];
179 if ([item isEqualToString:@"Play/Pause"]) {
180 playPauseMenuItem = [menu addItemWithTitle:@"Play"
181 action:@selector(playPause:)
183 [playPauseMenuItem setTarget:self];
184 } else if ([item isEqualToString:@"Next Track"]) {
185 [[menu addItemWithTitle:@"Next Track"
186 action:@selector(nextSong:)
187 keyEquivalent:@""] setTarget:self];
188 } else if ([item isEqualToString:@"Previous Track"]) {
189 [[menu addItemWithTitle:@"Previous Track"
190 action:@selector(prevSong:)
191 keyEquivalent:@""] setTarget:self];
192 } else if ([item isEqualToString:@"Fast Forward"]) {
193 [[menu addItemWithTitle:@"Fast Forward"
194 action:@selector(fastForward:)
195 keyEquivalent:@""] setTarget:self];
196 } else if ([item isEqualToString:@"Rewind"]) {
197 [[menu addItemWithTitle:@"Rewind"
198 action:@selector(rewind:)
199 keyEquivalent:@""] setTarget:self];
200 } else if ([item isEqualToString:@"Upcoming Songs"]) {
201 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
204 } else if ([item isEqualToString:@"Playlists"]) {
205 playlistItem = [menu addItemWithTitle:@"Playlists"
208 } else if ([item isEqualToString:@"EQ Presets"]) {
209 eqItem = [menu addItemWithTitle:@"EQ Presets"
212 } else if ([item isEqualToString:@"PreferencesÉ"]) {
213 [[menu addItemWithTitle:@"PreferencesÉ"
214 action:@selector(showPreferences:)
215 keyEquivalent:@""] setTarget:self];
216 } else if ([item isEqualToString:@"Quit"]) {
217 [[menu addItemWithTitle:@"Quit"
218 action:@selector(quitMenuTunes:)
219 keyEquivalent:@""] setTarget:self];
220 } else if ([item isEqualToString:@"Current Track Info"]) {
221 trackInfoIndex = [menu numberOfItems];
222 [menu addItemWithTitle:@"No Song"
225 } else if ([item isEqualToString:@"<separator>"]) {
226 [menu addItem:[NSMenuItem separatorItem]];
230 curTrackIndex = -1; //Force update of everything
231 [self timerUpdate]; //Updates dynamic info in the menu
237 //Updates the menu with current player state, song, and upcoming songs
240 NSMenuItem *menuItem;
241 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
243 if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
247 if (upcomingSongsItem) {
248 [self rebuildUpcomingSongsMenu];
252 [self rebuildPlaylistMenu];
256 [self rebuildEQPresetsMenu];
259 if (trackInfoIndex > -1)
261 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
262 curSongName = [self runScriptAndReturnResult:@"return name of current track"];
264 if ([defaults boolForKey:@"showAlbum"]) {
265 curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
268 if ([defaults boolForKey:@"showArtist"]) {
269 curArtistName = [self runScriptAndReturnResult:@"return artist of current track"];
272 if ([curSongName length] > 0) {
273 int index = [menu indexOfItemWithTitle:@"Now Playing"];
275 if ([defaults boolForKey:@"showName"]) {
276 [menu removeItemAtIndex:index + 1];
278 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
279 [menu removeItemAtIndex:index + 1];
281 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
282 [menu removeItemAtIndex:index + 1];
284 if ([defaults boolForKey:@"showTime"]) {
285 [menu removeItemAtIndex:index + 1];
289 if (!isPlayingRadio) {
290 if ([defaults boolForKey:@"showTime"]) {
291 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [self runScriptAndReturnResult:@"return time of current track"]]
294 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
298 if ([curArtistName length] > 0) {
299 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
302 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
306 if ([curAlbumName length] > 0) {
307 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
310 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
315 if ([defaults boolForKey:@"showName"]) {
316 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
319 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
324 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
325 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
326 [menu insertItem:menuItem atIndex:trackInfoIndex];
329 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
330 [menu removeItemAtIndex:trackInfoIndex];
332 if ([defaults boolForKey:@"showName"] == YES) {
333 [menu removeItemAtIndex:trackInfoIndex];
336 if ([defaults boolForKey:@"showTime"] == YES) {
337 [menu removeItemAtIndex:trackInfoIndex];
340 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
341 [menu removeItemAtIndex:trackInfoIndex];
344 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
345 [menu removeItemAtIndex:trackInfoIndex];
348 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
349 [menu insertItem:menuItem atIndex:trackInfoIndex];
353 if ([defaults boolForKey:@"showArtist"]) {
354 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
357 if ([defaults boolForKey:@"showAlbum"]) {
358 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
363 //Rebuild the upcoming songs submenu. Can be improved a lot.
364 - (void)rebuildUpcomingSongsMenu
366 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
367 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
368 if (!isPlayingRadio) {
370 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
373 [upcomingSongsMenu release];
374 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
375 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
376 [upcomingSongsItem setEnabled:YES];
378 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
380 NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
381 NSMenuItem *songItem;
382 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
383 [songItem setTarget:self];
384 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
385 [upcomingSongsMenu addItem:songItem];
393 [upcomingSongsItem setSubmenu:nil];
394 [upcomingSongsItem setEnabled:NO];
398 - (void)rebuildPlaylistMenu
400 int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
401 int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
408 if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
411 [playlistMenu release];
412 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
414 for (i = 1; i <= numPlaylists; i++) {
415 NSString *playlistName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of playlist %i", i]];
416 NSMenuItem *tempItem;
417 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
418 [tempItem setTarget:self];
419 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
420 [playlistMenu addItem:tempItem];
423 [playlistItem setSubmenu:playlistMenu];
426 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
430 //Build a menu with the list of all available EQ presets
431 - (void)rebuildEQPresetsMenu
433 int numSets = [[self runScriptAndReturnResult:@"return number of EQ presets"] intValue];
436 if (eqMenu && (numSets == [eqMenu numberOfItems]))
440 eqMenu = [[NSMenu alloc] initWithTitle:@""];
442 for (i = 1; i <= numSets; i++) {
443 NSString *setName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of EQ preset %i", i]];
444 NSMenuItem *tempItem;
445 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
446 [tempItem setTarget:self];
447 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
448 [eqMenu addItem:tempItem];
451 [eqItem setSubmenu:eqMenu];
453 [[eqMenu itemAtIndex:[[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue] - 1] setState:NSOnState];
458 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
459 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
460 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
461 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
462 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
467 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
469 if ([defaults objectForKey:@"PlayPause"] != nil) {
470 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
471 combo:[defaults keyComboForKey:@"PlayPause"]
472 target:self action:@selector(playPause:)];
475 if ([defaults objectForKey:@"NextTrack"] != nil) {
476 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
477 combo:[defaults keyComboForKey:@"NextTrack"]
478 target:self action:@selector(nextSong:)];
481 if ([defaults objectForKey:@"PrevTrack"] != nil) {
482 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
483 combo:[defaults keyComboForKey:@"PrevTrack"]
484 target:self action:@selector(prevSong:)];
487 if ([defaults objectForKey:@"TrackInfo"] != nil) {
488 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
489 combo:[defaults keyComboForKey:@"TrackInfo"]
490 target:self action:@selector(showCurrentTrackInfo)];
493 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
494 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
495 combo:[defaults keyComboForKey:@"UpcomingSongs"]
496 target:self action:@selector(showUpcomingSongs)];
500 //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.
501 - (NSString *)runScriptAndReturnResult:(NSString *)script
503 AEDesc scriptDesc, resultDesc;
508 script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
510 AECreateDesc(typeChar, [script cString], [script cStringLength],
513 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
515 length = AEGetDescDataSize(&resultDesc);
516 buffer = malloc(length);
518 AEGetDescData(&resultDesc, buffer, length);
519 AEDisposeDesc(&scriptDesc);
520 AEDisposeDesc(&resultDesc);
521 result = [NSString stringWithCString:buffer length:length];
522 if ( (! [result isEqualToString:@""]) &&
523 ([result characterAtIndex:0] == '\"') &&
524 ([result characterAtIndex:[result length] - 1] == '\"') ) {
525 result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
532 //Called when the timer fires.
536 if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
537 int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
539 if (trackPlayingIndex != curTrackIndex) {
540 bool wasPlayingRadio = isPlayingRadio;
541 isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
542 if (isPlayingRadio && !wasPlayingRadio) {
544 for (i = 0; i < [playlistMenu numberOfItems]; i++)
546 [[playlistMenu itemAtIndex:i] setState:NSOffState];
549 if (wasPlayingRadio) {
550 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
551 [menu insertItem:temp atIndex:trackInfoIndex + 1];
555 curTrackIndex = trackPlayingIndex;
559 int playlist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
560 if (playlist != curPlaylistIndex) {
561 bool wasPlayingRadio = isPlayingRadio;
562 isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
563 if (isPlayingRadio && !wasPlayingRadio) {
565 for (i = 0; i < [playlistMenu numberOfItems]; i++)
567 [[playlistMenu itemAtIndex:i] setState:NSOffState];
570 if (wasPlayingRadio) {
571 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
572 [menu insertItem:temp atIndex:trackInfoIndex + 1];
576 curTrackIndex = trackPlayingIndex;
577 curPlaylistIndex = playlist;
580 //Update Play/Pause menu item
581 if (playPauseMenuItem){
582 if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
583 [playPauseMenuItem setTitle:@"Pause"];
585 [playPauseMenuItem setTitle:@"Play"];
590 menu = [[NSMenu alloc] initWithTitle:@""];
591 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
592 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
593 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
594 [statusItem setMenu:menu];
596 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
597 [refreshTimer invalidate];
603 - (void)iTunesLaunched:(NSNotification *)note
605 NSDictionary *info = [note userInfo];
607 iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
608 iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
611 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
613 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
614 [statusItem setMenu:menu]; //Set the menu back to the main one
615 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
618 //Return the PSN of iTunes, if it's running
619 - (ProcessSerialNumber)iTunesPSN
621 ProcessSerialNumber procNum;
622 procNum.highLongOfPSN = kNoProcess;
623 procNum.lowLongOfPSN = 0;
625 while ( (GetNextProcess(&procNum) == noErr) ) {
626 CFStringRef procName;
627 if ( (CopyProcessName(&procNum, &procName) == noErr) ) {
628 if ([(NSString *)procName isEqualToString:@"iTunes"]) {
637 //Send an AppleEvent with a given event ID
638 - (void)sendAEWithEventClass:(AEEventClass)eventClass
639 andEventID:(AEEventID)eventID
641 OSType iTunesType = 'hook';
642 AppleEvent event, reply;
644 AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
646 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
647 AEDisposeDesc(&event);
648 AEDisposeDesc(&reply);
652 // Selectors - called from status item menu
655 - (void)playTrack:(id)sender
657 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play track %i of current playlist", [[sender representedObject] intValue]]];
661 - (void)selectPlaylist:(id)sender
663 int playlist = [[sender representedObject] intValue];
664 if (!isPlayingRadio) {
665 int curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
666 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
668 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
669 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
672 - (void)selectEQPreset:(id)sender
674 int curSet = [[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue];
675 int item = [[sender representedObject] intValue];
676 [self runScriptAndReturnResult:[NSString stringWithFormat:@"set current EQ preset to EQ preset %i", item]];
677 [self runScriptAndReturnResult:@"set EQ enabled to 1"];
678 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
679 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
682 - (void)playPause:(id)sender
684 NSString *state = [self runScriptAndReturnResult:@"return player state"];
685 if ([state isEqualToString:@"playing"]) {
686 [self sendAEWithEventClass:'hook' andEventID:'Paus'];
687 [playPauseMenuItem setTitle:@"Play"];
688 } else if ([state isEqualToString:@"fast forwarding"] || [state
689 isEqualToString:@"rewinding"]) {
690 [self sendAEWithEventClass:'hook' andEventID:'Paus'];
691 [self sendAEWithEventClass:'hook' andEventID:'Play'];
693 [self sendAEWithEventClass:'hook' andEventID:'Play'];
694 [playPauseMenuItem setTitle:@"Pause"];
698 - (void)nextSong:(id)sender
700 [self sendAEWithEventClass:'hook' andEventID:'Next'];
703 - (void)prevSong:(id)sender
705 [self sendAEWithEventClass:'hook' andEventID:'Prev'];
708 - (void)fastForward:(id)sender
710 [self sendAEWithEventClass:'hook' andEventID:'Fast'];
713 - (void)rewind:(id)sender
715 [self sendAEWithEventClass:'hook' andEventID:'Rwnd'];
718 - (void)quitMenuTunes:(id)sender
720 [NSApp terminate:self];
723 - (void)openiTunes:(id)sender
725 [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
728 - (void)showPreferences:(id)sender
730 if (!prefsController) {
731 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
737 - (void)closePreferences
739 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))) {
742 [prefsController release];
743 prefsController = nil;
748 // Show Current Track Info And Show Upcoming Songs Floaters
752 - (void)showCurrentTrackInfo
754 NSString *trackName = [self runScriptAndReturnResult:@"return name of current track"];
755 if (!statusController && [trackName length]) {
756 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
757 NSString *stringToShow = @"";
759 if ([defaults boolForKey:@"showName"]) {
760 if ([defaults boolForKey:@"showArtist"]) {
761 NSString *trackArtist = [self runScriptAndReturnResult:@"return artist of current track"];
762 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
764 stringToShow = [stringToShow stringByAppendingString:trackName];
765 stringToShow = [stringToShow stringByAppendingString:@"\n"];
768 if ([defaults boolForKey:@"showAlbum"]) {
769 NSString *trackAlbum = [self runScriptAndReturnResult:@"return album of current track"];
770 if ([trackAlbum length]) {
771 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
772 stringToShow = [stringToShow stringByAppendingString:@"\n"];
776 if ([defaults boolForKey:@"showTime"]) {
777 NSString *trackTime = [self runScriptAndReturnResult:@"return time of current track"];
778 if ([trackTime length]) {
779 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
784 int trackTimeLeft = [[self runScriptAndReturnResult:@"return (duration of current track) - player position"] intValue];
785 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
787 stringToShow = [stringToShow stringByAppendingString:
788 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
790 stringToShow = [stringToShow stringByAppendingString:
791 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
795 statusController = [[StatusWindowController alloc] init];
796 [statusController setTrackInfo:stringToShow];
797 [NSTimer scheduledTimerWithTimeInterval:3.0
799 selector:@selector(fadeAndCloseStatusWindow)
805 - (void)showUpcomingSongs
807 if (!statusController) {
808 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
811 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
812 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
814 NSString *songs = @"";
816 statusController = [[StatusWindowController alloc] init];
817 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
819 NSString *curSong = [self runScriptAndReturnResult:
820 [NSString stringWithFormat:@"return name of track %i of current playlist", i]];
821 songs = [songs stringByAppendingString:curSong];
822 songs = [songs stringByAppendingString:@"\n"];
825 [statusController setUpcomingSongs:songs];
826 [NSTimer scheduledTimerWithTimeInterval:3.0
828 selector:@selector(fadeAndCloseStatusWindow)
835 - (void)fadeAndCloseStatusWindow
837 [statusController fadeWindowOut];
838 [statusController release];
839 statusController = nil;
842 /*************************************************************************/
844 #pragma mark NSApplication DELEGATE METHODS
845 /*************************************************************************/
847 - (void)applicationWillTerminate:(NSNotification *)note
850 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
854 /*************************************************************************/
856 #pragma mark DEALLOCATION METHODS
857 /*************************************************************************/
862 [refreshTimer invalidate];
865 CloseComponent(asComponent);
866 [statusItem release];