I’ve got an AppleScript to Swift bridge, with my applescripts being called through the Swift AppDelegate. The project is a controller for iTunes. Buttons (eg play/pause, fast forward, rewind) work but I’m not sure how to get the play/pause button to change state depending on the player state of iTunes (ie if iTunes is playing my app’s play/pause button should show the pause symbol). This should change not just when I click the button in the app but when I interact with just iTunes.
Asked
Active
Viewed 75 times
-5
-
Show. Your. Code. – matt Mar 15 '19 at 11:40
-
I don’t have any code to respond to the state of iTunes. That’s my problem! The code to do my other functions (in the AppleScript file, the bridge and the delegate) I don’t think will help – Jack Vanderpump Mar 15 '19 at 11:49
-
What have you got so far? In addition to a scripting dictionary property you can query for the player state, you can register an observer for iTunes distributed notifications to be notified when the player state or song info changes. – red_menace Mar 15 '19 at 12:40
-
I can get the player state from AppleScript by a simple tell. But this issue is how to get that to be constantly updating the UI rather than having to call it e.g. when I press a button. I want the button to change state as soon as I say press pause in iTunes. tell application "iTunes" if player state is playing then return "playing" else return "not playing" end if end tell – Jack Vanderpump Mar 15 '19 at 13:05
1 Answers
0
Set the initial state of the buttons or whatever and register an observer for iTunes distributed notifications:
let center = DistributedNotificationCenter.default()
center.addObserver(self,
selector:#selector(updatePlayerInfo),
name:NSNotification.Name("com.apple.iTunes.playerInfo"),
object:nil)
Then use the appropriate key(s) in the notification’s userInfo to update as you go along:
@objc func updatePlayerInfo(_ notification: Notification) {
print(notification.userInfo?["Player State"] as! String) // example
}
You can also add NSWorkspace observers to get notifications if iTunes is quit out from under you or whatever.

red_menace
- 3,162
- 2
- 10
- 18
-
That's worked perfectly for the playpause button, but how do you track changes to properties that don't result in a notification e.g. player position/time? I was thinking I'd just set the current time to reset with each change of song and every second change the UI, but that wouldn't take into account if the song is manually brought back five seconds by someone dragging the pin because no notification is emitted form iTunes. – Jack Vanderpump Mar 16 '19 at 16:54
-
There are properties in the userInfo dictionary other than play/paused, but iTunes only provides the notifications that it provides. For live updates of other properties without polling iTunes, your application would need to be what is playing the file, for example via the [Media Player framework](https://developer.apple.com/documentation/mediaplayer) (macOS Sierra and later). – red_menace Mar 16 '19 at 18:50