3 ¥ Radio mode makes things act oddly
4 ¥ Make preferences window pretty
6 - hot keys can't be set when NSBGOnly is on. The window is not key,
7 so the KeyBroadcaster does not pick up key combos
8 - going to need a different way of defining key combos
10 ¥ Apple Events! Apple Events! Apple Events!
11 ¥ Upcoming songs menu items are disabled after launching iTunes and playing
15 #import "MenuTunesView.h"
16 #import "PreferencesController.h"
17 #import "HotKeyCenter.h"
18 #import "StatusWindowController.h"
20 @interface MenuTunes(Private)
21 - (void)registerDefaultsIfNeeded;
23 - (void)rebuildUpcomingSongsMenu;
24 - (void)rebuildPlaylistMenu;
25 - (void)rebuildEQPresetsMenu;
27 - (NSString *)runScriptAndReturnResult:(NSString *)script;
29 - (void)sendAEWithEventClass:(AEEventClass)eventClass andEventID:(AEEventID)eventID;
33 @implementation MenuTunes
35 /*************************************************************************/
37 #pragma mark INITIALIZATION METHODS
38 /*************************************************************************/
40 - (void)applicationDidFinishLaunching:(NSNotification *)note
42 asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
44 [self registerDefaultsIfNeeded];
46 menu = [[NSMenu alloc] initWithTitle:@""];
47 iTunesPSN = [self iTunesPSN]; //Get PSN of iTunes if it's running
49 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
52 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
54 selector:@selector(timerUpdate)
60 menu = [[NSMenu alloc] initWithTitle:@""];
61 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
62 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
63 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
64 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
68 statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
69 [statusItem setImage:[NSImage imageNamed:@"menu.tiff"]];
70 [statusItem setHighlightMode:YES];
71 [statusItem setMenu:menu];
73 view = [[MenuTunesView alloc] initWithFrame:[[statusItem view] frame]];
74 //[statusItem setView:view];
78 /*************************************************************************/
80 #pragma mark INSTANCE METHODS
81 /*************************************************************************/
83 - (void)registerDefaultsIfNeeded
85 if (![[NSUserDefaults standardUserDefaults] objectForKey:@"menu"]) {
86 [[NSUserDefaults standardUserDefaults] setObject:
87 [NSArray arrayWithObjects:
100 @"Current Track Info",
101 nil] forKey:@"menu"];
105 //Recreate the status item menu
108 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
112 didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
114 while ([menu numberOfItems] > 0) {
115 [menu removeItemAtIndex:0];
118 playPauseMenuItem = nil;
119 upcomingSongsItem = nil;
121 [playlistMenu release];
127 for (i = 0; i < [myMenu count]; i++) {
128 NSString *item = [myMenu objectAtIndex:i];
129 if ([item isEqualToString:@"Play/Pause"]) {
130 playPauseMenuItem = [menu addItemWithTitle:@"Play"
131 action:@selector(playPause:)
133 [playPauseMenuItem setTarget:self];
134 } else if ([item isEqualToString:@"Next Track"]) {
135 [[menu addItemWithTitle:@"Next Track"
136 action:@selector(nextSong:)
137 keyEquivalent:@""] setTarget:self];
138 } else if ([item isEqualToString:@"Previous Track"]) {
139 [[menu addItemWithTitle:@"Previous Track"
140 action:@selector(prevSong:)
141 keyEquivalent:@""] setTarget:self];
142 } else if ([item isEqualToString:@"Fast Forward"]) {
143 [[menu addItemWithTitle:@"Fast Forward"
144 action:@selector(fastForward:)
145 keyEquivalent:@""] setTarget:self];
146 } else if ([item isEqualToString:@"Rewind"]) {
147 [[menu addItemWithTitle:@"Rewind"
148 action:@selector(rewind:)
149 keyEquivalent:@""] setTarget:self];
150 } else if ([item isEqualToString:@"Upcoming Songs"]) {
151 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
154 } else if ([item isEqualToString:@"Playlists"]) {
155 playlistItem = [menu addItemWithTitle:@"Playlists"
158 } else if ([item isEqualToString:@"EQ Presets"]) {
159 eqItem = [menu addItemWithTitle:@"EQ Presets"
162 } else if ([item isEqualToString:@"PreferencesÉ"]) {
163 [[menu addItemWithTitle:@"PreferencesÉ"
164 action:@selector(showPreferences:)
165 keyEquivalent:@""] setTarget:self];
166 } else if ([item isEqualToString:@"Quit"]) {
167 [[menu addItemWithTitle:@"Quit"
168 action:@selector(quitMenuTunes:)
169 keyEquivalent:@""] setTarget:self];
170 } else if ([item isEqualToString:@"Current Track Info"]) {
171 trackInfoIndex = [menu numberOfItems];
172 [menu addItemWithTitle:@"No Song"
175 } else if ([item isEqualToString:@"<separator>"]) {
176 [menu addItem:[NSMenuItem separatorItem]];
180 curTrackIndex = -1; //Force update of everything
181 [self timerUpdate]; //Updates dynamic info in the menu
187 //Updates the menu with current player state, song, and upcoming songs
190 NSString *curSongName, *curAlbumName;
191 NSMenuItem *menuItem;
193 if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
197 //Get the current track name and album.
198 curSongName = [self runScriptAndReturnResult:@"return name of current track"];
199 curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
201 if (upcomingSongsItem) {
202 [self rebuildUpcomingSongsMenu];
205 [self rebuildPlaylistMenu];
208 [self rebuildEQPresetsMenu];
211 if ([curSongName length] > 0) {
212 int index = [menu indexOfItemWithTitle:@"Now Playing"];
215 [menu removeItemAtIndex:index + 1];
217 if (didHaveAlbumName) {
218 [menu removeItemAtIndex:index + 1];
222 if ([curAlbumName length] > 0) {
223 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
226 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
230 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
233 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
237 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
238 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
239 [menu insertItem:menuItem atIndex:trackInfoIndex];
243 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
244 [menu removeItemAtIndex:trackInfoIndex];
245 [menu removeItemAtIndex:trackInfoIndex];
247 if (didHaveAlbumName) {
248 [menu removeItemAtIndex:trackInfoIndex];
251 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
252 [menu insertItem:menuItem atIndex:trackInfoIndex];
256 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
259 //Rebuild the upcoming songs submenu. Can be improved a lot.
260 - (void)rebuildUpcomingSongsMenu
262 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
263 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
266 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
269 [upcomingSongsMenu release];
270 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
272 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
274 NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
275 NSMenuItem *songItem;
276 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
277 [songItem setTarget:self];
278 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
279 [upcomingSongsMenu addItem:songItem];
282 [upcomingSongsMenu addItemWithTitle:@"End of playlist." action:nil keyEquivalent:@""];
286 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
287 [upcomingSongsItem setEnabled:YES];
291 - (void)rebuildPlaylistMenu
293 int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
294 int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
296 if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
299 [playlistMenu release];
300 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
302 for (i = 1; i <= numPlaylists; i++) {
303 NSString *playlistName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of playlist %i", i]];
304 NSMenuItem *tempItem;
305 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
306 [tempItem setTarget:self];
307 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
308 [playlistMenu addItem:tempItem];
311 [playlistItem setSubmenu:playlistMenu];
314 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
318 //Build a menu with the list of all available EQ presets
319 - (void)rebuildEQPresetsMenu
321 int numSets = [[self runScriptAndReturnResult:@"return number of EQ presets"] intValue];
324 if (eqMenu && (numSets == [eqMenu numberOfItems]))
328 eqMenu = [[NSMenu alloc] initWithTitle:@""];
330 for (i = 1; i <= numSets; i++) {
331 NSString *setName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of EQ preset %i", i]];
332 NSMenuItem *tempItem;
333 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
334 [tempItem setTarget:self];
335 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
336 [eqMenu addItem:tempItem];
339 [eqItem setSubmenu:eqMenu];
341 [[eqMenu itemAtIndex:[[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue] - 1] setState:NSOnState];
346 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
347 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
348 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
349 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
350 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
355 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
357 if ([defaults objectForKey:@"PlayPause"] != nil) {
358 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
359 combo:[defaults keyComboForKey:@"PlayPause"]
360 target:self action:@selector(playPause:)];
363 if ([defaults objectForKey:@"NextTrack"] != nil) {
364 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
365 combo:[defaults keyComboForKey:@"NextTrack"]
366 target:self action:@selector(nextSong:)];
369 if ([defaults objectForKey:@"PrevTrack"] != nil) {
370 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
371 combo:[defaults keyComboForKey:@"PrevTrack"]
372 target:self action:@selector(prevSong:)];
375 if ([defaults objectForKey:@"TrackInfo"] != nil) {
376 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
377 combo:[defaults keyComboForKey:@"TrackInfo"]
378 target:self action:@selector(showCurrentTrackInfo)];
381 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
382 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
383 combo:[defaults keyComboForKey:@"UpcomingSongs"]
384 target:self action:@selector(showUpcomingSongs)];
388 //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.
389 - (NSString *)runScriptAndReturnResult:(NSString *)script
391 AEDesc scriptDesc, resultDesc;
396 script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
398 AECreateDesc(typeChar, [script cString], [script cStringLength],
401 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
403 length = AEGetDescDataSize(&resultDesc);
404 buffer = malloc(length);
406 AEGetDescData(&resultDesc, buffer, length);
407 AEDisposeDesc(&scriptDesc);
408 AEDisposeDesc(&resultDesc);
409 result = [NSString stringWithCString:buffer length:length];
410 if ( (! [result isEqualToString:@""]) &&
411 ([result characterAtIndex:0] == '\"') &&
412 ([result characterAtIndex:[result length] - 1] == '\"') ) {
413 result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
420 //Called when the timer fires.
425 if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
426 int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
428 if (trackPlayingIndex != curTrackIndex) {
430 curTrackIndex = trackPlayingIndex;
433 //Update Play/Pause menu item
434 if (playPauseMenuItem){
435 if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
436 [playPauseMenuItem setTitle:@"Pause"];
438 [playPauseMenuItem setTitle:@"Play"];
443 menu = [[NSMenu alloc] initWithTitle:@""];
444 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
445 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
446 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
447 [statusItem setMenu:menu];
449 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
450 [refreshTimer invalidate];
456 - (void)iTunesLaunched:(NSNotification *)note
458 NSDictionary *info = [note userInfo];
460 iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
461 iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
464 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
466 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
467 [statusItem setMenu:menu]; //Set the menu back to the main one
468 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
471 //Return the PSN of iTunes, if it's running
472 - (ProcessSerialNumber)iTunesPSN
474 ProcessSerialNumber procNum;
475 procNum.highLongOfPSN = kNoProcess;
476 procNum.lowLongOfPSN = 0;
478 while ( (GetNextProcess(&procNum) == noErr) ) {
479 CFStringRef procName;
480 if ( (CopyProcessName(&procNum, &procName) == noErr) ) {
481 if ([(NSString *)procName isEqualToString:@"iTunes"]) {
484 [(NSString *)procName release];
490 //Send an AppleEvent with a given event ID
491 - (void)sendAEWithEventClass:(AEEventClass)eventClass
492 andEventID:(AEEventID)eventID
494 OSType iTunesType = 'hook';
495 AppleEvent event, reply;
497 AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
499 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
500 AEDisposeDesc(&event);
501 AEDisposeDesc(&reply);
505 // Selectors - called from status item menu
508 - (void)playTrack:(id)sender
510 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play track %i of current playlist", [[sender representedObject] intValue]]];
514 - (void)selectPlaylist:(id)sender
516 int playlist = [[sender representedObject] intValue];
517 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
518 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
522 - (void)selectEQPreset:(id)sender
524 int curSet = [[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue];
525 int item = [[sender representedObject] intValue];
526 [self runScriptAndReturnResult:[NSString stringWithFormat:@"set current EQ preset to EQ preset %i", item]];
527 [self runScriptAndReturnResult:@"set EQ enabled to 1"];
528 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
529 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
532 - (void)playPause:(id)sender
534 NSString *state = [self runScriptAndReturnResult:@"return player state"];
535 if ([state isEqualToString:@"playing"]) {
536 [self sendAEWithEventClass:'hook' andEventID:'Paus'];
537 [playPauseMenuItem setTitle:@"Play"];
538 } else if ([state isEqualToString:@"fast forwarding"] || [state
539 isEqualToString:@"rewinding"]) {
540 [self sendAEWithEventClass:'hook' andEventID:'Paus'];
541 [self sendAEWithEventClass:'hook' andEventID:'Play'];
543 [self sendAEWithEventClass:'hook' andEventID:'Play'];
544 [playPauseMenuItem setTitle:@"Pause"];
548 - (void)nextSong:(id)sender
550 [self sendAEWithEventClass:'hook' andEventID:'Next'];
553 - (void)prevSong:(id)sender
555 [self sendAEWithEventClass:'hook' andEventID:'Prev'];
558 - (void)fastForward:(id)sender
560 [self sendAEWithEventClass:'hook' andEventID:'Fast'];
563 - (void)rewind:(id)sender
565 [self sendAEWithEventClass:'hook' andEventID:'Rwnd'];
568 - (void)quitMenuTunes:(id)sender
570 [NSApp terminate:self];
573 - (void)openiTunes:(id)sender
575 [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
578 - (void)showPreferences:(id)sender
580 if (!prefsController) {
581 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
587 - (void)closePreferences
589 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))) {
592 [prefsController release];
593 prefsController = nil;
598 // Show Current Track Info And Show Upcoming Songs Floaters
602 - (void)showCurrentTrackInfo
604 NSString *trackName = [self runScriptAndReturnResult:@"return name of current track"];
605 if (!statusController && [trackName length]) {
606 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
607 NSString *stringToShow = @"";
610 if ([defaults boolForKey:@"showName"]) {
611 if ([defaults boolForKey:@"showArtist"]) {
612 NSString *trackArtist = [self runScriptAndReturnResult:@"return artist of current track"];
613 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
615 stringToShow = [stringToShow stringByAppendingString:trackName];
616 stringToShow = [stringToShow stringByAppendingString:@"\n"];
617 if ([trackName length] > 38) {
623 if ([defaults boolForKey:@"showAlbum"]) {
624 NSString *trackAlbum = [self runScriptAndReturnResult:@"return album of current track"];
625 if ([trackAlbum length]) {
626 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
627 stringToShow = [stringToShow stringByAppendingString:@"\n"];
632 if ([defaults boolForKey:@"showTime"]) {
633 NSString *trackTime = [self runScriptAndReturnResult:@"return time of current track"];
634 if ([trackTime length]) {
635 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
641 int trackTimeLeft = [[self runScriptAndReturnResult:@"return (duration of current track) - player position"] intValue];
642 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
644 stringToShow = [stringToShow stringByAppendingString:
645 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
647 stringToShow = [stringToShow stringByAppendingString:
648 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
652 statusController = [[StatusWindowController alloc] init];
653 [statusController setTrackInfo:stringToShow lines:lines];
654 [NSTimer scheduledTimerWithTimeInterval:3.0
656 selector:@selector(fadeAndCloseStatusWindow)
662 - (void)showUpcomingSongs
664 if (!statusController) {
665 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
668 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
669 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
671 NSString *songs = @"";
673 statusController = [[StatusWindowController alloc] init];
674 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
676 NSString *curSong = [self runScriptAndReturnResult:
677 [NSString stringWithFormat:@"return name of track %i of current playlist", i]];
678 songs = [songs stringByAppendingString:curSong];
679 songs = [songs stringByAppendingString:@"\n"];
682 [statusController setUpcomingSongs:songs numSongs:numSongsInAdvance];
683 [NSTimer scheduledTimerWithTimeInterval:3.0
685 selector:@selector(fadeAndCloseStatusWindow)
692 - (void)fadeAndCloseStatusWindow
694 [statusController fadeWindowOut];
695 [statusController release];
696 statusController = nil;
699 /*************************************************************************/
701 #pragma mark NSApplication DELEGATE METHODS
702 /*************************************************************************/
704 - (void)applicationWillTerminate:(NSNotification *)note
707 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
711 /*************************************************************************/
713 #pragma mark DEALLOCATION METHODS
714 /*************************************************************************/
719 [refreshTimer invalidate];
722 CloseComponent(asComponent);
723 [statusItem release];