Seemingly identical blocks of iTunes scripts not producing same result

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

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!