Self-Opening AppleScript Droplets
September 11th, 2005Most of the AppleScripts I use are “plain scripts.” That is, they require a host application in order to be run. AppleScript also supports the notion of a script application, which can optionally accept “dropped” items directly to the script’s icon. These script applications are commonly referred to as “droplets.”
Most of my droplets are “pure droplets.” By that I mean, they are completely useless to me unless I have actually dropped something on to them. Because of this, and because of the fact that I’m in the habit of double-clicking “plain scripts” in the Finder to bring them up in Script Editor, I often find myself accidentally double-clicking my droplet scripts in an attempt to edit them. This has the unfriendly result of my script simply complaining that it didn’t get anything dropped on it. I then have to go find the Script Editor and drag the droplet to it in order to edit it.
It occurred to me today that I could use the script itself to achieve the desired result. Instead of responding to a double-click launch with no “dropped items” by failing, why not just do what I want it do? I added code to the top of one of my pure droplets, which yields the desired effect:
tell application "Script Editor" activate open (path to me) end tell return on open (dropArgs) -- Droplet code goes here end open
Now when I double click the droplet, it kindly asks the Script Editor to open itself for editing. This extra code has no impact on the “useful” droplet code, because a droplet’s code is reached through an “on open” handler, which means the implicit “on run” code at the top-level of the script is never run except when the item double-clicked.
December 1st, 2005 at 12:51 pm
Very nice! Well done! ;-)