Saturday, October 27, 2007

An AppleScript studio application

AppleScript studio is kind of the Visual Basic of MacOS X. (I'm sure some people will correct me firmly on that statement).

In order to try it out under Leopard I picked a simple project to give me something to shoot for.

I have a little Yaesu FT-817 that supports a remote control serial protocol they call CAT. It's 4800 baud, 8 bit, 2 stop bits. Each packet is 5 bytes, the last byte being the command and the other bytes are the data to go with it.

My objective is the program shown above, it periodically retrieves the current frequency and mode from the transceiver and shows it.

To interface with a common PL-2303 USB serial device, I installed the excellent (and free) Serialport X scripting addition. Thanks Art Coughlin!

To install you create a folder called ~/Library/Scripting Additions/ and stick it in there.

Prototyping was done in straight AppleScript until I was able to read and display the frequency. Next to XCode where I created an "Applescript Application". It reminds me a bit of HyperCard, you open the .nib file and in the Applescript properties tab you can set a handler for a button to call an "on clicked theObject" method in your script for example.

After some frustrating messing about, I've achieved my objective. Once you've chosen the serial device an idle handler refreshes the frequency. 

To make the built application run on a machine without the Serialport X applescript already installed you need to bundle it in the application. Thanks to the instructions here I was able to automate this. Note that I needed a -r after CpMac to copy recursively.

I find the AppleScript language frustrating for reasons I can't fully fathom, this "Applescript for python programmers" table was a great help to me as I'm very comfortable with python.

For what it's worth, I've put my xcode project here, hope it helps someone.

4 comments:

has said...

If you're comfortable with Python you should definitely check out PyObjC and py2app. PyObjC is a much more powerful and flexible Cocoa bridge than Studio's AppleScriptKit. PyObjC 2.0 is already bundled in Leopard, and should also be available for separate download from the PyObjC homepage. py2app makes it easy to package Python-based projects as self-contained application bundles.

I've used them myself to write several applications (see my sig for some examples) and they're great pieces of kit. Totally recommend them.

If you also need to control other scriptable applications, which is usually the main reason for using AppleScript, I'd recommend checking out appscript. (Leopard does include its own scripting bridge too, of course, although from what I've seen it's not as good.)

HTH

has
--
http://appscript.sourceforge.net

marxy said...

Thanks has,

I like the ability to interact with applications but, like you, I'm very comfortable with python and would like to be able to use python for this.

Apple has moved a long way toward what we can see might be possible in Leopard but we aren't there yet. I note the language popup in the Applescript editor for example, with no other languages... yet.

best wishes,

marxy.

has said...

"""I note the language popup in the Applescript editor for example, with no other languages... yet."""

Yes, you need a full OSA language component in order to write scripts in Script Editor and attach them to applications that support this (Mail rules, folder actions, etc.). Unfortunately, while there are a number of very good third-party Apple event bridges now available - Mac::Glue, Python/Ruby/ObjC appscript - third-party OSA component support isn't nearly as far along (yet).

For basic tasks, you could try Philip Aker's OSA components (though I can't give you a link just now as his .Mac homepage appears to be down). These allow you to compile and execute batch-style Perl/Python/Ruby/Tcl/PHP scripts in OSA-aware applications. They don't support more advanced OSA features such as sending and receiving events, however, so while you may be able to use them for simple things such as iCal alarm scripts aren't suitable for more complex tasks such as folder actions.

There's also Late Night Software's JavaScriptOSA which provides much more extensive OSA support, but it's never been hugely popular and has a number of design flaws in its Apple event support that limit its usefulness there.

If you'd really like to try a full-fledged Python OSA component and aren't afraid of a little caveat emptor, there is a developer release of PyOSA on the appscript website. It supports both basic and advanced OSA operations and provides full integration with appscript, and should be quite usable in OSA-aware applications with care. See the PyOSA documentation for a list of current known bugs and restrictions.

HTH

Anonymous said...

You're welcome.


Art Coughlin