Stairways Software Icon

Stairways Software

Excellence In Software For Over Ten Years – Only with a Mac

Passing parameters to AppleScripts

There may be times when you want to perform a variety of related actions using a single AppleScript from multiple macros.

For example, you might have an AppleScript that moves an object triggered by the four arrow keys, with modifiers to specify how much, and so you need to pass the direction and distance to the AppleScript.

Kevin van Haaren recently posted to the Keyboard Maestro Users Group explaining how to easily do this and graciously allowed me to post the details here. Basically, you use the Execute -> Unix Script action to run osascript with the arguments.

In this case, the script for the action might be

osascript ~/Applications/move_object.scpt UP 10

And the AppleScript code would be:

on run argv
    set theDirection to (item 1 of argv)
    set theAmount to (item 2 of argv)
    -- your script using the parameters follows
end run

Note that Kevin ran in to an interesting AppleScript issue where if you executed the script in rapid succession, errors occur as AppleScript opens the script for writing each time to allow it to update the properties. A simple workaround for this is to use plain text for the script and have it compiled each time (there is a performance hit the first time, but it is quite quick after that).

Of course, an alternative would be to just use Execute -> AppleScript text and paste the adjusted script into each macro, but that would mean any change to the script needs to be tediously updated for every macro. Still, this may be another easy way for scripts that rarely change.

My thanks to Kevin for sharing this useful technique.

Posted Sunday, July 27, 2008. Permalink. Post a Comment.


Post Comment

Name: (optional)
URL: (optional)
Email: (optional, not published)
CAPTCHA: one seven three eight (required)
  To prove you are human, please enter the number as digits (ie, 1-9).
  You can use some HTML tags such as <b>, <i>, and <a href="">.
We reserve the right to remove any offensive or inappropriate comments.
Due to spam issues, comments are initially invisible until we review them,
you can see them (background red) and we can see them, but no one else.

Comment Preview

None yet.

Buy Now

User Database