0

Is it possible to access the song name of the currently playing song in the Spotify Mac app via Applescript? I would like to be able to copy the name of the currently playing song to my clipboard.

If not possible through Applescript how else could I do this?

truth1ness
  • 4,831
  • 5
  • 21
  • 19
  • [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – CJK Apr 30 '19 at 15:30

1 Answers1

0
tell application "Spotify"
    set nowplaying to the current track's name
end tell

set the clipboard to nowplaying

Even though set the clipboard is a Standard Additions command, ordinarily one ought to be able to combine the above into a single line:

tell app "Spotify" to set the clipboard to the current track's name

but this appeared not to work on my system. However, placing the clipboard command outwith the Spotify tell block works fine.

CJK
  • 5,732
  • 1
  • 8
  • 26
  • The second example can not work because "current track" is a property defined by the Spotify app, so it has no meaning outside of a ** tell application Spotify** block. – Ron Reuter May 03 '19 at 19:29
  • @RonReuter I’ve edited the answer to illustrate my point a bit more clearly. – CJK May 03 '19 at 19:44
  • 1
    @CJK, I do not have Spotify but curious to know if this works: `tell application "Spotify" to set the clipboard to (get name of current track)` Even if it does though, sometimes I try to keep some things from Standard Additions outside of another app's `tell` _block_. Usually the ones that throw a non-fatal error when run in Script Editor's Events pane. Like the one I mentioned to wch1zpink the other day, it had two non-fatal errors when run within app's `tell` _block_. Substituting "iTunes" in this _command_ worked for me without errors while a track was playing. – user3439894 May 03 '19 at 20:06
  • 1
    @user3439894 Just tested it and you’re right, using `get` does, indeed, work. I agree with you about trying to keep application and scripting additions commands separate where possible, as it seems like good practice to do so. But your point about non-fatal errors is further justification as every error thrown takes time for AppleScript to recover before continuing with execution and, in theory at least, reduces efficiency of a script. – CJK May 03 '19 at 20:27
  • 1
    Both of these variants work: `tell application "Spotify" to set the clipboard to (get name of the current track)` as @CJK and @user3439894 said, and `tell application "Spotify" to set the clipboard to (name of the current track as string)`. What's going on is that `name of the current track` is evaluated to an AppleScript _reference_, not a string. The interpreter needs additional syntax to tell it to resolve the reference and produce a string. In the first example, the `get` causes evaluation of the reference, and in the second example the `as string` causes the evaluation. – Ron Reuter May 04 '19 at 23:25