Re: using a shell variable in an osascript line?
Re: using a shell variable in an osascript line?
- Subject: Re: using a shell variable in an osascript line?
- From: "Mark J. Reed" <email@hidden>
- Date: Fri, 27 Feb 2009 18:02:19 -0500
These sort of quoting problems come when you have to supply data in
the form of code; at that point you're really doing code generation,
and code generation is teh evil. (IMESHO, of course.)
The best solution is usually to supply the data through channels
intended for data instead of through channels intended for code. The
"on run(argv)" technique does that quite nicely for transient values,
and "system parameter" works well for values that are worth spending
more effort on "persisting" (albeit only in memory) into the
environment.
That concludes the AppleScript portion of this message. Please stop
reading if you don't care about shell scripts. Thank you.
Since I see lots of shell code being tossed around here, whether in do
shell script strings or Terminal do scripts or scripts that invoke
osascript, allow me to pass on a general tip: the best rule of thumb
is to ALWAYS quote parameter expansions. Somewhat like macros in C,
they are expanded *before* the shell does its normal compilation of
the command (word splitting). So this:
foo="1 2 3 4"
somecommand $foo
calls "somecommand" with four arguments, not one. This is sometimes
what you want, but frequently not. When I see bare $foo, I
instinctively reach for the keyboard to turn it into "$foo" instead.
:)
If you do want to expand something into a variable number of words,
you can use an array instead of a string, especially if you find
yourself trying to do yucky stuff like including quotes in the string
and eval'ing the result (see above re: code generation).
foo=(1 "2 3" 4)
somecommand "${foo[@]}" # somecommand is called with three arguments
Array variables are, sadly, not POSIX-compliant. If that's an issue
for you, one fallback is to use the shell's own positional parameters:
set -- 1 "2 3" 4
somecommand "$@" # calls somecommand with three arguments
..but that only works if you don't have data you care about already in
the positional params.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden