View Full Version : AppleScript and Xcode 4?

Dec 16, 2011, 09:26 AM
Hey all,

Im new to programming but I have played around with Xcode 3 before and found it ok. However in Xcode 4 I can't find a way to link my "push buttons" to an AppleScript. In Xcode 3 I would do this


But I can't find the equivalent in Xcode 4.

Anyone know were it is?


Dec 16, 2011, 11:10 AM
Right click on your button in inteface builder, and drag a line from there to your App delegate in IB's 'Objects' list. That'll bring up a selectable list of your functions.
Apple's Xcode 4 transition guide is well worth a read.

Red Menace
Dec 16, 2011, 07:02 PM
AppleScript Studio (which looks like what you are talking about) was deprecated in Snow Leopard, and is gone completely in Lion - AppleScriptObjC is the framework used in Xcode 4.

Dec 16, 2011, 09:33 PM
Thanks for your replies,

So can i easily link an appleScript such as this

tell application "Google Earth"
end tell

To a push button in Xcode 4 or not?

Dec 16, 2011, 10:36 PM
It's very easy, just a little obscure if you've not seen it done or read about how to do it.

Dec 16, 2011, 10:46 PM
Im sorry guys I'm just not getting it. Any chance someone could take some screen shots of the process? It would be much appreciated :)

Red Menace
Dec 17, 2011, 02:21 PM
You can use the shortcut mentioned by Partron22, but I think an easier way to visualize would be to use the Connections Inspector to look at what is available in the App Delegate script. After you have added property and handler definitions to your script (see the AppleScriptObjC Release Notes (http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html) for naming conventions), they will show up in the Outlets and Received Actions areas, respectively . From there, you make a connection by dragging from the connector beside desired action to your button - once connected, when the button is pressed that handler will be called (with the particular button passed as a parameter). For an example, in the following screen shot I am connecting a button to the myButtonHandler_ handler in my script:

on myButtonHandler_(sender)
tell application "Google Earth"
end tell
end doMyButton_


Dec 18, 2011, 08:13 AM
Thanks Red Menace,

I spent some time today trying to do this but I was running in to the problem that I could not get my applescripts in to the "Received Actions" menu.

How do I do this?


Dec 18, 2011, 12:07 PM
I was running in to the problem that I could not get my applescripts in to the "Received Actions" menu.

Your functions need to take an argument.
It's usually called 'sender'.
It contains info on who called the function (in this case, your button)

So your function declations need to look like:
on doButtonStuff_(sender)
end doButtonStuff_
That _ is important. It tells ASOC that the function takes 1 argument.
Your linked function MUST take that 1 argument.
Without the _, your function won't show up in Appdelegate's connection Inspector pane.

Red Menace
Dec 18, 2011, 12:12 PM
The UI Editor will use any handler (method) that has a single parameter (which means a single trailing underscore) as an action, like the example in my last post. Also, make sure to save your script after making changes.