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: Axel Luttgens <email@hidden>
- Date: Fri, 27 Feb 2009 10:40:42 +0100
Le 27 févr. 09 à 07:43, LuKreme a écrit :
I have a shell variable that I set to the output of a shell command
VAR1=`tail -1 /var/log/maillog`
and then I use VAR1 in the shell script for various things.
Then, at some point, I want to use that VAR1 in an osascript line,
maybe something like:
osascript -e "display dialog $VAR1"
this doesn't work, however. the variable is transient in nature, so
I can't simply redo the assignment as a do shell script.
Do I have to do
osascript -e "display dialog (do shell script \"echo $VAR1\")"
which while it works, is fugly to behold, or is there a better
option I'm missing?
If I understood you correctly, you have a single shell script with
multiple statements; among those statements, two are of interest here:
[...]
VAR1=`tail -1 /var/log/mail.log`
[...]
osascript -e "display dialog $VAR1"
[...]
The first statement will set the value of shell variable VAR1 to some
string, say:
Jul 25 11:08:32 mybox postfix/postqueue[13118]: fatal: Queue report
unavailable - mail system is down
When encountering the second of above statements, the shell will
interpret "osascript" as being the name of the command, and first
build the list of arguments to be passed to that command; it will
consider the remainder of the line
-e "display dialog $VAR1"
and proceed by applying various rules (shell expansion).
There are two syntactic chunks: -e and "display dialog $VAR1".
The first one is just viewed as a constant, and will be passed "as is"
as the first argument of osascript.
The second one is a quoted string; the shell will thus look inside the
string so as to decide whether other expansions are needed. And yes,
there is one: replace $VAR1 by the contents of VAR1.
As a result, one is invoking osascript with two arguments:
-e
and:
display dialog Jul 25 11:08:32 mybox postfix/postqueue[13118]: fatal:
Queue report unavailable - mail system is down
One is thus trying to execute following AppleScript statement:
display dialog Jul 25 11:08:32 mybox postfix/postqueue[13118]: fatal:
Queue report unavailable - mail system is down
Clearly, this is an invalid AppleScript statement; this is the one we
want:
display dialog "Jul 25 11:08:32 mybox postfix/postqueue[13118]:
fatal: Queue report unavailable - mail system is down"
Assuming the string in VAR1 will never contain double quotes, one may
ask the shell to create a string with the needed quotes:
osascript -e "display dialog \"$VAR1\""
We are thus using escape mechanisms at the shell level, so that the
shell builds a syntactically correct string from AppleScript's point
of view.
But there's still a problem left; depending on the OS version, above
statement may fail because it involves "user interaction". One could
then try something like:
osascript -e "tell application \"Finder\" to display dialog \"$VAR1\""
HTH,
Axel _______________________________________________
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