3 ¥ Make preferences window pretty
5 ¥ Apple Events! Apple Events! Apple Events!
10 #import "PreferencesController.h"
11 #import "HotKeyCenter.h"
12 #import "StatusWindowController.h"
14 @interface MenuTunes(Private)
16 - (void)rebuildUpcomingSongsMenu;
17 - (void)rebuildPlaylistMenu;
18 - (void)rebuildEQPresetsMenu;
20 - (NSString *)runScriptAndReturnResult:(NSString *)script;
22 - (void)sendAEWithEventClass:(AEEventClass)eventClass andEventID:(AEEventID)eventID;
26 @implementation MenuTunes
28 /*************************************************************************/
30 #pragma mark INITIALIZATION METHODS
31 /*************************************************************************/
33 - (void)applicationDidFinishLaunching:(NSNotification *)note
35 asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
37 [self registerDefaultsIfNeeded];
39 menu = [[NSMenu alloc] initWithTitle:@""];
40 iTunesPSN = [self iTunesPSN]; //Get PSN of iTunes if it's running
42 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)))
45 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5
47 selector:@selector(timerUpdate)
53 menu = [[NSMenu alloc] initWithTitle:@""];
54 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
55 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
56 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
57 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
61 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
62 withLength:NSSquareStatusItemLength];
64 [statusItem setImage:[NSImage imageNamed:@"menu"]];
65 [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
66 [statusItem setMenu:menu];
67 // Below line of code is for creating builds for Beta Testers
68 // [statusItem setToolTip:@"This Nontransferable Beta (Built on XXX) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com)."];
72 /*************************************************************************/
74 #pragma mark INSTANCE METHODS
75 /*************************************************************************/
77 - (void)registerDefaultsIfNeeded
79 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
80 if (![defaults objectForKey:@"menu"]) {
82 NSMutableDictionary *loginwindow;
83 NSMutableArray *loginarray;
87 [NSArray arrayWithObjects:
100 @"Current Track Info",
101 nil] forKey:@"menu"];
103 [defaults synchronize];
104 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
105 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
107 for (i = 0; i < [loginarray count]; i++) {
108 NSDictionary *tempDict = [loginarray objectAtIndex:i];
109 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
115 if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
116 AEDesc scriptDesc, resultDesc;
117 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]];
119 AECreateDesc(typeChar, [script cString], [script cStringLength],
122 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
124 AEDisposeDesc(&scriptDesc);
125 AEDisposeDesc(&resultDesc);
130 if (![defaults integerForKey:@"SongsInAdvance"])
132 [defaults setInteger:5 forKey:@"SongsInAdvance"];
135 if (![defaults objectForKey:@"showName"]) {
136 [defaults setBool:YES forKey:@"showName"];
139 if (![defaults objectForKey:@"showArtist"]) {
140 [defaults setBool:YES forKey:@"showArtist"];
143 if (![defaults objectForKey:@"showAlbum"]) {
144 [defaults setBool:NO forKey:@"showAlbum"];
147 if (![defaults objectForKey:@"showTime"]) {
148 [defaults setBool:NO forKey:@"showTime"];
152 //Recreate the status item menu
155 NSArray *myMenu = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
159 didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
160 didHaveArtistName = ([[self runScriptAndReturnResult:@"return artist of current track"] length] > 0);
163 while ([menu numberOfItems] > 0) {
164 [menu removeItemAtIndex:0];
167 playPauseMenuItem = nil;
168 upcomingSongsItem = nil;
170 [playlistMenu release];
176 for (i = 0; i < [myMenu count]; i++) {
177 NSString *item = [myMenu objectAtIndex:i];
178 if ([item isEqualToString:@"Play/Pause"]) {
179 playPauseMenuItem = [menu addItemWithTitle:@"Play"
180 action:@selector(playPause:)
182 [playPauseMenuItem setTarget:self];
183 } else if ([item isEqualToString:@"Next Track"]) {
184 [[menu addItemWithTitle:@"Next Track"
185 action:@selector(nextSong:)
186 keyEquivalent:@""] setTarget:self];
187 } else if ([item isEqualToString:@"Previous Track"]) {
188 [[menu addItemWithTitle:@"Previous Track"
189 action:@selector(prevSong:)
190 keyEquivalent:@""] setTarget:self];
191 } else if ([item isEqualToString:@"Fast Forward"]) {
192 [[menu addItemWithTitle:@"Fast Forward"
193 action:@selector(fastForward:)
194 keyEquivalent:@""] setTarget:self];
195 } else if ([item isEqualToString:@"Rewind"]) {
196 [[menu addItemWithTitle:@"Rewind"
197 action:@selector(rewind:)
198 keyEquivalent:@""] setTarget:self];
199 } else if ([item isEqualToString:@"Upcoming Songs"]) {
200 upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
203 } else if ([item isEqualToString:@"Playlists"]) {
204 playlistItem = [menu addItemWithTitle:@"Playlists"
207 } else if ([item isEqualToString:@"EQ Presets"]) {
208 eqItem = [menu addItemWithTitle:@"EQ Presets"
211 } else if ([item isEqualToString:@"PreferencesÉ"]) {
212 [[menu addItemWithTitle:@"PreferencesÉ"
213 action:@selector(showPreferences:)
214 keyEquivalent:@""] setTarget:self];
215 } else if ([item isEqualToString:@"Quit"]) {
216 [[menu addItemWithTitle:@"Quit"
217 action:@selector(quitMenuTunes:)
218 keyEquivalent:@""] setTarget:self];
219 } else if ([item isEqualToString:@"Current Track Info"]) {
220 trackInfoIndex = [menu numberOfItems];
221 [menu addItemWithTitle:@"No Song"
224 } else if ([item isEqualToString:@"<separator>"]) {
225 [menu addItem:[NSMenuItem separatorItem]];
229 curTrackIndex = -1; //Force update of everything
230 [self timerUpdate]; //Updates dynamic info in the menu
236 //Updates the menu with current player state, song, and upcoming songs
239 NSMenuItem *menuItem;
240 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
242 if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
246 if (upcomingSongsItem) {
247 [self rebuildUpcomingSongsMenu];
251 [self rebuildPlaylistMenu];
255 [self rebuildEQPresetsMenu];
258 if (trackInfoIndex > -1)
260 NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
261 curSongName = [self runScriptAndReturnResult:@"return name of current track"];
263 if ([defaults boolForKey:@"showAlbum"]) {
264 curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
267 if ([defaults boolForKey:@"showArtist"]) {
268 curArtistName = [self runScriptAndReturnResult:@"return artist of current track"];
271 if ([curSongName length] > 0) {
272 int index = [menu indexOfItemWithTitle:@"Now Playing"];
274 if ([defaults boolForKey:@"showName"]) {
275 [menu removeItemAtIndex:index + 1];
277 if (didHaveAlbumName) {
278 [menu removeItemAtIndex:index + 1];
280 if (didHaveArtistName) {
281 [menu removeItemAtIndex:index + 1];
283 if ([defaults boolForKey:@"showTime"]) {
284 [menu removeItemAtIndex:index + 1];
287 if (!isPlayingRadio) {
288 if ([defaults boolForKey:@"showTime"]) {
289 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", [self runScriptAndReturnResult:@"return time of current track"]]
292 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
296 if ([curArtistName length] > 0) {
297 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curArtistName]
300 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
304 if ([curAlbumName length] > 0) {
305 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curAlbumName]
308 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
313 if ([defaults boolForKey:@"showName"]) {
314 menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@" %@", curSongName]
317 [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
322 menuItem = [[NSMenuItem alloc] initWithTitle:@"Now Playing" action:nil keyEquivalent:@""];
323 [menu removeItemAtIndex:[menu indexOfItemWithTitle:@"No Song"]];
324 [menu insertItem:menuItem atIndex:trackInfoIndex];
327 } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
328 [menu removeItemAtIndex:trackInfoIndex];
330 if ([defaults boolForKey:@"showName"] == YES) {
331 [menu removeItemAtIndex:trackInfoIndex];
334 if ([defaults boolForKey:@"showTime"] == YES) {
335 [menu removeItemAtIndex:trackInfoIndex];
338 if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
339 [menu removeItemAtIndex:trackInfoIndex];
342 if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
343 [menu removeItemAtIndex:trackInfoIndex];
346 menuItem = [[NSMenuItem alloc] initWithTitle:@"No Song" action:nil keyEquivalent:@""];
347 [menu insertItem:menuItem atIndex:trackInfoIndex];
351 if ([defaults boolForKey:@"showArtist"]) {
352 didHaveAlbumName = (([curArtistName length] > 0) ? YES : NO);
355 if ([defaults boolForKey:@"showAlbum"]) {
356 didHaveArtistName = (([curAlbumName length] > 0) ? YES : NO);
361 //Rebuild the upcoming songs submenu. Can be improved a lot.
362 - (void)rebuildUpcomingSongsMenu
364 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
365 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
366 if (!isPlayingRadio) {
368 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
371 [upcomingSongsMenu release];
372 upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
373 [upcomingSongsItem setSubmenu:upcomingSongsMenu];
374 [upcomingSongsItem setEnabled:YES];
376 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
378 NSString *curSong = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i of current playlist", i]];
379 NSMenuItem *songItem;
380 songItem = [[NSMenuItem alloc] initWithTitle:curSong action:@selector(playTrack:) keyEquivalent:@""];
381 [songItem setTarget:self];
382 [songItem setRepresentedObject:[NSNumber numberWithInt:i]];
383 [upcomingSongsMenu addItem:songItem];
391 [upcomingSongsItem setSubmenu:nil];
392 [upcomingSongsItem setEnabled:NO];
396 - (void)rebuildPlaylistMenu
398 int numPlaylists = [[self runScriptAndReturnResult:@"return number of playlists"] intValue];
399 int i, curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
406 if (playlistMenu && (numPlaylists == [playlistMenu numberOfItems]))
409 [playlistMenu release];
410 playlistMenu = [[NSMenu alloc] initWithTitle:@""];
412 for (i = 1; i <= numPlaylists; i++) {
413 NSString *playlistName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of playlist %i", i]];
414 NSMenuItem *tempItem;
415 tempItem = [[NSMenuItem alloc] initWithTitle:playlistName action:@selector(selectPlaylist:) keyEquivalent:@""];
416 [tempItem setTarget:self];
417 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
418 [playlistMenu addItem:tempItem];
421 [playlistItem setSubmenu:playlistMenu];
424 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOnState];
428 //Build a menu with the list of all available EQ presets
429 - (void)rebuildEQPresetsMenu
431 int numSets = [[self runScriptAndReturnResult:@"return number of EQ presets"] intValue];
434 if (eqMenu && (numSets == [eqMenu numberOfItems]))
438 eqMenu = [[NSMenu alloc] initWithTitle:@""];
440 for (i = 1; i <= numSets; i++) {
441 NSString *setName = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of EQ preset %i", i]];
442 NSMenuItem *tempItem;
443 tempItem = [[NSMenuItem alloc] initWithTitle:setName action:@selector(selectEQPreset:) keyEquivalent:@""];
444 [tempItem setTarget:self];
445 [tempItem setRepresentedObject:[NSNumber numberWithInt:i]];
446 [eqMenu addItem:tempItem];
449 [eqItem setSubmenu:eqMenu];
451 [[eqMenu itemAtIndex:[[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue] - 1] setState:NSOnState];
456 [[HotKeyCenter sharedCenter] removeHotKey:@"PlayPause"];
457 [[HotKeyCenter sharedCenter] removeHotKey:@"NextTrack"];
458 [[HotKeyCenter sharedCenter] removeHotKey:@"PrevTrack"];
459 [[HotKeyCenter sharedCenter] removeHotKey:@"TrackInfo"];
460 [[HotKeyCenter sharedCenter] removeHotKey:@"UpcomingSongs"];
465 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
467 if ([defaults objectForKey:@"PlayPause"] != nil) {
468 [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause"
469 combo:[defaults keyComboForKey:@"PlayPause"]
470 target:self action:@selector(playPause:)];
473 if ([defaults objectForKey:@"NextTrack"] != nil) {
474 [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack"
475 combo:[defaults keyComboForKey:@"NextTrack"]
476 target:self action:@selector(nextSong:)];
479 if ([defaults objectForKey:@"PrevTrack"] != nil) {
480 [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack"
481 combo:[defaults keyComboForKey:@"PrevTrack"]
482 target:self action:@selector(prevSong:)];
485 if ([defaults objectForKey:@"TrackInfo"] != nil) {
486 [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo"
487 combo:[defaults keyComboForKey:@"TrackInfo"]
488 target:self action:@selector(showCurrentTrackInfo)];
491 if ([defaults objectForKey:@"UpcomingSongs"] != nil) {
492 [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs"
493 combo:[defaults keyComboForKey:@"UpcomingSongs"]
494 target:self action:@selector(showUpcomingSongs)];
498 //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.
499 - (NSString *)runScriptAndReturnResult:(NSString *)script
501 AEDesc scriptDesc, resultDesc;
506 script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
508 AECreateDesc(typeChar, [script cString], [script cStringLength],
511 OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
513 length = AEGetDescDataSize(&resultDesc);
514 buffer = malloc(length);
516 AEGetDescData(&resultDesc, buffer, length);
517 AEDisposeDesc(&scriptDesc);
518 AEDisposeDesc(&resultDesc);
519 result = [NSString stringWithCString:buffer length:length];
520 if ( (! [result isEqualToString:@""]) &&
521 ([result characterAtIndex:0] == '\"') &&
522 ([result characterAtIndex:[result length] - 1] == '\"') ) {
523 result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
530 //Called when the timer fires.
534 if (GetProcessPID(&iTunesPSN, &pid) == noErr) {
535 int trackPlayingIndex = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
537 if (trackPlayingIndex != curTrackIndex) {
538 bool wasPlayingRadio = isPlayingRadio;
539 isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
540 if (isPlayingRadio && !wasPlayingRadio) {
542 for (i = 0; i < [playlistMenu numberOfItems]; i++)
544 [[playlistMenu itemAtIndex:i] setState:NSOffState];
547 if (wasPlayingRadio) {
548 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
549 [menu insertItem:temp atIndex:trackInfoIndex + 1];
553 curTrackIndex = trackPlayingIndex;
557 int playlist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
558 if (playlist != curPlaylistIndex) {
559 bool wasPlayingRadio = isPlayingRadio;
560 isPlayingRadio = [[self runScriptAndReturnResult:@"return class of current playlist"] isEqualToString:@"radio tuner playlist"];
561 if (isPlayingRadio && !wasPlayingRadio) {
563 for (i = 0; i < [playlistMenu numberOfItems]; i++)
565 [[playlistMenu itemAtIndex:i] setState:NSOffState];
568 if (wasPlayingRadio) {
569 NSMenuItem *temp = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
570 [menu insertItem:temp atIndex:trackInfoIndex + 1];
574 curTrackIndex = trackPlayingIndex;
575 curPlaylistIndex = playlist;
578 //Update Play/Pause menu item
579 if (playPauseMenuItem){
580 if ([[self runScriptAndReturnResult:@"return player state"] isEqualToString:@"playing"]) {
581 [playPauseMenuItem setTitle:@"Pause"];
583 [playPauseMenuItem setTitle:@"Play"];
588 menu = [[NSMenu alloc] initWithTitle:@""];
589 [[menu addItemWithTitle:@"Open iTunes" action:@selector(openiTunes:) keyEquivalent:@""] setTarget:self];
590 [[menu addItemWithTitle:@"Preferences" action:@selector(showPreferences:) keyEquivalent:@""] setTarget:self];
591 [[menu addItemWithTitle:@"Quit" action:@selector(quitMenuTunes:) keyEquivalent:@""] setTarget:self];
592 [statusItem setMenu:menu];
594 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
595 [refreshTimer invalidate];
601 - (void)iTunesLaunched:(NSNotification *)note
603 NSDictionary *info = [note userInfo];
605 iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
606 iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
609 refreshTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerUpdate) userInfo:nil repeats:YES];
611 [self rebuildMenu]; //Rebuild the menu since no songs will be playing
612 [statusItem setMenu:menu]; //Set the menu back to the main one
613 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
616 //Return the PSN of iTunes, if it's running
617 - (ProcessSerialNumber)iTunesPSN
619 ProcessSerialNumber procNum;
620 procNum.highLongOfPSN = kNoProcess;
621 procNum.lowLongOfPSN = 0;
623 while ( (GetNextProcess(&procNum) == noErr) ) {
624 CFStringRef procName;
625 if ( (CopyProcessName(&procNum, &procName) == noErr) ) {
626 if ([(NSString *)procName isEqualToString:@"iTunes"]) {
629 [(NSString *)procName release];
635 //Send an AppleEvent with a given event ID
636 - (void)sendAEWithEventClass:(AEEventClass)eventClass
637 andEventID:(AEEventID)eventID
639 OSType iTunesType = 'hook';
640 AppleEvent event, reply;
642 AEBuildAppleEvent(eventClass, eventID, typeApplSignature, &iTunesType, sizeof(iTunesType), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
644 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
645 AEDisposeDesc(&event);
646 AEDisposeDesc(&reply);
650 // Selectors - called from status item menu
653 - (void)playTrack:(id)sender
655 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play track %i of current playlist", [[sender representedObject] intValue]]];
659 - (void)selectPlaylist:(id)sender
661 int playlist = [[sender representedObject] intValue];
662 if (!isPlayingRadio) {
663 int curPlaylist = [[self runScriptAndReturnResult:@"return index of current playlist"] intValue];
664 [[playlistMenu itemAtIndex:curPlaylist - 1] setState:NSOffState];
666 [self runScriptAndReturnResult:[NSString stringWithFormat:@"play playlist %i", playlist]];
667 [[playlistMenu itemAtIndex:playlist - 1] setState:NSOnState];
670 - (void)selectEQPreset:(id)sender
672 int curSet = [[self runScriptAndReturnResult:@"return index of current EQ preset"] intValue];
673 int item = [[sender representedObject] intValue];
674 [self runScriptAndReturnResult:[NSString stringWithFormat:@"set current EQ preset to EQ preset %i", item]];
675 [self runScriptAndReturnResult:@"set EQ enabled to 1"];
676 [[eqMenu itemAtIndex:curSet - 1] setState:NSOffState];
677 [[eqMenu itemAtIndex:item - 1] setState:NSOnState];
680 - (void)playPause:(id)sender
682 NSString *state = [self runScriptAndReturnResult:@"return player state"];
683 if ([state isEqualToString:@"playing"]) {
684 [self sendAEWithEventClass:'hook' andEventID:'Paus'];
685 [playPauseMenuItem setTitle:@"Play"];
686 } else if ([state isEqualToString:@"fast forwarding"] || [state
687 isEqualToString:@"rewinding"]) {
688 [self sendAEWithEventClass:'hook' andEventID:'Paus'];
689 [self sendAEWithEventClass:'hook' andEventID:'Play'];
691 [self sendAEWithEventClass:'hook' andEventID:'Play'];
692 [playPauseMenuItem setTitle:@"Pause"];
696 - (void)nextSong:(id)sender
698 [self sendAEWithEventClass:'hook' andEventID:'Next'];
701 - (void)prevSong:(id)sender
703 [self sendAEWithEventClass:'hook' andEventID:'Prev'];
706 - (void)fastForward:(id)sender
708 [self sendAEWithEventClass:'hook' andEventID:'Fast'];
711 - (void)rewind:(id)sender
713 [self sendAEWithEventClass:'hook' andEventID:'Rwnd'];
716 - (void)quitMenuTunes:(id)sender
718 [NSApp terminate:self];
721 - (void)openiTunes:(id)sender
723 [[NSWorkspace sharedWorkspace] launchApplication:@"iTunes"];
726 - (void)showPreferences:(id)sender
728 if (!prefsController) {
729 prefsController = [[PreferencesController alloc] initWithMenuTunes:self];
735 - (void)closePreferences
737 if (!((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0))) {
740 [prefsController release];
741 prefsController = nil;
746 // Show Current Track Info And Show Upcoming Songs Floaters
750 - (void)showCurrentTrackInfo
752 NSString *trackName = [self runScriptAndReturnResult:@"return name of current track"];
753 if (!statusController && [trackName length]) {
754 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
755 NSString *stringToShow = @"";
758 if ([defaults boolForKey:@"showName"]) {
759 if ([defaults boolForKey:@"showArtist"]) {
760 NSString *trackArtist = [self runScriptAndReturnResult:@"return artist of current track"];
761 trackName = [NSString stringWithFormat:@"%@ - %@", trackArtist, trackName];
763 stringToShow = [stringToShow stringByAppendingString:trackName];
764 stringToShow = [stringToShow stringByAppendingString:@"\n"];
765 if ([trackName length] > 38) {
771 if ([defaults boolForKey:@"showAlbum"]) {
772 NSString *trackAlbum = [self runScriptAndReturnResult:@"return album of current track"];
773 if ([trackAlbum length]) {
774 stringToShow = [stringToShow stringByAppendingString:trackAlbum];
775 stringToShow = [stringToShow stringByAppendingString:@"\n"];
780 if ([defaults boolForKey:@"showTime"]) {
781 NSString *trackTime = [self runScriptAndReturnResult:@"return time of current track"];
782 if ([trackTime length]) {
783 stringToShow = [NSString stringWithFormat:@"%@Total Time: %@\n", stringToShow, trackTime];
789 int trackTimeLeft = [[self runScriptAndReturnResult:@"return (duration of current track) - player position"] intValue];
790 int minutes = trackTimeLeft / 60, seconds = trackTimeLeft % 60;
792 stringToShow = [stringToShow stringByAppendingString:
793 [NSString stringWithFormat:@"Time Remaining: %i:0%i", minutes, seconds]];
795 stringToShow = [stringToShow stringByAppendingString:
796 [NSString stringWithFormat:@"Time Remaining: %i:%i", minutes, seconds]];
800 statusController = [[StatusWindowController alloc] init];
801 [statusController setTrackInfo:stringToShow lines:lines];
802 [NSTimer scheduledTimerWithTimeInterval:3.0
804 selector:@selector(fadeAndCloseStatusWindow)
810 - (void)showUpcomingSongs
812 if (!statusController) {
813 int numSongs = [[self runScriptAndReturnResult:@"return number of tracks in current playlist"] intValue];
816 int numSongsInAdvance = [[NSUserDefaults standardUserDefaults] integerForKey:@"SongsInAdvance"];
817 int curTrack = [[self runScriptAndReturnResult:@"return index of current track"] intValue];
819 NSString *songs = @"";
821 statusController = [[StatusWindowController alloc] init];
822 for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
824 NSString *curSong = [self runScriptAndReturnResult:
825 [NSString stringWithFormat:@"return name of track %i of current playlist", i]];
826 songs = [songs stringByAppendingString:curSong];
827 songs = [songs stringByAppendingString:@"\n"];
830 [statusController setUpcomingSongs:songs numSongs:numSongsInAdvance];
831 [NSTimer scheduledTimerWithTimeInterval:3.0
833 selector:@selector(fadeAndCloseStatusWindow)
840 - (void)fadeAndCloseStatusWindow
842 [statusController fadeWindowOut];
843 [statusController release];
844 statusController = nil;
847 /*************************************************************************/
849 #pragma mark NSApplication DELEGATE METHODS
850 /*************************************************************************/
852 - (void)applicationWillTerminate:(NSNotification *)note
855 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
859 /*************************************************************************/
861 #pragma mark DEALLOCATION METHODS
862 /*************************************************************************/
867 [refreshTimer invalidate];
870 CloseComponent(asComponent);
871 [statusItem release];