From de4d215bef6076d7d98756d912438855c80f91b3 Mon Sep 17 00:00:00 2001 From: Blobtoe Date: Tue, 4 Sep 2018 22:59:01 -0700 Subject: [PATCH] Added pause and play menu item --- Statusfy/SFYAppDelegate.m | 45 +++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/Statusfy/SFYAppDelegate.m b/Statusfy/SFYAppDelegate.m index 6775ba2..5f2a04b 100644 --- a/Statusfy/SFYAppDelegate.m +++ b/Statusfy/SFYAppDelegate.m @@ -16,6 +16,7 @@ @interface SFYAppDelegate () @property (nonatomic, strong) NSMenuItem *playerStateMenuItem; @property (nonatomic, strong) NSMenuItem *dockIconMenuItem; +@property (nonatomic, strong) NSMenuItem *togglePlayingMenuItem; @property (nonatomic, strong) NSStatusItem *statusItem; @end @@ -36,8 +37,11 @@ - (void)applicationDidFinishLaunching:(NSNotification * __unused)aNotification self.dockIconMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Hide Dock Icon", nil) action:@selector(toggleDockIconVisibility) keyEquivalent:@""]; + self.togglePlayingMenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Pause", nil) action:@selector(togglePlaying) keyEquivalent:@""]; + [menu addItem:self.playerStateMenuItem]; [menu addItem:self.dockIconMenuItem]; + [menu addItem:self.togglePlayingMenuItem]; [menu addItemWithTitle:NSLocalizedString(@"Quit", nil) action:@selector(quit) keyEquivalent:@"q"]; [self.statusItem setMenu:menu]; @@ -54,21 +58,26 @@ - (void)setStatusItemTitle NSString *artistName = [[self executeAppleScript:@"get artist of current track"] stringValue]; if (trackName && artistName) { + self.togglePlayingMenuItem.hidden = false; NSString *titleText = [NSString stringWithFormat:@"%@ - %@", trackName, artistName]; if ([self getPlayerStateVisibility]) { - NSString *playerState = [self determinePlayerStateText]; + NSString *playerState = [self determinePlayerStateText:@"player"]; titleText = [NSString stringWithFormat:@"%@ (%@)", titleText, playerState]; } self.statusItem.image = nil; self.statusItem.title = titleText; + + NSString *playingText = [self determinePlayerStateText:@"playing"]; + self.togglePlayingMenuItem.title = playingText; } else { NSImage *image = [NSImage imageNamed:@"status_icon"]; [image setTemplate:true]; self.statusItem.image = image; self.statusItem.title = nil; + self.togglePlayingMenuItem.hidden = true; } } @@ -105,19 +114,31 @@ - (NSString *)determinePlayerStateMenuItemTitle return [self getPlayerStateVisibility] ? NSLocalizedString(@"Hide Player State", nil) : NSLocalizedString(@"Show Player State", nil); } -- (NSString *)determinePlayerStateText +- (NSString *)determinePlayerStateText:(NSString*) use { NSString *playerStateText = nil; NSString *playerStateConstant = [[self executeAppleScript:@"get player state"] stringValue]; - if ([playerStateConstant isEqualToString:@"kPSP"]) { - playerStateText = NSLocalizedString(@"Playing", nil); - } - else if ([playerStateConstant isEqualToString:@"kPSp"]) { - playerStateText = NSLocalizedString(@"Paused", nil); + if ([use isEqual:@"player"]) + { + if ([playerStateConstant isEqualToString:@"kPSP"]) { + playerStateText = NSLocalizedString(@"Playing", nil); + } + else if ([playerStateConstant isEqualToString:@"kPSp"]) { + playerStateText = NSLocalizedString(@"Paused", nil); + } + else { + playerStateText = NSLocalizedString(@"Stopped", nil); + } } - else { - playerStateText = NSLocalizedString(@"Stopped", nil); + else if ([use isEqual:@"playing"]) + { + if ([playerStateConstant isEqualToString:@"kPSP"]) { + playerStateText = NSLocalizedString(@"Pause", nil); + } + else if ([playerStateConstant isEqualToString:@"kPSp"]) { + playerStateText = NSLocalizedString(@"Play", nil); + } } return playerStateText; @@ -153,6 +174,12 @@ - (void)toggleDockIconVisibility } } +-(void) togglePlaying +{ + NSString* state = [self determinePlayerStateText:@"playing"]; + [[self executeAppleScript:[NSString stringWithFormat: @"%@ current track", state]] stringValue]; +} + - (NSString *)determineDockIconMenuItemTitle { return [self getDockIconVisibility] ? NSLocalizedString(@"Hide Dock Icon", nil) : NSLocalizedString(@"Show Dock Icon", nil);