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;
24 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
25 onItem:(NSMenuItem *)item;
29 @implementation MenuTunes
31 /*************************************************************************/
33 #pragma mark INITIALIZATION METHODS
34 /*************************************************************************/
36 - (void)applicationDidFinishLaunching:(NSNotification *)note
38 asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
40 [self registerDefaultsIfNeeded];
42 menu = [[NSMenu alloc] initWithTitle:@""];
43 iTunesPSN = [self iTunesPSN]; //Get PSN of iTunes if it's running
45 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
48 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
50 selector:@selector(timerUpdate)
56 menu = [[NSMenu alloc] initWithTitle:@""];
57 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
58 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
59 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
60 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
64 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
65 withLength:NSSquareStatusItemLength];
67 [statusItem setImage:[NSImage imageNamed:@"menu"]];
68 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
69 [statusItem setMenu:menu];
70 // Below line of code is for creating builds for Beta Testers
71 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
75 /*************************************************************************/
77 #pragma mark INSTANCE METHODS
78 /*************************************************************************/
80 - (void)registerDefaultsIfNeeded
82 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
83 if (![defaults objectForKey:@"menu"]) {
85 NSMutableDictionary *loginwindow;
86 NSMutableArray *loginarray;
90 [NSArray arrayWithObjects:
103 @"Current Track Info",
104 nil] forKey:@"menu"];
106 [defaults synchronize];
107 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
108 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
110 for (i = 0; i < [loginarray count]; i++) {
111 NSDictionary *tempDict = [loginarray objectAtIndex:i];
112 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
118 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
119 AEDesc scriptDesc, resultDesc;
120 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]];
122 AECreateDesc(typeChar, [script cString], [script cStringLength],
125 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
127 AEDisposeDesc(&scriptDesc);
128 AEDisposeDesc(&resultDesc);
133 if (![defaults integerForKey:@"SongsInAdvance"])
135 [defaults setInteger:5 forKey:@"SongsInAdvance"];
138 if (![defaults objectForKey:@"showName"]) {
139 [defaults setBool:YES forKey:@"showName"];
142 if (![defaults objectForKey:@"showArtist"]) {
143 [defaults setBool:YES forKey:@"showArtist"];
146 if (![defaults objectForKey:@"showAlbum"]) {
147 [defaults setBool:NO forKey:@"showAlbum"];
150 if (![defaults objectForKey:@"showTime"]) {
151 [defaults setBool:NO forKey:@"showTime"];
155 //Recreate the status item menu
158 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
162 didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
163 didHaveArtistName = ([[self runScriptAndReturnResult:@"return artist of current track"] length] > 0);
166 while ([menu numberOfItems] > 0) {
167 [menu removeItemAtIndex:0];
170 playPauseMenuItem = nil;
171 upcomingSongsItem = nil;
173 [playlistMenu release];
179 for (i = 0; i < [myMenu count]; i++) {
180 NSString *item = [myMenu objectAtIndex:i];
181 if ([item isEqualToString:@"Play/Pause"]) {
182 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
183 playPauseMenuItem = [menu addItemWithTitle:@"Play"
184 action:@selector(playPause:)
186 [playPauseMenuItem setTarget:self];
190 [self setKeyEquivalentForCode:[tempCombo keyCode]
191 andModifiers:[tempCombo modifiers] onItem:playPauseMenuItem];
194 } else if ([item isEqualToString:@"Next Track"]) {
195 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
196 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
197 action:@selector(nextSong:)
200 [nextTrack setTarget:self];
203 [self setKeyEquivalentForCode:[tempCombo keyCode]
204 andModifiers:[tempCombo modifiers] onItem:nextTrack];
207 } else if ([item isEqualToString:@"Previous Track"]) {
208 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
209 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
210 action:@selector(prevSong:)
213 [prevTrack setTarget:self];
216 [self setKeyEquivalentForCode:[tempCombo keyCode]
217 andModifiers:[tempCombo modifiers] onItem:prevTrack];
220 } else if ([item isEqualToString:@"Fast Forward"]) {
221 [[menu addItemWithTitle:@"Fast Forward"
222 action:@selector(fastForward:)
223 keyEquivalent:@""] setTarget:self];
224 } else if ([item isEqualToString:@"Rewind"]) {
225 [[menu addItemWithTitle:@"Rewind"
226 action:@selector(rewind:)
227 keyEquivalent:@""] setTarget:self];
228 } else if ([item isEqualToString:@"Upcoming Songs"]) {
229 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
232 } else if ([item isEqualToString:@"Playlists"]) {
233 playlistItem = [menu addItemWithTitle:@"Playlists"
236 } else if ([item isEqualToString:@"EQ Presets"]) {
237 eqItem = [menu addItemWithTitle:@"EQ Presets"
240 } else if ([item isEqualToString:@"PreferencesÉ"]) {
241 [[menu addItemWithTitle:@"PreferencesÉ"
242 action:@selector(showPreferences:)
243 keyEquivalent:@""] setTarget:self];
244 } else if ([item isEqualToString:@"Quit"]) {
245 [[menu addItemWithTitle:@"Quit"
246 action:@selector(quitMenuTunes:)
247 keyEquivalent:@""] setTarget:self];
248 } else if ([item isEqualToString:@"Current Track Info"]) {
249 trackInfoIndex = [menu numberOfItems];
250 [menu addItemWithTitle:@"No Song"
253 } else if ([item isEqualToString:@"<separator>"]) {
254 [menu addItem:[NSMenuItem separatorItem]];
258 curTrackIndex = -1; //Force update of everything
259 [self timerUpdate]; //Updates dynamic info in the menu
265 //Updates the menu with current player state, song, and upcoming songs
268 NSMenuItem *menuItem;
269 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
271 if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
275 if (upcomingSongsItem) {
276 [self rebuildUpcomingSongsMenu];
280 [self rebuildPlaylistMenu];
284 [self rebuildEQPresetsMenu];
287 if (trackInfoIndex > -1)
289 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
290 curSongName = [self runScriptAndReturnResult:@"return name of current track"];
292 if ([defaults boolForKey:@"showAlbum"]) {
293 curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
296 if ([defaults boolForKey:@"showArtist"]) {
297 curArtistName = [self runScriptAndReturnResult:@"return artist of current track"];
300 if ([curSongName length] > 0) {
301 int index = [menu indexOfItemWithTitle:@"Now Playing"];
303 if ([defaults boolForKey:@"showName"]) {
304 [menu removeItemAtIndex:index + 1];
306 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
307 [menu removeItemAtIndex:index + 1];
309 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
310 [menu removeItemAtIndex:index + 1];
312 if ([defaults boolForKey:@"showTime"]) {
313 [menu removeItemAtIndex:index + 1];
317 if (!isPlayingRadio) {
318 if ([defaults boolForKey:@"showTime"]) {
319 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [self runScriptAndReturnResult:@"return time of current track"]]
322 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
326 if ([curArtistName length] > 0) {
327 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
330 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
334 if ([curAlbumName length] > 0) {
335 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
338 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
343 if ([defaults boolForKey:@"showName"]) {
344 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
347 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
352 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
353 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
354 [menu insertItem:menuItem atIndex:trackInfoIndex];
357 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
358 [menu removeItemAtIndex:trackInfoIndex];
360 if ([defaults boolForKey:@"showName"] == YES) {
361 [menu removeItemAtIndex:trackInfoIndex];
364 if ([defaults boolForKey:@"showTime"] == YES) {
365 [menu removeItemAtIndex:trackInfoIndex];
368 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
369 [menu removeItemAtIndex:trackInfoIndex];
372 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
373 [menu removeItemAtIndex:trackInfoIndex];
376 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
377 [menu insertItem:menuItem atIndex:trackInfoIndex];
381 if ([defaults boolForKey:@"showArtist"]) {
382 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
385 if ([defaults boolForKey:@"showAlbum"]) {
386 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
391 //Rebuild the upcoming songs submenu. Can be improved a lot.
392 - (void)rebuildUpcomingSongsMenu
394 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
395 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
396 if (!isPlayingRadio) {
398 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
401 [upcomingSongsMenu release];
402 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
403 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
404 [upcomingSongsItem setEnabled:YES];
406 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
408 NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
409 NSMenuItem *songItem;
410 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
411 [songItem setTarget:self];
412 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
413 [upcomingSongsMenu addItem:songItem];
421 [upcomingSongsItem setSubmenu:nil];
422 [upcomingSongsItem setEnabled:NO];
426 - (void)rebuildPlaylistMenu
428 int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
429 int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
436 if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
439 [playlistMenu release];
440 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
442 for (i = 1; i <= numPlaylists; i++) {
443 NSString *playlistName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of playlist %i", i]];
444 NSMenuItem *tempItem;
445 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
446 [tempItem setTarget:self];
447 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
448 [playlistMenu addItem:tempItem];
451 [playlistItem setSubmenu:playlistMenu];
454 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
458 //Build a menu with the list of all available EQ presets
459 - (void)rebuildEQPresetsMenu
461 int numSets = [[self runScriptAndReturnResult:@"return number of EQ presets"] intValue];
464 if (eqMenu && (numSets == [eqMenu numberOfItems]))
468 eqMenu = [[NSMenu alloc] initWithTitle:@""];
470 for (i = 1; i <= numSets; i++) {
471 NSString *setName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of EQ preset %i", i]];
472 NSMenuItem *tempItem;
473 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
474 [tempItem setTarget:self];
475 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
476 [eqMenu addItem:tempItem];
479 [eqItem setSubmenu:eqMenu];
481 [[eqMenu itemAtIndex:[[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue] - 1] setState:NSOnState];
486 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
487 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
488 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
489 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
490 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
495 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
497 if ([defaults objectForKey:@"PlayPause"] != nil) {
498 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
499 combo:[defaults keyComboForKey:@"PlayPause"]
500 target:self action:@selector(playPause:)];
503 if ([defaults objectForKey:@"NextTrack"] != nil) {
504 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
505 combo:[defaults keyComboForKey:@"NextTrack"]
506 target:self action:@selector(nextSong:)];
509 if ([defaults objectForKey:@"PrevTrack"] != nil) {
510 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
511 combo:[defaults keyComboForKey:@"PrevTrack"]
512 target:self action:@selector(prevSong:)];
515 if ([defaults objectForKey:@"TrackInfo"] != nil) {
516 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
517 combo:[defaults keyComboForKey:@"TrackInfo"]
518 target:self action:@selector(showCurrentTrackInfo)];
521 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
522 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
523 combo:[defaults keyComboForKey:@"UpcomingSongs"]
524 target:self action:@selector(showUpcomingSongs)];
528 //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.
529 - (NSString *)runScriptAndReturnResult:(NSString *)script
531 AEDesc scriptDesc, resultDesc;
536 script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
538 AECreateDesc(typeChar, [script cString], [script cStringLength],
541 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
543 length = AEGetDescDataSize(&resultDesc);
544 buffer = malloc(length);
546 AEGetDescData(&resultDesc, buffer, length);
547 AEDisposeDesc(&scriptDesc);
548 AEDisposeDesc(&resultDesc);
549 result = [NSString stringWithCString:buffer length:length];
550 if ( (! [result isEqualToString:@""]) &&
551 ([result characterAtIndex:0] == '\"') &&
552 ([result characterAtIndex:[result length] - 1] == '\"') ) {
553 result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
560 //Called when the timer fires.
564 if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
565 int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
567 if (trackPlayingIndex != curTrackIndex) {
568 bool wasPlayingRadio = isPlayingRadio;
569 isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
570 if (isPlayingRadio && !wasPlayingRadio) {
572 for (i = 0; i < [playlistMenu numberOfItems]; i++)
574 [[playlistMenu itemAtIndex:i] setState:NSOffState];
577 if (wasPlayingRadio) {
578 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
579 [menu insertItem:temp atIndex:trackInfoIndex + 1];
583 curTrackIndex = trackPlayingIndex;
587 int playlist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
588 if (playlist != curPlaylistIndex) {
589 bool wasPlayingRadio = isPlayingRadio;
590 isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
591 if (isPlayingRadio && !wasPlayingRadio) {
593 for (i = 0; i < [playlistMenu numberOfItems]; i++)
595 [[playlistMenu itemAtIndex:i] setState:NSOffState];
598 if (wasPlayingRadio) {
599 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
600 [menu insertItem:temp atIndex:trackInfoIndex + 1];
604 curTrackIndex = trackPlayingIndex;
605 curPlaylistIndex = playlist;
608 //Update Play/Pause menu item
609 if (playPauseMenuItem){
610 if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
611 [playPauseMenuItem setTitle:@"Pause"];
613 [playPauseMenuItem setTitle:@"Play"];
618 menu = [[NSMenu alloc] initWithTitle:@""];
619 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
620 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
621 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
622 [statusItem setMenu:menu];
624 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
625 [refreshTimer invalidate];
631 - (void)iTunesLaunched:(NSNotification *)note
633 NSDictionary *info = [note userInfo];
635 iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
636 iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
639 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
641 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
642 [statusItem setMenu:menu]; //Set the menu back to the main one
643 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
646 //Return the PSN of iTunes, if it's running
647 - (ProcessSerialNumber)iTunesPSN
649 ProcessSerialNumber procNum;
650 procNum.highLongOfPSN = kNoProcess;
651 procNum.lowLongOfPSN = 0;
653 while ( (GetNextProcess(&procNum) == noErr) ) {
654 CFStringRef procName;
655 if ( (CopyProcessName(&procNum, &procName) == noErr) ) {
656 if ([(NSString *)procName isEqualToString:@"iTunes"]) {
665 //Send an AppleEvent with a given event ID
666 - (void)sendAEWithEventClass:(AEEventClass)eventClass
667 andEventID:(AEEventID)eventID
669 OSType iTunesType = 'hook';
670 AppleEvent event, reply;
672 AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
674 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
675 AEDisposeDesc(&event);
676 AEDisposeDesc(&reply);
680 // Selectors - called from status item menu
683 - (void)playTrack:(id)sender
685 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play track %i of current playlist", [[sender representedObject] intValue]]];
689 - (void)selectPlaylist:(id)sender
691 int playlist = [[sender representedObject] intValue];
692 if (!isPlayingRadio) {
693 int curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
694 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
696 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
697 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
700 - (void)selectEQPreset:(id)sender
702 int curSet = [[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue];
703 int item = [[sender representedObject] intValue];
704 [self runScriptAndReturnResult:[NSString stringWithFormat:@"set current EQ preset to EQ preset %i", item]];
705 [self runScriptAndReturnResult:@"set EQ enabled to 1"];
706 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
707 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
710 - (void)playPause:(id)sender
712 NSString *state = [self runScriptAndReturnResult:@"return player state"];
713 if ([state isEqualToString:@"playing"]) {
714 [self sendAEWithEventClass:'hook' andEventID:'Paus'];
715 [playPauseMenuItem setTitle:@"Play"];
716 } else if ([state isEqualToString:@"fast forwarding"] || [state
717 isEqualToString:@"rewinding"]) {
718 [self sendAEWithEventClass:'hook' andEventID:'Paus'];
719 [self sendAEWithEventClass:'hook' andEventID:'Play'];
721 [self sendAEWithEventClass:'hook' andEventID:'Play'];
722 [playPauseMenuItem setTitle:@"Pause"];
726 - (void)nextSong:(id)sender
728 [self sendAEWithEventClass:'hook' andEventID:'Next'];
731 - (void)prevSong:(id)sender
733 [self sendAEWithEventClass:'hook' andEventID:'Prev'];
736 - (void)fastForward:(id)sender
738 [self sendAEWithEventClass:'hook' andEventID:'Fast'];
741 - (void)rewind:(id)sender
743 [self sendAEWithEventClass:'hook' andEventID:'Rwnd'];
746 - (void)quitMenuTunes:(id)sender
748 [NSApp terminate:self];
751 - (void)openiTunes:(id)sender
753 [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
756 - (void)showPreferences:(id)sender
758 if (!prefsController) {
759 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
765 - (void)closePreferences
767 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))) {
770 [prefsController release];
771 prefsController = nil;
776 // Show Current Track Info And Show Upcoming Songs Floaters
780 - (void)showCurrentTrackInfo
782 NSString *trackName = [self runScriptAndReturnResult:@"return name of current track"];
783 if (!statusController && [trackName length]) {
784 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
785 NSString *stringToShow = @"";
787 if ([defaults boolForKey:@"showName"]) {
788 if ([defaults boolForKey:@"showArtist"]) {
789 NSString *trackArtist = [self runScriptAndReturnResult:@"return artist of current track"];
790 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
792 stringToShow = [stringToShow stringByAppendingString:trackName];
793 stringToShow = [stringToShow stringByAppendingString:@"\n"];
796 if ([defaults boolForKey:@"showAlbum"]) {
797 NSString *trackAlbum = [self runScriptAndReturnResult:@"return album of current track"];
798 if ([trackAlbum length]) {
799 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
800 stringToShow = [stringToShow stringByAppendingString:@"\n"];
804 if ([defaults boolForKey:@"showTime"]) {
805 NSString *trackTime = [self runScriptAndReturnResult:@"return time of current track"];
806 if ([trackTime length]) {
807 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
812 int trackTimeLeft = [[self runScriptAndReturnResult:@"return (duration of current track) - player position"] intValue];
813 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
815 stringToShow = [stringToShow stringByAppendingString:
816 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
818 stringToShow = [stringToShow stringByAppendingString:
819 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
823 statusController = [[StatusWindowController alloc] init];
824 [statusController setTrackInfo:stringToShow];
825 [NSTimer scheduledTimerWithTimeInterval:3.0
827 selector:@selector(fadeAndCloseStatusWindow)
833 - (void)showUpcomingSongs
835 if (!statusController) {
836 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
839 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
840 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
842 NSString *songs = @"";
844 statusController = [[StatusWindowController alloc] init];
845 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
847 NSString *curSong = [self runScriptAndReturnResult:
848 [NSString stringWithFormat:@"return name of track %i of current playlist", i]];
849 songs = [songs stringByAppendingString:curSong];
850 songs = [songs stringByAppendingString:@"\n"];
853 [statusController setUpcomingSongs:songs];
854 [NSTimer scheduledTimerWithTimeInterval:3.0
856 selector:@selector(fadeAndCloseStatusWindow)
863 - (void)fadeAndCloseStatusWindow
865 [statusController fadeWindowOut];
866 [statusController release];
867 statusController = nil;
870 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
871 onItem:(NSMenuItem *)item
873 unichar charcode = 'a';
875 long cocoaModifiers = 0;
876 static long carbonToCocoa[6][2] =
878 { cmdKey, NSCommandKeyMask },
879 { optionKey, NSAlternateKeyMask },
880 { controlKey, NSControlKeyMask },
881 { shiftKey, NSShiftKeyMask },
884 for (i = 0; i < 6; i++)
886 if (modifiers & carbonToCocoa[i][0])
888 cocoaModifiers += carbonToCocoa[i][1];
891 [item setKeyEquivalentModifierMask:cocoaModifiers];
893 //Missing key combos for some keys. Must find them later.
906 charcode = NSDeleteFunctionKey;
918 charcode = NSF5FunctionKey;
922 charcode = NSF6FunctionKey;
926 charcode = NSF7FunctionKey;
930 charcode = NSF3FunctionKey;
934 charcode = NSF8FunctionKey;
938 charcode = NSF9FunctionKey;
942 charcode = NSF11FunctionKey;
946 charcode = NSF3FunctionKey;
950 charcode = NSF14FunctionKey;
954 charcode = NSF10FunctionKey;
958 charcode = NSF12FunctionKey;
962 charcode = NSF13FunctionKey;
966 charcode = NSInsertFunctionKey;
973 charcode = NSPageUpFunctionKey;
977 charcode = NSDeleteFunctionKey;
981 charcode = NSF4FunctionKey;
985 charcode = NSEndFunctionKey;
989 charcode = NSF2FunctionKey;
993 charcode = NSPageDownFunctionKey;
997 charcode = NSF1FunctionKey;
1001 charcode = NSLeftArrowFunctionKey;
1005 charcode = NSRightArrowFunctionKey;
1009 charcode = NSDownArrowFunctionKey;
1013 charcode = NSUpArrowFunctionKey;
1017 if (charcode == 'a') {
1018 unsigned long state;
1023 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1024 keyTrans = KeyTranslate(kchr, code, &state);
1025 charCode = keyTrans;
1026 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1028 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1032 /*************************************************************************/
1034 #pragma mark NSApplication DELEGATE METHODS
1035 /*************************************************************************/
1037 - (void)applicationWillTerminate:(NSNotification *)note
1039 [self clearHotKeys];
1040 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1044 /*************************************************************************/
1046 #pragma mark DEALLOCATION METHODS
1047 /*************************************************************************/
1052 [refreshTimer invalidate];
1055 CloseComponent(asComponent);
1056 [statusItem release];