Re: how to refer to environment variable in a Terminal do script?
Re: how to refer to environment variable in a Terminal do script?
- Subject: Re: how to refer to environment variable in a Terminal do script?
- From: "Mark J. Reed" <email@hidden>
- Date: Mon, 3 Dec 2007 11:17:46 -0500
On Dec 3, 2007 10:45 AM, Skeeve <email@hidden> wrote:
> a) No $ in export line
> b) No ' quotes around the $DISPLAY in the osascript line
>
> #!/bin/sh
> export DISPLAY=:1.0
> osascript -e 'tell app "Terminal" to activate' -e 'tell app
> "Terminal" to do script "DISPLAY='$DISPLAY' /path/to/toolToRun"
That's fine as long as the display value doesn' t have any spaces or
anything. Especially now that launchd is involved, I wouldn't want to
bet on it.
Let's work backwards from the desired shell command. Assuming we
don't know ahead of time what the value is, the only reliable way to
use it is to put quotes around it. We still have a choice of single
or double quotes, though. So we have one of these:
DISPLAY="someValue" /path/to/toolToRun
DISPLAY='someValue' /path/to/toolToRun
The main difference between the two is that the first one works when
the value is itself a variable in the shell, e.g.
DISPLAY="$myDisplay" /path/to/toolToRun
But if we could get a shell variable set to what we want, we wouldn't
need that in the first place; we could just skip to toolToRun.
OK, either of the above work great if you type them in Terminal. Now
you want to put it into a "do script" command, which itself has to be
in double quotes. So we need to either quote the quote:
tell application "Terminal" to do script "DISPLAY=\"someValue\"
/path/to/toolToRun
or use the other ones:
tell application "Terminal" to do script "DISPLAY='someValue'
/path/to/toolToRun"
That works from Script Editor, or a file run by osascript. But now you
want to put it into the argument to the osascript -e options, which
requires more quoting. And we're out of types of quotes. Safest is
single quotes again, but then we have to do something to quote the
single quotes we already have. There's no way to do exactly that -
single quoted strings are LITERAL, DAGNABIT! with no exceptions. But
since the shell lets you stick multiple quoted strings together any
which way you want, like this:
$ 'e'c"h"\o H\e"ll"o' 'world
Hello world
So all you have to do to get a single quote into a single-quoted
string is close the single quotes, add a literal single quote via \',
and then open the single quotes back up again. Which looks like this:
'It'\''s a long way to Tipperary'. Or this:
osascript -e 'tell application "Terminal" to do script
"DISPLAY='\''someValue'\'' /path/to/toolToRun" '
One last thing - we want to replace 'someValue' with the outer value
of $DISPLAY, which won't work inside single quotes. So for just those
two innermost single quotes directly around the display value, we
change them to double quotes:
osascript -e 'tell application "Terminal" to do script
"DISPLAY='\'"$DISPLAY"\'' /path/to/toolToRun" '
And that's your solution.
--
Mark J. Reed <email@hidden>
_______________________________________________
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