Making MenuTunes use the ITKit, for ITStatusItem. There are a few notes:
[MenuTunes.git] / MenuTunes.m
index a17391f..7df098f 100755 (executable)
@@ -57,16 +57,15 @@ Things to do:
         [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
         refreshTimer = nil;
     }
         [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(iTunesLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
         refreshTimer = nil;
     }
+
+    statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
+                                              withLength:NSVariableStatusItemLength];
     
     
-    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
-    [[[statusItem _button] cell] setType:NSNullCellType];
     [statusItem setImage:[NSImage imageNamed:@"menu"]];
     [statusItem setImage:[NSImage imageNamed:@"menu"]];
-    [[statusItem _button] setAlternateImage:[NSImage imageNamed:@"selected_image"]];
-    [statusItem setHighlightMode:YES];
+    [statusItem setAlternateImage:[NSImage imageNamed:@"selected_image"]];
     [statusItem setMenu:menu];
     // Below line of code is for creating builds for Beta Testers
     // [statusItem setToolTip:@"This Nontransferable Beta (Built on __DATE__) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com)."];
     [statusItem setMenu:menu];
     // Below line of code is for creating builds for Beta Testers
     // [statusItem setToolTip:@"This Nontransferable Beta (Built on __DATE__) of iThink Software's MenuTunes is Registered to: Beta Tester (betatester@somedomain.com)."];
-    [statusItem retain];
 }
 
 
 }
 
 
@@ -77,8 +76,9 @@ Things to do:
 
 - (void)registerDefaultsIfNeeded
 {
 
 - (void)registerDefaultsIfNeeded
 {
-    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"menu"]) {
-        [[NSUserDefaults standardUserDefaults] setObject:
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+    if (![defaults objectForKey:@"menu"]) {
+        [defaults setObject:
             [NSArray arrayWithObjects:
                 @"Play/Pause",
                 @"Next Track",
             [NSArray arrayWithObjects:
                 @"Play/Pause",
                 @"Next Track",
@@ -95,6 +95,22 @@ Things to do:
                 @"Current Track Info",
                 nil] forKey:@"menu"];
     }
                 @"Current Track Info",
                 nil] forKey:@"menu"];
     }
+    
+    if (![defaults objectForKey:@"showName"]) {
+        [defaults setBool:YES forKey:@"showName"];
+    }
+    
+    if (![defaults objectForKey:@"showArtist"]) {
+        [defaults setBool:YES forKey:@"showArtist"];
+    }
+    
+    if (![defaults objectForKey:@"showAlbum"]) {
+        [defaults setBool:NO forKey:@"showAlbum"];
+    }
+    
+    if (![defaults objectForKey:@"showTime"]) {
+        [defaults setBool:NO forKey:@"showTime"];
+    }
 }
 
 //Recreate the status item menu
 }
 
 //Recreate the status item menu
@@ -105,7 +121,9 @@ Things to do:
     
     trackInfoIndex = -1;
     didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
     
     trackInfoIndex = -1;
     didHaveAlbumName = ([[self runScriptAndReturnResult:@"return album of current track"] length] > 0);
-
+    didHaveArtistName = ([[self runScriptAndReturnResult:@"return artist of current track"] length] > 0);
+    
+    
     while ([menu numberOfItems] > 0) {
         [menu removeItemAtIndex:0];
     }
     while ([menu numberOfItems] > 0) {
         [menu removeItemAtIndex:0];
     }
