How to get currently playing track in Spotify Mac

2019-06-07 18:25发布

问题:

I'm developing a lyric application for Mac and I want to add Spotify support to my app. I just need to retrieve currently playing track on spotify client for Mac and get notified if playing state has changed.

is there any API to use in my app for doing this?

回答1:

To get notified when playing state has changed (which tells you the new track), you watch for a distributed notification called "com.spotify.client.PlaybackStateChanged". There are at least two other questions here asking how to do that, one of which has sample code, so try searching for that. (Or just fire up Notification Watcher and you can see them for yourself, and it should be easy to figure out how to write the code to handle them.)

If you want to check the current track manually (e.g., at startup), you can use the Spotify AppleScript interface (which of course you can also access via ScriptingBridge, appscript, etc.), like this:

tell application "Spotify"
  name of current track
end tell

There are similar properties for the artist, etc. Fire up AppleScript Editor and open the Spotify Dictionary for full details.