Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pause and play menu item #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions Statusfy/SFYAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down