[AS] Quickies fixing problems with script bundles
Monday, January 22nd, 2007I’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?