I have been trying to figure this out for a few days now and still cannot understand how this works.
I have created a new AppleScript Project in Xcode version 13.4.1 and added a CheckBox and a Button. My goal is to try and so something depending on whether the CheckBox is Ticked or Un-Ticked.
I am extremely new to Xcode so I am most likely doing something wrong. The reason for not showing real code is that I have nothing that works so I am asking if anyone can help me learn this by filling in the missing gaps.
The documentation for Xcode Applescript object seems thin on the ground to me or at least that's what I am finding.
Thank you
property checkBox : 1 -- bound to the value of the check box & is ticked when run
#START BUTTON
on startButtonClick_(sender)
-- if checkbox is ticked then show alert "ticked"
-- if checkbox is NOT ticked then show alert "NOT ticked"
end startButtonClick_
Update
It was suggested that including real code would be more useful which I do agree with so here is the latest attempt I have tried.
property checkBox : missing value -- bound to the value of the check box
#START BUTTON
on startButtonClick_(sender)
set checkBox to (checkBox as boolean) -- toggle
if checkBox is 1 then display alert "Ticked"
log checkBox
end startButtonClick_
The alert never shows but yet the log output shows 1 when the checkbox is ticked and 0 when its unticked when I press the button
CodePudding user response:
There are two issues here, depending on exactly what you're trying to do.
If you are trying to connect the checkbox action so that it calls a handler when you click on the checkbox, follow this procedure:
Make a handler in your script (in your case, the
startButtonClick_(sender)
handler)Open the xib file, select the First Responder object on the left
Go to the Attributes pane in the Inspectors area. You'll see a box labeled 'User Defined', so click the ' ' button to add a new user defined action.
- Use the name of your handler with a colon, not an underscore, e.g.: startButtonClick:
- Set the 'type' to NSButton (or I suppose you can use the default 'id')
Select the File's Owner object in IB, go to the Connections Inspector, and make sure the AppDlegate is connected as the File Owners delegate.
- It should be, but I tripped over that problem during testing, so...
Select the button-in-question in IB
Go to the Connections Inspector, and control-drag from the Sent Actions area to the First Responder, selecting your user-defined action (which should now appear in the popup menu).
Now when you run the app and click the checkbox, it will run the hander with a reference to the checkbox as its argument. You can query that using sender's state()
to get the current value of the button.
If you want a static, global reference to the button so you can check its state anywhere in the script, you may run into a problem. In Xcode 12 and earlier script properties would appear as outlets in the AppDelegate popup menu, so that you could connect GUI elements graphically. Xcode 13 has an as yet unresolved bug that makes this impossible, but you can work around it to an extent by editing the xib xml directly. Ive detailed how to do that in this post.
Hope this helps.