3

Is there a way to be able to access the same function that the F7, F8 and F9 keys on the Apple Keyboard have in Swift for Mac OS X as I want to be able to create a button that will either pause the current playing track in Spotify or iTunes and then play it again.

  • Possible duplicate of [Control iTunes from a cocoa application](http://stackoverflow.com/questions/3452555/control-itunes-from-a-cocoa-application) – JAL Apr 14 '16 at 18:03

1 Answers1

0

You can use Applescript to do this, running the applescript from swift with NSAppleScript. Here's a quick and dirty example for toggling the play/pause state of the current track in spotify:

let command = "if application \"Spotify\" is running then tell application \"Spotify\" to playpause"
let commandObject = NSAppleScript(source: command)
var error: NSDictionary?
commandObject!.executeAndReturnError(&error)

You can read more information about Spotify's applescript API at https://developer.spotify.com/applescript-api/