Archive for the 'Applescript' Category

[AS] Quickies fixing problems with script bundles

Monday, January 22nd, 2007

I’m sure someone at Apple knows what to do with script bundles but I’m at a loss for them. I spent a couple of hours today trying to get something working with them but it just doesn’t happen. The problem is that scripts run from within Script Editor have the me global set to Script Editor. This is a problem when you’re adding things to a script bundle. path to resource won’t work because the script will be looking in Script Editor instead of your script. Notably, when run using osascript or NSAppleScript, the me global is correctly set to the path to the script. So here’s a little deal to grab a corrected resource:

if name of me is "Script Editor" then
    set x to path of first document of me as POSIX file
else
    set x to path to me
end if
path to resource "description.rtfd" in bundle x

There are also problems loading scripts that are bundles. Simply put, load script can’t open file packages. It can, however, open aliases. Here’s how to load script bundles:

tell application "System Events"
    set p to file package "Example.scptd" of desktop folder
end tell
set s to load script (path of p as alias)

Unfortunately, you cannot combine the two methods. The global variable me then either refers to Script Editor (and the frontmost document won’t be the script bundle but the script you’re working on and the above fix won’t work) or, if you save the script as an application and run it, me will refer to the calling script, not the script bundle.

(* Example.scptd *)
on run
    return POSIX path of (path to me)
end run

(* Breakage.scpt *)
tell application "System Events"
    set p to file package "Example.scptd" of desktop folder
end tell
set s to load script (path of p as alias)
run of s

-- Result of running Breakage.scpt in Script Editor
"/Applications/AppleScript/Script Editor.app/"

-- Result of saving Breakage.scpt as an app and running Breakage.app
"/Users/ghansard/Desktop/Breakage.app"

As you can see, the me variable in Example.scptd doesn’t point to the script. This is painful and annoying. Does anyone know a workaround or fix? Are there any script bundle experts that can lend a hand?

[AS] Simple XMLRPC calls

Thursday, January 4th, 2007

Applescript’s really nifty trick is the ability to tie applications together. It also can tie internet applications together. No, seriously. Applescript can interact with internet applications with XMLRPC/SOAP protocols. There’s a few caveats but it’s actually quite simple. An example with notes are in the extended section. (more…)

[AS] Database Events

Sunday, November 26th, 2006

Database Events is, in my opinion, although with respect for whatever Apple engineer wrote it, absolutely useless and one of the dumbest additions to Applescript. Don’t use it. Period. There’s almost always a better solution to whatever problem you’re trying to solve.

  • Need to work with SQLite databases? Look somewhere else. While the files Applescript uses are SQLite, Database Events does not appear to be able to read any SQLite documents other than the ones it creates. Wrap up the SQLite command line application

  • Need to save data between sessions? Consider property list files (my post about them available somewhere else) or a script object (information available in a future post, keep an eye on this blog). Need to save a lot of data between sessions? Consider wrapping SQLite.

  • Considering Database Events for any other purpose? Don’t.

Still, I haven’t seen anything online that demonstrates working with Database Events so the extended section will contain some code. (more…)

[AS] Load script

Tuesday, November 14th, 2006

A woefully underutilized feature of Applescript is its ability to load other scripts on the fly. Many people don’t realize that Applescript can do this. It makes it easy to reuse useful code and isn’t difficult to use itself. (more…)

[AS] Image Events

Sunday, October 29th, 2006

Working with images got a lot easier in Applescript with OS X 10.4. Apple included an Image Events suite that lets you do basic image editing right within Applescript. Image Events lets you do things like scale, rotate, crop, and flip images with ease. (more…)