Pass parameters upon invocation of Applescript
I am writing an applescript to automate some iterm-related repetitive tasks.
I need to find out what is the appropriate way of accessing parameters from the script, say as in
on run {input, parameters}
set appName to "iTerm"
if application appName is not running then
tell application appName to activate
end if
tell application "iTerm"
tell current session of current tab of current window
write text "cd {param1}"
end tell
end tell
return input
end run
...where I should be able to perform (assuming the above is saved in myscript.scrpt
) sth like
./myscript.scrpt /path/to/somewhere
and the corresponding line would execute
write text "cd /path/to/somewhere"
Your run handler looks lifted from Automator, but your desire to have it as a script invoked from the shell suggests it's designed to be run independently of Automator.
Here's an edited form of the script that you can copy and paste into a terminal (like iTerm):
(:(){ paste - << SCRIPT > "$*" && chmod +x "$*"
#!/usr/bin/env osascript
on run {input} # Don't need the parameters arguments
# set appName to "iTerm"
# if application appName is not running then -- Not necessary since
# tell application appName to activate -- activate will open it
# end if
tell application "iTerm"
activate
tell current session of current tab of current window
write text "cd " & quoted form of the input
end tell
end tell
end run
SCRIPT
} ; : /path/to/my saved script )
This combines several tasks into one, namely creating a script file into which the applescript text is saved, and then made executable. Then to invoke:
"/path/to/my saved script" "/folder/path/parameter"
Although, if you're going to be running this from within the terminal anyway, I'm curious why you want to involve AppleScript at all. You can just do this:
cd () { command cd "$( eval echo "${*-$HOME}" )" ; }
This re-defines cd
so that doesn't need to quote the path they pass to it. It's just an example, and you can replace cd ()
with whatever name for a new function you wish to create, and insert whatever commands you want inside {...}
.