• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: using a shell variable in an osascript line?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Sun, 1 Mar 2009 11:29:13 -0500

On Sat, Feb 28, 2009 at 3:31 PM, LuKreme <email@hidden> wrote:
> #!/bin/sh
>
> # Is the current user running Mail?
> MAIL=`ps -U"$USER" -co command | grep Mail`
>
> #Get a random 'fortune' for signature
> RAND=`/opt/local/bin/fortune /path/to/mysigs`
>
> #write the fortune to .signature for slrn, Postbox, and Thunderbird's use
> echo $RAND > $HOME/.signature
>
> if [ $MAIL ]; then
>          osascript -e "set myVAR1 to (do shell script \"cat
> $HOME/.signature\") as text" \
>          -e "tell application \"Mail\" to set content of signature
> \"Fortune\" to \"-- \" & return &  myVAR1"
>

OK, osascript -e 'do shell script' is patently silly.  I can imagine -
barely - circumstances in which it might be called for, but this isn't
one of them.  First of all, AppleScript is perfectly capable of
reading the contents of text files without recourse to 'do shell
script "cat"'"

set myVAR1 to (read file (POSIX file ((system attribute "HOME") &
"/.signature"))) as text

 so even if the variable-passing techniques discussed in this thread
aren't suitable for some reason, there's always that.

I suspect that AS would be better at detecting whether Mail is
running, too, but in any case this line:

> if [ $MAIL ]

is going to be a syntax error if Mail isn't running, since (per
previous note) it expands to this:

> if [ ]

also, MAIL and RAND are poor choices of variable name as they have
predefined meanings to the shell:  MAIL is the location of your
mailbox (which the shell will check and display a "You have mail"
message right before each prompt), and RAND is a random number that
changes every time you read it.  In general it's a good idea to avoid
all-uppercase names except for variables that you're going to export
into the environment for some other program to access, which should
have some program-specific prefix to avoid naming conflicts.

So here's how I'd rewrite that script.

#!/bin/sh
myFortunes="$HOME/lib/fortune/sigs"
sigFile="$HOME/.signature"
/opt/local/bin/fortune "$myFortunes" >"$sigFile"
if ps x | grep -q 'M[a]il'; then
    osascript <<END_AS
       set sig to (read file (POSIX file "$sigFile"))
       tell application "Mail"
           set content of signature "Fortune" to ("--" & return & sig)
        end tell
END_AS
fi

Note that the shell here-document syntax (<<END_AS) expands shell
variables ($sigFile) inside the text, but doesn't treat quotation
marks as special, so those don't need to be backslashed.  Technically,
passing the filename as an Applescript literal like that goes against
my anti-code-generation philosophy, but since we're only passing the
filename rather than the contents, there's not likely to be quotation
marks or other troublesome characters in there.  Still, this version
uses the safer and more general technique:

#!/bin/sh
sigFile="$HOME/.signature"
/opt/local/bin/fortune >"$sigFile"
if ps x | grep -q 'M[a]il'; then
    osascript - "$sigFile" <<'END_AS'
       on run(argv)
         set sig to (read file (POSIX file (first item of argv as text)))
         tell application "Mail"
           set content of signature "Fortune" to ("--" & return & sig)
         end tell
      end run
END_AS
fi

Here, the quotes around the end-delimiter 'END_AS' prevent expansion
of anything inside the here-doc - it's all taken literally.

--
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

  • Follow-Ups:
    • Re: using a shell variable in an osascript line?
      • From: LuKreme <email@hidden>
  • Prev by Date: Trigger event on sound input level threshold?
  • Next by Date: Basic casting question
  • Previous by thread: Re: Trigger event on sound input level threshold?
  • Next by thread: Re: using a shell variable in an osascript line?
  • Index(es):
    • Date
    • Thread