AppleScriptObjC : The new ASS

Discussion in 'Mac Programming' started by robbieduncan, Jan 7, 2010.

  1. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #1
    So it seems that as-of a recent version of XCode or perhaps OSX 10.6 AppleScript Studio (ASS) is no longer available. Instead there is a new thing called AppleScriptObjC that lets you write classes in AppleScript and use all of the Cocoa framework from AppleScript.

    I've written a small app using this (see this thread).

    I though that I'd attach the project here in case it helps anyone else learn how to use this new feature in XCode.

    Things to notice. You can declare IBOutlets like this:

    Code:
    property myOutlet : missing value
    Basically any property with a starting value of "missing value" will be treated as an IBOutlet by Interface Builder. Note that as AppleScript is not typed in the same was as Objective-C we don't need to declare types.

    IBActions look like this:

    Code:
    on start_(sender)
    <code goes here>
    end start_
    Note the _ in the name? This is a critical part of the bridge. This tells the compiler/bridge that this can be called from Obj-C and that there is a parameter. If we want methods with multiple parameters we can have them like:
    Code:
    on myMethod_withTwoParams_(param1,param2)
    We can also call into Objective-C/use Objective-C objects from our AppleScript. If we have a property called myButton that we've connected in IB to a NSButton then this would make that button disabled:

    Code:
    property myButton : missing value
    
    ...
    
    tell myButton to setEnabled_(false)
    
    Note the _ again. This is because we are calling setEnabled: on the UIControl.

    We can also call Objective-C methods with a different syntax. It looks like this:

    Code:
    myObject's method_(param)
    
    The important part here is the "'s". The variable/property is called myObject. The 's tells the system we are calling a method.

    Note that as the object that your code is in is a NSObject you can call methods on the object itself that are part of the NSObject protocol. So you can do this to run a method with a delay:

    Code:
    my performSelector_withObject_afterDelay_("loop", missing value, 1.0)
    
    This will call the AppleScript "method" loop after 1.0 seconds and not repeat. Note that this loop was declared as:

    Code:
    on loop
    <code here>
    end loop
    Note the lack of _

    And that's about all I know! Is anyone else using this?
     

    Attached Files:

  2. dylanweber macrumors newbie

    Joined:
    Jul 14, 2009
    Location:
    USA
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    I guess it's good if you're writing an app that mainly deals with scripting, and you need some basic UI work, but, ASS was nice because it was fairly simple and straightforward for scripters. However, AppleScriptObjC requires you know Cocoa, and people familiar with Cocoa can already use Scripting Bridge, so I'm not sure who the target audience is.

    Plus, Apple seems to have a problem maintaining support for non-Obj-C languages. Java got killed, and SL doesn't have a project template for Ruby/Python anymore, so I'd be hesitant to put any long-term investments in this.
     
  4. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #4
    Note that the bridges are still there nice and fine, it is just the templates that went away. I filed a bug on that when I discovered that in the seeding process, and got a "as expected" response with a note that you can still use the templates from older versions of XCode (this works fine I can verify).
     
  5. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #5
    ASS was a quick-n-dirty low-budget lashup to Xcode with a lot of frustrating limitations. I wouldn't call it 'nice', but it was free and mostly usable, i.e. adequate. FaceSpan was a much, much better toolkit, but eventually discontinued for other reasons.

    ASOC is a proper bridge, though until/unless Apple or the AppleScript community write some high-level wrappers, c.f. MacRuby's HotCocoa library, then users do, as you say, have to have a good understanding of Cocoa proper to use it.

    Alternatively, there are other simpler, though much less powerful options for knocking up cheap and cheerful solutions to some types of tasks, e.g. CocoaDialog, Platypus, Smile.

    The more advanced AppleScript users, basically.

    That said, it may have some appeal to other developers who need to do a significant amount of application automation. Scripting Bridge is less capable than AppleScript and much more prone to application compatibility problems; plus its documentation and tool support suck. Not exactly an ideal replacement for AS. ObjC-appscript is significantly better than SB, incidentally, though even appscript isn't 100% compatible with the huge spread of scriptable applications out there.

    In both cases, it's actually somewhat unpleasant to write large amounts of application scripting code in ObjC using either of these bridges, due to the verbosity of the APIs and the quantity of chained method calls involved (which ObjC syntax is the world's worst for). So if you have a solid mass of application scripting-specific code to do within a larger ObjC-based application, you may find it easier to implement that portion in AS with ASOC providing the necessary bridging to the rest of your app.

    Plus there's the usual speed-of-development arguments for very-high-level interpreted languages over a not-so-high-level compiled language such as ObjC, although AppleScript probably isn't the best example for making that case.


    JavaCocoa got killed for the same reason ASS is on its way out: it was a hand-tailored bridge between two very disparate systems, which meant the cost of developing and maintaining it and the difficulty of making it both capable and reliable exceeded the benefits it provided and the number of users it attracted.

    Apple dropped the PyObjC and RubyCocoa project templates from Xcode as they were a hassle for them to maintain. Considering these templates are crap anyway, can't say I miss 'em myself. If you want them, they're easy enough to get from the PyObjC and RubyCocoa websites.

    As for PyObjC and RubyCocoa themselves, both are open-source, so even if Apple someday decided not to bundle them in the OS, it would make very little difference to application developers using those bridges (it just means you have to include the bridge framework in your distribution, which many apps do anyway for portability reasons).

    PyObjC is quietly trundling along as it always does, and I believe a Python 3 version is in the works. (Personally I'm really looking forward to this as I've got a large Python+PyObjC+appscript system that I'm currently transitioning to Python 3 for its superior Unicode support.)

    RubyCocoa probably doesn't have a long-term future as it is in the process of being superceded by the upcoming MacRuby. MacRuby should eventually rock for Cocoa development as it's a Ruby interpreter that runs directly on top of ObjC rather than a bridge between separate Ruby and ObjC runtimes as RubyCocoa is. That doesn't stop you developing with RubyCocoa for now and eventually switching to MacRuby. Incidentally there's a book by Brian Marick on developing with RubyCocoa that you could take a look at if you were considering going down that route.

    HTH
     
  6. robj macrumors regular

    Joined:
    Dec 23, 2009
    Location:
    Madrid, Spain
    #6
    Applescript Studio comes with latest Xcode but it's hidden as it's deprecated.

    Your solution is better because AppleScriptObj is the new standard to follow, but if you need to use ASS again, you can re-enable it typing this in the terminal

    Code:
    defaults write com.apple.InterfaceBuilder3 IBEnableAppleScriptStudioSupport -bool YES
     
  7. bsk macrumors newbie

    Joined:
    Apr 13, 2010
    Location:
    Bristol, UK
    #7
    Both or either/or

    If I did this would I lose the ability to use ASOC if I wanted?
     
  8. robbieduncan thread starter Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    I don't think so. You an always reverse the command by replacing YES with NO.
     

Share This Page