I have this code as a standalone script that works perfectly when run:
tell application "iTunes"
playpause
set status to (get player state) as text
end tell
display dialog status
I then put that into and if block in another script here:
else if (currentitem is "pause") or (currentitem is "play") then
if appIsRunning("iTunes") then
tell application "iTunes"
playpause
set status to (get player state) as text
end tell
display dialog status -- check to see if status is correct, it's not!!!!!
set reply's end to {status & ", "}
else
set reply's end to {"Unable to pause/play, "}
end if
And I donāt know what the problem is, but for some reason instead of setting the status to āplayingā or āpausedā it is giving me āĀ«constant ****kPSPĀ»ā and āĀ«constant ****kPSpĀ»ā in the dialog box! The dialog box is only there for debugging purposes, to find out where the result is going bad.
Why is my script not returning the proper value for the player state here, when if I take the code out of the if block, it works just fine?
Hi,
those player state values are enumerated constants, which could behave weird in some cases.
A more reliable way is to use boolean values for example
tell application "iTunes"
set isPlaying to player state is playing
end tell
or if you need all different states, something like this
tell application "iTunes"
set status to ((player state is fast forwarding) as integer) * 16 + ((player state is paused) as integer) * 8 + ((player state is playing) as integer) * 4 + ((player state is rewinding) as integer) * 2 + ((player state is stopped) as integer)
end tell
if status is 16 then
display dialog "fast forwarding"
else if status is 8 then
display dialog "paused"
else if status is 4 then
display dialog "playing"
else if status is 2 then
display dialog "rewinding"
else if status is 1 then
display dialog "stopped"
end if
1 Like
Thanks stefan!
That worked well! I still donāt totally understand why the method before stopped working (at one point, it worked just fine!)
Thanks for the advice though, I changed all my scripts using the state command to use a boolean result instead of text - everything is working great!
Hello @StefanK. I just signed up to thank you! Your answer also helps with Spotify.
tell application "Spotify"
set spotify_player_state to Ā¬
((player state is playing) as integer) * 1 + Ā¬
((player state is stopped) as integer) * 2 + Ā¬
((player state is paused) as integer) * 3
end tell
if spotify_player_state is 1 then
display dialog "playing"
else if spotify_player_state is 2 then
display dialog "stopped"
else if spotify_player_state is 3 then
display dialog "paused"
end if
In your original script you have this line
set status to (get player state) as text
no need to use āgetā, just do thisā¦
set status to player state as text