Q: Applescript Variable Error
Hi all,
I am trying to write an applescript that is called with a variable and then uses that variable in the script. For example:
on run argv set a to item 1 of argv tell application "iTerm-2" tell current window tell the current session write text "ssh" & a end tell end tell end tell end run
So the app would be called on the command line: open mypp.app --args machine. The app iTerm-2 would open and run the command "ssh machine". At the moment it launches iTerm-2 and runs the ssh command but ignores the variable. Am I called the variable of "a" incorrectly?
Thanks,
Sam
Posted on Jul 21, 2016 3:14 AM
This also works to get machine into your script. It will still work without the on run argv block. Use quotes for arguments containing white-space. You can adapt this to parse multiple arguments of different data types from a single command line.
-- reference: http://unixjunkie.blogspot.com/2006/07/command-line-processing-in-cocoa.html
-- reference: http://perspx.com/archives/parsing-command-line-arguments-nsuserdefaults/
-- Terminal invocation: open mypp.app --args -stringArg machine
use scripting additions
use framework "Foundation"
on run argv
set argx to current application's NSUserDefaults's standardUserDefaults()
set sarg to (argx's stringForKey:"stringArg") as text
if sarg is not "" then
display dialog sarg
end if
end run
Posted on Jul 21, 2016 9:24 AM