- NSArray *myMenu = [df arrayForKey:@"menu"];
- int playlist = [currentRemote currentPlaylistIndex];
- int i;
-
- if ([currentRemote playerRunningState] == ITMTRemotePlayerNotRunning) {
- return;
- }
-
- trackInfoIndex = -1;
- lastPlaylistIndex = -1;
-
- [menu release];
- menu = [[NSMenu alloc] initWithTitle:@""];
-
- playPauseItem = nil;
-
- upcomingSongsItem = nil;
- [upcomingSongsMenu release];
- upcomingSongsMenu = nil;
-
- if (ratingItem) {
- [ratingItem setSubmenu:nil];
- }
-
- playlistItem = nil;
- [playlistMenu release];
- playlistMenu = nil;
-
- eqItem = nil;
- [eqMenu release];
- eqMenu = nil;
-
- //Build the custom menu
- for (i = 0; i < [myMenu count]; i++) {
- NSString *item = [myMenu objectAtIndex:i];
- if ([item isEqualToString:@"Play/Pause"]) {
- KeyCombo *tempCombo = [df keyComboForKey:@"PlayPause"];
- playPauseItem = [menu addItemWithTitle:@"Play"
- action:@selector(playPause:)
- keyEquivalent:@""];
-
- if (tempCombo) {
- [self setKeyEquivalentForCode:[tempCombo keyCode]
- andModifiers:[tempCombo modifiers] onItem:playPauseItem];
- [tempCombo release];
- }
- } else if ([item isEqualToString:@"Next Track"]) {
- KeyCombo *tempCombo = [df keyComboForKey:@"NextTrack"];
- NSMenuItem *nextTrack = [menu addItemWithTitle:@"Next Track"
- action:@selector(nextSong:)
- keyEquivalent:@""];
-
- if (tempCombo) {
- [self setKeyEquivalentForCode:[tempCombo keyCode]
- andModifiers:[tempCombo modifiers] onItem:nextTrack];
- [tempCombo release];
- }
- } else if ([item isEqualToString:@"Previous Track"]) {
- KeyCombo *tempCombo = [df keyComboForKey:@"PrevTrack"];
- NSMenuItem *prevTrack = [menu addItemWithTitle:@"Previous Track"
- action:@selector(prevSong:)
- keyEquivalent:@""];
-
- if (tempCombo) {
- [self setKeyEquivalentForCode:[tempCombo keyCode]
- andModifiers:[tempCombo modifiers] onItem:prevTrack];
- [tempCombo release];
- }
- } else if ([item isEqualToString:@"Fast Forward"]) {
- [menu addItemWithTitle:@"Fast Forward"
- action:@selector(fastForward:)
- keyEquivalent:@""];
- } else if ([item isEqualToString:@"Rewind"]) {
- [menu addItemWithTitle:@"Rewind"
- action:@selector(rewind:)
- keyEquivalent:@""];
- } else if ([item isEqualToString:@"Show Player"]) {
- [menu addItemWithTitle:[NSString stringWithFormat:@"Show %@", [currentRemote playerSimpleName]]
- action:@selector(showPlayer:)
- keyEquivalent:@""];
- } else if ([item isEqualToString:@"Upcoming Songs"]) {
- upcomingSongsItem = [menu addItemWithTitle:@"Upcoming Songs"
- action:nil
- keyEquivalent:@""];
- upcomingSongsMenu = [[NSMenu alloc] initWithTitle:@""];
- [upcomingSongsItem setSubmenu:upcomingSongsMenu];
- [upcomingSongsItem setEnabled:NO];
- } else if ([item isEqualToString:@"Playlists"]) {
- playlistItem = [menu addItemWithTitle:@"Playlists"
- action:nil
- keyEquivalent:@""];
- } else if ([item isEqualToString:@"EQ Presets"]) {
- eqItem = [menu addItemWithTitle:@"EQ Presets"
- action:nil
- keyEquivalent:@""];
- } else if ([item isEqualToString:@"Preferences…"]) {
- [menu addItemWithTitle:@"Preferences…"
- action:@selector(showPreferences:)
- keyEquivalent:@""];
- } else if ([item isEqualToString:@"Quit"]) {
- [menu addItemWithTitle:@"Quit"
- action:@selector(quitMenuTunes:)
- keyEquivalent:@""];
- } else if ([item isEqualToString:@"Current Track Info"]) {
- trackInfoIndex = [menu numberOfItems];
- [menu addItemWithTitle:@"No Song"
- action:nil
- keyEquivalent:@""];
- } else if ([item isEqualToString:@"Song Rating"]) {
- ratingItem = [menu addItemWithTitle:@"Song Rating"
- action:nil
- keyEquivalent:@""];
- [ratingItem setSubmenu:ratingMenu];
- } else if ([item isEqualToString:@"<separator>"]) {
- [menu addItem:[NSMenuItem separatorItem]];
- }
- }
-
- if (playlistItem) {
- [self rebuildPlaylistMenu];
- }
-
- if (eqItem) {
- [self rebuildEQPresetsMenu];
- }
-
- isPlayingRadio = ([currentRemote classOfPlaylistAtIndex:playlist] == ITMTRemotePlayerRadioPlaylist);
-
- if (upcomingSongsItem) {
- [self rebuildUpcomingSongsMenu];
- }
-
- if (ratingItem) {
- if (isPlayingRadio || !playlist) {
- [ratingItem setEnabled:NO];
- } else {
- int currentSongRating = ([currentRemote currentSongRating] * 5);
- [[ratingMenu itemAtIndex:lastSongRating] setState:NSOffState];
- lastSongRating = currentSongRating;
- [[ratingMenu itemAtIndex:lastSongRating] setState:NSOnState];
- [ratingItem setEnabled:YES];
- }
- }
-
- //Set the new unique song identifier
- lastSongIdentifier = [[currentRemote currentSongUniqueIdentifier] retain];
-
- //If we're in a playlist or radio mode
- if ( ![lastSongIdentifier isEqualToString:@"0-0"] && (trackInfoIndex > -1) ) {
- NSString *title;
-
- if ( (i = [menu indexOfItemWithTitle:@"No Song"]) ) {
- if ( (i > -1) ) {
- [menu removeItemAtIndex:i];
- [menu insertItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@"" atIndex:i];
- }
- }
-
- title = [currentRemote currentSongTitle];
-
- if (!isPlayingRadio) {
- if ([df boolForKey:@"showTime"]) {
- NSString *length = [currentRemote currentSongLength];
- char character = [length characterAtIndex:0];
- if ( (character > '0') && (character < '9') ) {
- [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongLength]] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
- }
- }
-
- if ([df boolForKey:@"showRating"]) {
- if (title) { //Check to see if there's a song playing
- [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", [[ratingMenu itemAtIndex:[currentRemote currentSongRating] * 5] title]] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
- }
- }
-
- if ([df boolForKey:@"showArtist"]) {
- NSString *artist = [currentRemote currentSongArtist];
- if ([artist length] > 0) {
- [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", artist] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
- }
- }
-
- if ([df boolForKey:@"showNumber"]) {
- int track = [currentRemote currentSongTrack];
- int total = [currentRemote currentAlbumTrackCount];
- if (total > 0) {
- [menu insertItemWithTitle:[NSString stringWithFormat:@" Track %i of %i", track, total] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
- }
- }
-
- if ([df boolForKey:@"showAlbum"]) {
- NSString *album = [currentRemote currentSongAlbum];
- if ([album length] > 0) {
- [menu insertItemWithTitle:[NSString stringWithFormat:@" %@", album] action:nil keyEquivalent:@"" atIndex:trackInfoIndex + 1];
+ if ([[self currentRemote] playerStateUniqueIdentifier] == nil) {
+ if ([statusItem isEnabled]) {
+ [statusItem setToolTip:@"iTunes not responding."];
+ [[ITHotKeyCenter sharedCenter] setEnabled:NO];
+ }
+ [statusItem setEnabled:NO];
+ return;
+ } else if (![statusItem isEnabled]) {
+ [statusItem setEnabled:YES];
+ [statusItem setToolTip:_toolTip];
+ [[ITHotKeyCenter sharedCenter] setEnabled:YES];
+ return;
+ }
+
+ if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
+ ITDebugLog(@"The song changed. '%@'", _latestSongIdentifier);
+ if ([df boolForKey:@"runScripts"]) {
+ NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
+ NSEnumerator *scriptsEnum = [scripts objectEnumerator];
+ NSString *nextScript;
+ ITDebugLog(@"Running AppleScripts for song change.");
+ while ( (nextScript = [scriptsEnum nextObject]) ) {
+ NSDictionary *error;
+ NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
+ ITDebugLog(@"Running script: %@", nextScript);
+ if (!currentScript || ![currentScript executeAndReturnError:nil]) {
+ ITDebugLog(@"Error running script %@.", nextScript);