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)
16 - (ITMTRemote *)loadRemote;
18 - (void)rebuildUpcomingSongsMenu;
19 - (void)rebuildPlaylistMenu;
20 - (void)rebuildEQPresetsMenu;
22 - (NSString *)runScriptAndReturnResult:(NSString *)script;
24 - (void)sendAEWithEventClass:(AEEventClass)eventClass andEventID:(AEEventID)eventID;
25 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
26 onItem:(NSMenuItem *)item;
30 @implementation MenuTunes
32 /*************************************************************************/
34 #pragma mark INITIALIZATION METHODS
35 /*************************************************************************/
39 if ( ( self = [super init] ) ) {
40 remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
45 - (void)applicationDidFinishLaunching:(NSNotification *)note
47 currentRemote = [self loadRemote];
48 [currentRemote begin];
50 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iTunesTerminated:) name:@"ITMTRemoteAppDidTerminateNotification" object:nil];
51 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iTunesLaunched:) name:@"ITMTRemoteAppDidLaunchNotification" object:nil];
53 asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
55 [self registerDefaultsIfNeeded];
57 menu = [[NSMenu alloc] initWithTitle:@""];
58 iTunesPSN = [self iTunesPSN]; //Get PSN of iTunes if it's running
60 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
63 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
65 selector:@selector(timerUpdate)
71 menu = [[NSMenu alloc] initWithTitle:@""];
72 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
73 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
74 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
78 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
79 withLength:NSSquareStatusItemLength];
81 [statusItem setImage:[NSImage imageNamed:@"menu"]];
82 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
83 [statusItem setMenu:menu];
84 // Below line of code is for creating builds for Beta Testers
85 // [statusItem setToolTip:@[NSString stringWithFormat:@"This Nontransferable Beta (Built on %s) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com).",__DATE__]];
88 - (ITMTRemote *)loadRemote
90 NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
93 NSArray *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
94 NSEnumerator *enumerator = [bundlePathList objectEnumerator];
97 while ( (bundlePath = [enumerator nextObject]) ) {
98 NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
101 Class remoteClass = [remoteBundle principalClass];
103 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
104 [remoteClass isKindOfClass:[NSObject class]]) {
106 id remote = [remoteClass remote];
107 [remoteArray addObject:remote];
112 // if ( [remoteArray count] > 0 ) {
113 // if ( [remoteArray count] > 1 ) {
114 // [remoteArray sortUsingSelector:@selector(sortAlpha:)];
116 // [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
119 NSLog(@"%@", [remoteArray objectAtIndex:0]);
120 return [remoteArray objectAtIndex:0];
124 /*************************************************************************/
126 #pragma mark INSTANCE METHODS
127 /*************************************************************************/
129 - (void)registerDefaultsIfNeeded
131 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
132 if (![defaults objectForKey:@"menu"]) {
134 NSMutableDictionary *loginwindow;
135 NSMutableArray *loginarray;
139 [NSArray arrayWithObjects:
152 @"Current Track Info",
153 nil] forKey:@"menu"];
155 [defaults synchronize];
156 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
157 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
159 for (i = 0; i < [loginarray count]; i++) {
160 NSDictionary *tempDict = [loginarray objectAtIndex:i];
161 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
167 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
168 AEDesc scriptDesc, resultDesc;
169 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]];
171 AECreateDesc(typeChar, [script cString], [script cStringLength],
174 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
176 AEDisposeDesc(&scriptDesc);
177 AEDisposeDesc(&resultDesc);
182 if (![defaults integerForKey:@"SongsInAdvance"])
184 [defaults setInteger:5 forKey:@"SongsInAdvance"];
187 if (![defaults objectForKey:@"showName"]) {
188 [defaults setBool:YES forKey:@"showName"];
191 if (![defaults objectForKey:@"showArtist"]) {
192 [defaults setBool:YES forKey:@"showArtist"];
195 if (![defaults objectForKey:@"showAlbum"]) {
196 [defaults setBool:NO forKey:@"showAlbum"];
199 if (![defaults objectForKey:@"showTime"]) {
200 [defaults setBool:NO forKey:@"showTime"];
204 //Recreate the status item menu
207 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
212 didHaveAlbumName = ([[currentRemote currentSongAlbum] length] > 0);
213 didHaveArtistName = ([[currentRemote currentSongArtist] length] > 0);
215 while ([menu numberOfItems] > 0) {
216 [menu removeItemAtIndex:0];
219 playPauseMenuItem = nil;
220 upcomingSongsItem = nil;
222 [playlistMenu release];
228 for (i = 0; i < [myMenu count]; i++) {
229 NSString *item = [myMenu objectAtIndex:i];
230 if ([item isEqualToString:@"Play/Pause"]) {
231 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PlayPause"];
232 playPauseMenuItem = [menu addItemWithTitle:@"Play"
233 action:@selector(playPause:)
235 [playPauseMenuItem setTarget:self];
239 [self setKeyEquivalentForCode:[tempCombo keyCode]
240 andModifiers:[tempCombo modifiers] onItem:playPauseMenuItem];
243 } else if ([item isEqualToString:@"Next Track"]) {
244 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"NextTrack"];
245 NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
246 action:@selector(nextSong:)
249 [nextTrack setTarget:self];
252 [self setKeyEquivalentForCode:[tempCombo keyCode]
253 andModifiers:[tempCombo modifiers] onItem:nextTrack];
256 } else if ([item isEqualToString:@"Previous Track"]) {
257 KeyCombo *tempCombo = [[NSUserDefaults standardUserDefaults] keyComboForKey:@"PrevTrack"];
258 NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
259 action:@selector(prevSong:)
262 [prevTrack setTarget:self];
265 [self setKeyEquivalentForCode:[tempCombo keyCode]
266 andModifiers:[tempCombo modifiers] onItem:prevTrack];
269 } else if ([item isEqualToString:@"Fast Forward"]) {
270 [[menu addItemWithTitle:@"Fast Forward"
271 action:@selector(fastForward:)
272 keyEquivalent:@""] setTarget:self];
273 } else if ([item isEqualToString:@"Rewind"]) {
274 [[menu addItemWithTitle:@"Rewind"
275 action:@selector(rewind:)
276 keyEquivalent:@""] setTarget:self];
277 } else if ([item isEqualToString:@"Upcoming Songs"]) {
278 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
281 } else if ([item isEqualToString:@"Playlists"]) {
282 playlistItem = [menu addItemWithTitle:@"Playlists"
285 } else if ([item isEqualToString:@"EQ Presets"]) {
286 eqItem = [menu addItemWithTitle:@"EQ Presets"
289 } else if ([item isEqualToString:@"PreferencesÉ"]) {
290 [[menu addItemWithTitle:@"PreferencesÉ"
291 action:@selector(showPreferences:)
292 keyEquivalent:@""] setTarget:self];
293 } else if ([item isEqualToString:@"Quit"]) {
294 [[menu addItemWithTitle:@"Quit"
295 action:@selector(quitMenuTunes:)
296 keyEquivalent:@""] setTarget:self];
297 } else if ([item isEqualToString:@"Current Track Info"]) {
298 trackInfoIndex = [menu numberOfItems];
299 [menu addItemWithTitle:@"No Song"
302 } else if ([item isEqualToString:@"<separator>"]) {
303 [menu addItem:[NSMenuItem separatorItem]];
307 [self timerUpdate]; //Updates dynamic info in the menu
313 //Updates the menu with current player state, song, and upcoming songs
316 NSMenuItem *menuItem;
317 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
319 if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
323 if (upcomingSongsItem) {
324 [self rebuildUpcomingSongsMenu];
328 [self rebuildPlaylistMenu];
332 [self rebuildEQPresetsMenu];
335 if (trackInfoIndex > -1)
337 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
338 curSongName = [currentRemote currentSongTitle];
340 if ([defaults boolForKey:@"showAlbum"]) {
341 curAlbumName = [currentRemote currentSongAlbum];
344 if ([defaults boolForKey:@"showArtist"]) {
345 curArtistName = [currentRemote currentSongArtist];
348 if ([curSongName length] > 0) {
349 int index = [menu indexOfItemWithTitle:@"Now Playing"];
351 if ([defaults boolForKey:@"showName"]) {
352 [menu removeItemAtIndex:index + 1];
354 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
355 [menu removeItemAtIndex:index + 1];
357 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
358 [menu removeItemAtIndex:index + 1];
360 if ([defaults boolForKey:@"showTime"]) {
361 [menu removeItemAtIndex:index + 1];
365 if (!isPlayingRadio) {
366 if ([defaults boolForKey:@"showTime"]) {
367 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]]
370 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
374 if ([curArtistName length] > 0) {
375 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
378 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
382 if ([curAlbumName length] > 0) {
383 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
386 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
391 if ([defaults boolForKey:@"showName"]) {
392 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
395 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
400 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
401 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
402 [menu insertItem:menuItem atIndex:trackInfoIndex];
405 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
406 [menu removeItemAtIndex:trackInfoIndex];
408 if ([defaults boolForKey:@"showName"] == YES) {
409 [menu removeItemAtIndex:trackInfoIndex];
412 if ([defaults boolForKey:@"showTime"] == YES) {
413 [menu removeItemAtIndex:trackInfoIndex];
416 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
417 [menu removeItemAtIndex:trackInfoIndex];
420 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
421 [menu removeItemAtIndex:trackInfoIndex];
424 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
425 [menu insertItem:menuItem atIndex:trackInfoIndex];
429 if ([defaults boolForKey:@"showArtist"]) {
430 didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
433 if ([defaults boolForKey:@"showAlbum"]) {
434 didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
439 //Rebuild the upcoming songs submenu. Can be improved a lot.
440 - (void)rebuildUpcomingSongsMenu
442 int curIndex = [currentRemote currentPlaylistIndex];
443 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curIndex];
444 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
445 if (!isPlayingRadio) {
447 int curTrack = [currentRemote currentSongIndex];
450 [upcomingSongsMenu release];
451 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
452 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
453 [upcomingSongsItem setEnabled:YES];
455 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
457 NSString *curSong = [currentRemote songTitleAtIndex:i];
458 NSMenuItem *songItem;
459 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
460 [songItem setTarget:self];
461 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
462 [upcomingSongsMenu addItem:songItem];
470 [upcomingSongsItem setSubmenu:nil];
471 [upcomingSongsItem setEnabled:NO];
475 - (void)rebuildPlaylistMenu
477 NSArray *playlists = [currentRemote playlists];
478 int i, curPlaylist = [currentRemote currentPlaylistIndex];
485 if (playlistMenu && ([playlists count] == [playlistMenu numberOfItems]))
488 [playlistMenu release];
489 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
491 for (i = 1; i < [playlists count]; i++) {
492 NSString *playlistName = [playlists objectAtIndex:i];
493 NSMenuItem *tempItem;
494 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
495 [tempItem setTarget:self];
496 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
497 [playlistMenu addItem:tempItem];
500 [playlistItem setSubmenu:playlistMenu];
503 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
507 //Build a menu with the list of all available EQ presets
508 - (void)rebuildEQPresetsMenu
510 NSArray *eqPresets = [currentRemote eqPresets];
513 if (eqMenu && ([[currentRemote eqPresets] count] == [eqMenu numberOfItems]))
517 eqMenu = [[NSMenu alloc] initWithTitle:@""];
519 for (i = 0; i < [eqPresets count]; i++) {
520 NSString *setName = [eqPresets objectAtIndex:i];
521 NSMenuItem *tempItem;
522 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
523 [tempItem setTarget:self];
524 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
525 [eqMenu addItem:tempItem];
528 [eqItem setSubmenu:eqMenu];
530 [[eqMenu itemAtIndex:[currentRemote currentEQPresetIndex] - 1] setState:NSOnState];
535 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
536 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
537 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
538 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
539 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
544 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
546 if ([defaults objectForKey:@"PlayPause"] != nil) {
547 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
548 combo:[defaults keyComboForKey:@"PlayPause"]
549 target:self action:@selector(playPause:)];
552 if ([defaults objectForKey:@"NextTrack"] != nil) {
553 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
554 combo:[defaults keyComboForKey:@"NextTrack"]
555 target:self action:@selector(nextSong:)];
558 if ([defaults objectForKey:@"PrevTrack"] != nil) {
559 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
560 combo:[defaults keyComboForKey:@"PrevTrack"]
561 target:self action:@selector(prevSong:)];
564 if ([defaults objectForKey:@"TrackInfo"] != nil) {
565 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
566 combo:[defaults keyComboForKey:@"TrackInfo"]
567 target:self action:@selector(showCurrentTrackInfo)];
570 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
571 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
572 combo:[defaults keyComboForKey:@"UpcomingSongs"]
573 target:self action:@selector(showUpcomingSongs)];
577 //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.
578 - (NSString *)runScriptAndReturnResult:(NSString *)script
580 AEDesc scriptDesc, resultDesc;
585 script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
587 AECreateDesc(typeChar, [script cString], [script cStringLength],
590 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
592 length = AEGetDescDataSize(&resultDesc);
593 buffer = malloc(length);
595 AEGetDescData(&resultDesc, buffer, length);
596 AEDisposeDesc(&scriptDesc);
597 AEDisposeDesc(&resultDesc);
598 result = [NSString stringWithCString:buffer length:length];
599 if ( (! [result isEqualToString:@""]) &&
600 ([result characterAtIndex:0] == '\"') &&
601 ([result characterAtIndex:[result length] - 1] == '\"') ) {
602 result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
609 //Called when the timer fires.
612 if ([currentRemote playerState] != stopped) {
613 int trackPlayingIndex = [currentRemote currentSongIndex];
614 int playlist = [currentRemote currentPlaylistIndex];
616 if (trackPlayingIndex != lastSongIndex) {
617 bool wasPlayingRadio = isPlayingRadio;
618 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
619 if (isPlayingRadio && !wasPlayingRadio) {
621 for (i = 0; i < [playlistMenu numberOfItems]; i++)
623 [[playlistMenu itemAtIndex:i] setState:NSOffState];
626 if (wasPlayingRadio) {
627 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
628 [menu insertItem:temp atIndex:trackInfoIndex + 1];
632 lastSongIndex = trackPlayingIndex;
636 if (playlist != [currentRemote currentPlaylistIndex]) {
637 bool wasPlayingRadio = isPlayingRadio;
638 isPlayingRadio = [[currentRemote classOfPlaylistAtIndex:playlist] isEqualToString:@"radio tuner playlist"];
639 if (isPlayingRadio && !wasPlayingRadio) {
641 for (i = 0; i < [playlistMenu numberOfItems]; i++)
643 [[playlistMenu itemAtIndex:i] setState:NSOffState];
646 if (wasPlayingRadio) {
647 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
648 [menu insertItem:temp atIndex:trackInfoIndex + 1];
652 lastSongIndex = trackPlayingIndex;
655 //Update Play/Pause menu item
656 if (playPauseMenuItem){
657 if ([currentRemote playerState] == playing) {
658 [playPauseMenuItem setTitle:@"Pause"];
660 [playPauseMenuItem setTitle:@"Play"];
666 - (void)iTunesLaunched:(NSNotification *)note
668 NSDictionary *info = [note userInfo];
670 iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
671 iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
674 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
676 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
677 [statusItem setMenu:menu]; //Set the menu back to the main one
680 - (void)iTunesTerminated:(NSNotification *)note
683 menu = [[NSMenu alloc] initWithTitle:@""];
684 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
685 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
686 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
687 [statusItem setMenu:menu];
689 [refreshTimer invalidate];
694 //Return the PSN of iTunes, if it's running
695 - (ProcessSerialNumber)iTunesPSN
697 NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
698 ProcessSerialNumber number;
701 number.highLongOfPSN = kNoProcess;
703 for (i = 0; i < [apps count]; i++)
705 NSDictionary *curApp = [apps objectAtIndex:i];
707 if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
709 number.highLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
710 number.lowLongOfPSN = [[curApp objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
716 //Send an AppleEvent with a given event ID
717 - (void)sendAEWithEventClass:(AEEventClass)eventClass
718 andEventID:(AEEventID)eventID
720 OSType iTunesType = 'hook';
721 AppleEvent event, reply;
723 AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
725 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
726 AEDisposeDesc(&event);
727 AEDisposeDesc(&reply);
732 // Selectors - called from status item menu
736 // Plugin dependent selectors
738 - (void)playTrack:(id)sender
740 [currentRemote switchToSongAtIndex:[[sender representedObject] intValue]];
744 - (void)selectPlaylist:(id)sender
746 int playlist = [[sender representedObject] intValue];
747 if (!isPlayingRadio) {
748 int curPlaylist = [currentRemote currentPlaylistIndex];
749 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
751 [currentRemote switchToPlaylistAtIndex:playlist];
752 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
756 - (void)selectEQPreset:(id)sender
758 int curSet = [currentRemote currentEQPresetIndex];
759 int item = [[sender representedObject] intValue];
760 [currentRemote switchToEQAtIndex:item];
761 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
762 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
765 - (void)playPause:(id)sender
767 PlayerState state = [currentRemote playerState];
768 if (state == playing) {
769 [currentRemote pause];
770 [playPauseMenuItem setTitle:@"Play"];
771 } else if ((state == forwarding) || (state == rewinding)) {
772 [currentRemote play];
773 [currentRemote pause];
775 [currentRemote play];
776 [playPauseMenuItem setTitle:@"Pause"];
780 - (void)nextSong:(id)sender
782 [currentRemote goToNextSong];
785 - (void)prevSong:(id)sender
787 [currentRemote goToPreviousSong];
790 - (void)fastForward:(id)sender
792 [currentRemote fastForward];
795 - (void)rewind:(id)sender
797 [currentRemote rewind];
802 // Plugin independent selectors
805 - (void)quitMenuTunes:(id)sender
807 [NSApp terminate:self];
810 //How is this going to work, now that we're pluginized?
811 - (void)openiTunes:(id)sender
813 [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
816 - (void)showPreferences:(id)sender
818 if (!prefsController) {
819 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
825 - (void)closePreferences
827 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))) {
830 [prefsController release];
831 prefsController = nil;
836 // Show Current Track Info And Show Upcoming Songs Floaters
840 - (void)showCurrentTrackInfo
842 NSString *trackName = [currentRemote currentSongTitle];
843 if (!statusController && [trackName length]) {
844 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
845 NSString *stringToShow = @"";
847 if ([defaults boolForKey:@"showName"]) {
848 if ([defaults boolForKey:@"showArtist"]) {
849 NSString *trackArtist = [currentRemote currentSongArtist];
850 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
852 stringToShow = [stringToShow stringByAppendingString:trackName];
853 stringToShow = [stringToShow stringByAppendingString:@"\n"];
856 if ([defaults boolForKey:@"showAlbum"]) {
857 NSString *trackAlbum = [currentRemote currentSongAlbum];
858 if ([trackAlbum length]) {
859 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
860 stringToShow = [stringToShow stringByAppendingString:@"\n"];
864 if ([defaults boolForKey:@"showTime"]) {
865 NSString *trackTime = [currentRemote currentSongLength];
866 if ([trackTime length]) {
867 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
872 int trackTimeLeft = [[currentRemote currentSongRemaining] intValue];
873 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
875 stringToShow = [stringToShow stringByAppendingString:
876 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
878 stringToShow = [stringToShow stringByAppendingString:
879 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
883 statusController = [[StatusWindowController alloc] init];
884 [statusController setTrackInfo:stringToShow];
885 [NSTimer scheduledTimerWithTimeInterval:3.0
887 selector:@selector(fadeAndCloseStatusWindow)
893 - (void)showUpcomingSongs
895 int curPlaylist = [currentRemote currentPlaylistIndex];
896 if (!statusController) {
897 int numSongs = [currentRemote numberOfSongsInPlaylistAtIndex:curPlaylist];
900 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
901 int curTrack = [currentRemote currentSongIndex];
903 NSString *songs = @"";
905 statusController = [[StatusWindowController alloc] init];
906 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
908 NSString *curSong = [currentRemote songTitleAtIndex:i];
909 songs = [songs stringByAppendingString:curSong];
910 songs = [songs stringByAppendingString:@"\n"];
913 [statusController setUpcomingSongs:songs];
914 [NSTimer scheduledTimerWithTimeInterval:3.0
916 selector:@selector(fadeAndCloseStatusWindow)
923 - (void)fadeAndCloseStatusWindow
925 [statusController fadeWindowOut];
926 [statusController release];
927 statusController = nil;
930 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
931 onItem:(NSMenuItem *)item
933 unichar charcode = 'a';
935 long cocoaModifiers = 0;
936 static long carbonToCocoa[6][2] =
938 { cmdKey, NSCommandKeyMask },
939 { optionKey, NSAlternateKeyMask },
940 { controlKey, NSControlKeyMask },
941 { shiftKey, NSShiftKeyMask },
944 for (i = 0; i < 6; i++)
946 if (modifiers & carbonToCocoa[i][0])
948 cocoaModifiers += carbonToCocoa[i][1];
951 [item setKeyEquivalentModifierMask:cocoaModifiers];
953 //Missing key combos for some keys. Must find them later.
967 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
968 NSLog(@"%@", menuRef);
969 SetMenuItemCommandKey(menuRef, 0, NO, 49);
970 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
971 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
977 charcode = NSDeleteFunctionKey;
993 charcode = NSF5FunctionKey;
997 charcode = NSF6FunctionKey;
1001 charcode = NSF7FunctionKey;
1005 charcode = NSF3FunctionKey;
1009 charcode = NSF8FunctionKey;
1013 charcode = NSF9FunctionKey;
1017 charcode = NSF11FunctionKey;
1021 charcode = NSF3FunctionKey;
1025 charcode = NSF14FunctionKey;
1029 charcode = NSF10FunctionKey;
1033 charcode = NSF12FunctionKey;
1037 charcode = NSF13FunctionKey;
1041 charcode = NSInsertFunctionKey;
1045 charcode = NSHomeFunctionKey;
1049 charcode = NSPageUpFunctionKey;
1053 charcode = NSDeleteFunctionKey;
1057 charcode = NSF4FunctionKey;
1061 charcode = NSEndFunctionKey;
1065 charcode = NSF2FunctionKey;
1069 charcode = NSPageDownFunctionKey;
1073 charcode = NSF1FunctionKey;
1077 charcode = NSLeftArrowFunctionKey;
1081 charcode = NSRightArrowFunctionKey;
1085 charcode = NSDownArrowFunctionKey;
1089 charcode = NSUpArrowFunctionKey;
1093 if (charcode == 'a') {
1094 unsigned long state;
1099 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
1100 keyTrans = KeyTranslate(kchr, code, &state);
1101 charCode = keyTrans;
1102 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
1103 } else if (charcode != 'b') {
1104 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];
1108 /*************************************************************************/
1110 #pragma mark NSApplication DELEGATE METHODS
1111 /*************************************************************************/
1113 - (void)applicationWillTerminate:(NSNotification *)note
1115 [self clearHotKeys];
1116 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1120 /*************************************************************************/
1122 #pragma mark DEALLOCATION METHODS
1123 /*************************************************************************/
1128 [refreshTimer invalidate];
1131 CloseComponent(asComponent);
1132 [currentRemote halt];
1133 [statusItem release];