Applescript not working when trying to check if checkbox is checked

I have a button and a checkbox. When the button is pressed, it plays iTunes. That works great. I want the iTunes to delay playing for 5 seconds when the checkbox is checked. I can get a dialog to display when clicking the checkbox and setting a variable to 0 or 1, but when adding “if theValue is 1 then”, the applescript won’t work. When clicking the button, nothing happens.

When I remove the if statement, it works fine again. Can you help in fixing this? Below is my code.

on checkme_(sender)
  set theValue to sender's intValue() as text
  -- this returns either 0 or 1
end checkme_

    on clickedme_(sender)
        tell application "iTunes"
                    if theValue is 1 then
                    delay 5
                    end if
            play
            end tell
    end clickedme_

Model: iMac
Operating System: Mac OS X (10.6)

with your code the return value is "0’ or “1” (text) instead of 0 or 1 (integer).
But that’s not the major issue.
handlers like on clickedme() have their own scope, the variable theValue is visible only in the on checkme() handler

In your script it’s not necessary to save the state of the checkBox after clicking it.
Just check it right after clicking the button.
I’d also recommend to prefer the Cocoa delay method.

In Interface Builder bind the value of the checkbox to the class of the script with Model Key Path checkBoxState


property checkBoxState : missing value

on clickedme:sender
	if my checkBoxState as boolean then
		my performSelector:"playITunes" withObject:(missing value) afterDelay:5.0
	end if
end clickedme:

on playITunes()
	tell application "iTunes" to play
end playITunes

Thanks so much for your reply. It’s not exactly working, but I think that is because the checkbox doesn’t recognize the command. How should I ‘connect’ the checkbox with checkBoxState? In other words, where should I set the name?

Thank you!

In Xcode

¢ select the .xib file in the sidebar
¢ select the checkbox
¢ Press ⌥⌘7 to show the bindings inspector
¢ click on the disclosure triangle next to Value
¢ In the Popup menu “Bind to” select the class of the script
¢ In the text field Model Key Path write checkBoxState and press return
(make sure that the value line changes to Value ([AppleScript class].checkBoxState))