AppleScript and Xcode 4?

Discussion in 'Mac Programming' started by wrignj08, Dec 16, 2011.

  1. macrumors newbie

    Joined:
    Jun 13, 2009
    #1
    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

    [​IMG]

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

    Anyone know were it is?

    Thanks
     
  2. macrumors 65816

    Partron22

    Joined:
    Apr 13, 2011
    #2
    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.
     
  3. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Jun 13, 2009
    #4
    Thanks for your replies,

    So can i easily link an appleScript such as this

    tell application "Google Earth"
    activate
    end tell

    To a push button in Xcode 4 or not?
     
  5. macrumors 65816

    Partron22

    Joined:
    Apr 13, 2011
    #5
    It's very easy, just a little obscure if you've not seen it done or read about how to do it.
     
  6. wrignj08, Dec 16, 2011
    Last edited: Dec 16, 2011

    thread starter macrumors newbie

    Joined:
    Jun 13, 2009
    #6
    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 :)
     
  7. Red Menace, Dec 17, 2011
    Last edited by a moderator: Dec 17, 2011

    macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #7
    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 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:

    Code:
        on myButtonHandler_(sender)
            tell application "Google Earth"
                activate
            end tell
        end doMyButton_
    [​IMG]
     
  8. thread starter macrumors newbie

    Joined:
    Jun 13, 2009
    #8
    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?

    Thanks
     
  9. macrumors 65816

    Partron22

    Joined:
    Apr 13, 2011
    #9
    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:
    Code:
    on doButtonStuff_(sender)
    	beep()
    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.
     
  10. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #10
    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.
     

Share This Page