Spotify's “player state” is available in the e

2019-07-05 22:07发布

问题:

Here's a testing code:

tell application "Spotify"
    set playerState to player state as string
end tell
display dialog playerState

Works fine from the AppleScript editor. However, when I export my script as an app, all I get is this:

Why is this happening?

回答1:

It seems that Spotify is not coercing the constant into a string. Since the editor can't coerce it from an applet as it does when you are running the script in AppleScript Editor, the four-letter constant code is returned. Since you can't test the player state's value as a string, try to test it against the constants themselves.

property spotPause : «constant ****kPSp»
property spotPlay : «constant ****kPSP»

tell application "Spotify" to set playerState to player state

if playerState = spotPause then
    display dialog "paused"
else if playerState = spotPlay then
    display dialog "playing"
end if


回答2:

It's better to use this code to obtain the player state. You don't need to know the exact constant values. Works on OS X 10.13

tell application "Spotify"
    if player state is playing then
        display dialog "Player running"
    else if player state is paused then
        display dialog "Player paused"
    else if player is stopped then
        display dialog "Player is stopped"
    else
        display dialog "Unknow state"
    end if
end tell