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

Add a play and pause button when clicking on the menu bar item #10

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
28 changes: 19 additions & 9 deletions SpotStatus/AppDelegate.swift
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
//
// AppDelegate.swift
// SpotStatus
//
// Created by Josh Spicer <https://joshspicer.com/>. (c) 2019 - All rights reserved.
//

import Cocoa
import Foundation


enum SongInfoToShow {
case title, artist
}
Expand Down Expand Up @@ -51,6 +43,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {

"""

let playPauseScript = """
if application "Spotify" is running then
tell application "Spotify"
playpause
end tell
end if
"""

let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.variableLength)
var songName: String!
var moreDetail: String!
Expand All @@ -76,10 +76,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
pasteboard.setString(url, forType: NSPasteboard.PasteboardType.string)
}

@objc func playPauseSong(_ sender: Any?) {
if let scriptObject = NSAppleScript(source: playPauseScript) {
var errorDict: NSDictionary? = nil
scriptObject.executeAndReturnError(&errorDict)
if let error = errorDict {
print(error)
}
}
}

// Generates the dropdown menu
func constructMenu() {
let menu = NSMenu()

menu.addItem(NSMenuItem(title: "Play/Pause", action: #selector(playPauseSong(_:)), keyEquivalent: "p"))
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "Preferences", action: #selector(showPrefs(_:)), keyEquivalent: "m"))
menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
Expand Down Expand Up @@ -238,4 +249,3 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationWillTerminate(_ aNotification: Notification) { }
}