1010
1111
1212static NSString * const SFYPlayerStatePreferenceKey = @" ShowPlayerState" ;
13- static NSString * const SFYPlayerDockIconPreferenceKey = @" YES" ;
13+ static NSString * const SFYHideIfNotPlayingKey = @" HideIfNotPlaying" ;
14+ static NSString * const SFYPlayerDockIconPreferenceKey = @" ShowPlayerDockIcon" ;
1415
1516@interface SFYAppDelegate ()
1617
1718@property (nonatomic , strong ) NSMenuItem *playerStateMenuItem;
1819@property (nonatomic , strong ) NSMenuItem *dockIconMenuItem;
20+ @property (nonatomic , strong ) NSMenuItem *hideIfNotPlayingItem;
1921@property (nonatomic , strong ) NSStatusItem *statusItem;
2022
2123@end
@@ -24,20 +26,22 @@ @implementation SFYAppDelegate
2426
2527- (void )applicationDidFinishLaunching : (NSNotification * __unused)aNotification
2628{
27- // Initialize the variable the getDockIconVisibility method checks
28- [[NSUserDefaults standardUserDefaults ] setBool: YES forKey: SFYPlayerDockIconPreferenceKey];
29-
29+ [self showOrHideDockIcon ];
30+
3031 self.statusItem = [[NSStatusBar systemStatusBar ] statusItemWithLength: NSVariableStatusItemLength ];
3132 self.statusItem .highlightMode = YES ;
3233
3334 NSMenu *menu = [[NSMenu alloc ] initWithTitle: @" " ];
3435
3536 self.playerStateMenuItem = [[NSMenuItem alloc ] initWithTitle: [self determinePlayerStateMenuItemTitle ] action: @selector (togglePlayerStateVisibility ) keyEquivalent: @" " ];
3637
37- self.dockIconMenuItem = [[NSMenuItem alloc ] initWithTitle: NSLocalizedString(@" Hide Dock Icon" , nil ) action: @selector (toggleDockIconVisibility ) keyEquivalent: @" " ];
38+ self.dockIconMenuItem = [[NSMenuItem alloc ] initWithTitle: [self determineDockIconMenuItemTitle ] action: @selector (toggleDockIconVisibility ) keyEquivalent: @" " ];
39+
40+ self.hideIfNotPlayingItem = [[NSMenuItem alloc ] initWithTitle: [self determineHideIfNotPlayingMenuItemTitle ] action: @selector (toggleHideIfNotPlaying ) keyEquivalent: @" " ];
3841
3942 [menu addItem: self .playerStateMenuItem];
4043 [menu addItem: self .dockIconMenuItem];
44+ [menu addItem: self .hideIfNotPlayingItem];
4145 [menu addItemWithTitle: NSLocalizedString(@" Quit" , nil ) action: @selector (quit ) keyEquivalent: @" q" ];
4246
4347 [self .statusItem setMenu: menu];
@@ -53,23 +57,37 @@ - (void)setStatusItemTitle
5357 NSString *trackName = [[self executeAppleScript: @" get name of current track" ] stringValue ];
5458 NSString *artistName = [[self executeAppleScript: @" get artist of current track" ] stringValue ];
5559
56- if (trackName && artistName) {
57- NSString *titleText = [NSString stringWithFormat: @" %@ - %@ " , trackName, artistName];
58-
59- if ([self getPlayerStateVisibility ]) {
60- NSString *playerState = [self determinePlayerStateText ];
61- titleText = [NSString stringWithFormat: @" %@ (%@ )" , titleText, playerState];
62- }
63-
64- self.statusItem .image = nil ;
65- self.statusItem .title = titleText;
60+ if ([self getHideIfNotPlaying ] && ![self checkIfIsPlaying ]) {
61+ [self showIconAndHideText ];
6662 }
6763 else {
68- NSImage *image = [NSImage imageNamed: @" status_icon" ];
69- [image setTemplate: true ];
70- self.statusItem .image = image;
71- self.statusItem .title = nil ;
64+ if (trackName && artistName) {
65+ NSString *titleText = [NSString stringWithFormat: @" %@ - %@ ♪" , trackName, artistName];
66+
67+ if ([self getPlayerStateVisibility ]) {
68+ NSString *playerState = [self determinePlayerStateText ];
69+ titleText = [NSString stringWithFormat: @" %@ (%@ )" , titleText, playerState];
70+ }
71+
72+ [self hideIconAndDisplayText: titleText];
73+ }
74+ else {
75+ [self showIconAndHideText ];
76+ }
7277 }
78+
79+ }
80+
81+ - (void )hideIconAndDisplayText : (NSString *)titleText {
82+ self.statusItem .image = nil ;
83+ self.statusItem .title = titleText;
84+ }
85+
86+ - (void )showIconAndHideText {
87+ NSImage *image = [NSImage imageNamed: @" status_icon" ];
88+ [image setTemplate: true ];
89+ self.statusItem .image = image;
90+ self.statusItem .title = nil ;
7391}
7492
7593#pragma mark - Executing AppleScript
@@ -105,6 +123,12 @@ - (NSString *)determinePlayerStateMenuItemTitle
105123 return [self getPlayerStateVisibility ] ? NSLocalizedString(@" Hide Player State" , nil ) : NSLocalizedString(@" Show Player State" , nil );
106124}
107125
126+ - (BOOL )checkIfIsPlaying
127+ {
128+ NSString *playerStateConstant = [[self executeAppleScript: @" get player state" ] stringValue ];
129+ return [playerStateConstant isEqualToString: @" kPSP" ];
130+ }
131+
108132- (NSString *)determinePlayerStateText
109133{
110134 NSString *playerStateText = nil ;
@@ -125,6 +149,22 @@ - (NSString *)determinePlayerStateText
125149
126150#pragma mark - Toggle Dock Icon
127151
152+ - (BOOL )getHideIfNotPlaying
153+ {
154+ return [[NSUserDefaults standardUserDefaults ] boolForKey: SFYHideIfNotPlayingKey];
155+ }
156+
157+ -(void )setHideIfNotPlaying : (BOOL )hide
158+ {
159+ [[NSUserDefaults standardUserDefaults ] setBool: hide forKey: SFYHideIfNotPlayingKey];
160+ }
161+
162+ - (void )toggleHideIfNotPlaying
163+ {
164+ [self setHideIfNotPlaying: ![self getHideIfNotPlaying ]];
165+ self.hideIfNotPlayingItem .title = [self determineHideIfNotPlayingMenuItemTitle ];
166+ }
167+
128168- (BOOL )getDockIconVisibility
129169{
130170 return [[NSUserDefaults standardUserDefaults ] boolForKey: SFYPlayerDockIconPreferenceKey];
@@ -139,7 +179,11 @@ - (void)toggleDockIconVisibility
139179{
140180 [self setDockIconVisibility: ![self getDockIconVisibility ]];
141181 self.dockIconMenuItem .title = [self determineDockIconMenuItemTitle ];
142-
182+ [self showOrHideDockIcon ];
183+ }
184+
185+ - (void ) showOrHideDockIcon
186+ {
143187 if (![self getDockIconVisibility ])
144188 {
145189 // Apple recommended method to show and hide dock icon
@@ -153,6 +197,12 @@ - (void)toggleDockIconVisibility
153197 }
154198}
155199
200+ - (NSString *)determineHideIfNotPlayingMenuItemTitle
201+ {
202+ return ![self getHideIfNotPlaying ] ? NSLocalizedString(@" Hide If Not Playing" , nil ) :
203+ NSLocalizedString (@" Show Even If Not Playing" , nil );
204+ }
205+
156206- (NSString *)determineDockIconMenuItemTitle
157207{
158208 return [self getDockIconVisibility ] ? NSLocalizedString(@" Hide Dock Icon" , nil ) : NSLocalizedString(@" Show Dock Icon" , nil );
0 commit comments