@@ -182,8 +200,9 @@ Things to do:
 //Updates the menu with current player state, song, and upcoming songs
 - (void)updateMenu
 {
 //Updates the menu with current player state, song, and upcoming songs
 - (void)updateMenu
 {
-    NSString *curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
     NSMenuItem *menuItem;
     NSMenuItem *menuItem;
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+    
     if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
         return;
     }
     if ((iTunesPSN.highLongOfPSN == kNoProcess) && (iTunesPSN.lowLongOfPSN == 0)) {
         return;
     }
@@ -191,27 +210,62 @@ Things to do:
     if (upcomingSongsItem) {
         [self rebuildUpcomingSongsMenu];
     }
     if (upcomingSongsItem) {
         [self rebuildUpcomingSongsMenu];
     }
+    
     if (playlistItem) {
         [self rebuildPlaylistMenu];
     }
     if (playlistItem) {
         [self rebuildPlaylistMenu];
     }
+    
     if (eqItem) {
         [self rebuildEQPresetsMenu];
     }
     
     if (trackInfoIndex > -1)
     {
     if (eqItem) {
         [self rebuildEQPresetsMenu];
     }
     
     if (trackInfoIndex > -1)
     {
-        NSString *curSongName;
+        NSString *curSongName, *curAlbumName = @"", *curArtistName = @"";
         curSongName = [self runScriptAndReturnResult:@"return name of current track"];
         curSongName = [self runScriptAndReturnResult:@"return name of current track"];
+        
+        if ([defaults boolForKey:@"showArtist"]) {
+            curAlbumName = [self runScriptAndReturnResult:@"return album of current track"];
+        }
+        
+        if ([defaults boolForKey:@"showAlbum"]) {
+            curArtistName = [self runScriptAndReturnResult:@"return artist of current track"];
+        }
+        
         if ([curSongName length] > 0) {
             int index = [menu indexOfItemWithTitle:@"Now Playing"];
             
             if (index > -1) {
         if ([curSongName length] > 0) {
             int index = [menu indexOfItemWithTitle:@"Now Playing"];
             
             if (index > -1) {
-                [menu removeItemAtIndex:index + 1];
+                if ([defaults boolForKey:@"showName"]) {
+                    [menu removeItemAtIndex:index + 1];
+                }
                 if (didHaveAlbumName) {
                     [menu removeItemAtIndex:index + 1];
                 }
                 if (didHaveAlbumName) {
                     [menu removeItemAtIndex:index + 1];
                 }
+                if (didHaveArtistName) {
+                    [menu removeItemAtIndex:index + 1];
+                }
+                if ([defaults boolForKey:@"showTime"]) {
+                    [menu removeItemAtIndex:index + 1];
+                }
             }
             if (!isPlayingRadio) {
             }
             if (!isPlayingRadio) {
+                if ([defaults boolForKey:@"showTime"]) {
+                    menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", [self runScriptAndReturnResult:@"return time of current track"]]
+                                                        action:nil
+                                                        keyEquivalent:@""];
+                    [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
+                    [menuItem release];
+                }
+                
+                if ([curArtistName length] > 0) {
+                    menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curArtistName]
+                                                        action:nil
+                                                        keyEquivalent:@""];
+                    [menu insertItem:menuItem atIndex:trackInfoIndex + 1];
+                    [menuItem release];
+                }
+                
                 if ([curAlbumName length] > 0) {
                     menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curAlbumName]
                                                         action:nil
                 if ([curAlbumName length] > 0) {
                     menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"  %@", curAlbumName]
                                                         action:nil
@@ -235,9 +289,20 @@ Things to do:
             
         } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
             [menu removeItemAtIndex:trackInfoIndex];
             
         } else if ([menu indexOfItemWithTitle:@"No Song"] == -1) {
             [menu removeItemAtIndex:trackInfoIndex];
-            [menu removeItemAtIndex:trackInfoIndex];
             
             
-            if (didHaveAlbumName) {
+            if ([defaults boolForKey:@"showName"] == YES) {
+                [menu removeItemAtIndex:trackInfoIndex];
+            }
+            
+            if ([defaults boolForKey:@"showTime"] == YES) {
+                [menu removeItemAtIndex:trackInfoIndex];
+            }
+            
+            if (didHaveArtistName && [defaults boolForKey:@"showArtist"]) {
+                [menu removeItemAtIndex:trackInfoIndex];
+            }
+            
+            if (didHaveAlbumName && [defaults boolForKey:@"showAlbum"]) {
                 [menu removeItemAtIndex:trackInfoIndex];
             }
             
                 [menu removeItemAtIndex:trackInfoIndex];
             }
             
@@ -245,8 +310,15 @@ Things to do:
             [menu insertItem:menuItem atIndex:trackInfoIndex];
             [menuItem release];
         }
             [menu insertItem:menuItem atIndex:trackInfoIndex];
             [menuItem release];
         }
+        
+        if ([defaults boolForKey:@"showArtist"]) {
+                didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
+            }
+            
+            if ([defaults boolForKey:@"showAlbum"]) {
+                didHaveArtistName = (([curArtistName length] > 0) ? YES : NO);
+            }
     }
     }
-    didHaveAlbumName = (([curAlbumName length] > 0) ? YES : NO);
 }
 
 //Rebuild the upcoming songs submenu. Can be improved a lot.
 }
 
 //Rebuild the upcoming songs submenu. Can be improved a lot.