Re: newbie question: AppleScript and hashbang
Re: newbie question: AppleScript and hashbang
- Subject: Re: newbie question: AppleScript and hashbang
- From: Peter Vandenberk <email@hidden>
- Date: Tue, 25 Apr 2006 10:01:07 +0100
On Apr 24, 2006, at 8:21 AM, Christopher Nebel wrote:
Well, you could do something like this:
#!/bin/sh
sed -e '1,4d' $0 | osascript
exit 0
# Script begins after this line.
"hello" & " " & "world"
Here-documents usually work pretty well, though.
Thanks for your reply, Christopher. Yes, that'd work, as does - as
you point out - the here-document approach, which is described on the
article I linked to earlier:
http://www.macosxhints.com/article.php?story=20040617170055379
... and which typically looks something like:
#!/bin/sh
osascript <<EOF
"hello" & " " & "world"
But as I argue in a reply I posted on macosxhints (reply to the "here
document" approach discussed in the referenced thread, but applies
equally to the "sed"-based solution offered in this thread):
<snippet>
Although the "here document" workaround definitely has it uses, it's
not exactly a replacement for "the real thing", though: if '#' was
recognised as a token at the start of single-line comments in
AppleScript, then the above could be written as:
#!/usr/bin/osascript
tell app "Safari"
activate
end tell
The nice thing about this script is that it would be valid
AppleScript, meaning you would still be able to edit, run and debug
this script in the "Script Editor" (because the shebang line would be
parsed as a single-line comment) but at the same time it would be an
executable shell script as well.
You basically get the best of both worlds: it's a proper AppleScript,
which you can run, compile, install etc like any old AppleScript, but
at the same time it is a self-contained, executable shell script,
that can be integrated with other scripts - possibly in other
scripting languages like sh, perl, ruby etc - and that can also be
integrated with other Unix facilities - such as cron for instance.
Compare this to the "here document" way of doing things:
#!/bin/sh
osascript <<\EOF
tell app "Safari"
activate
end tell
... which is not exactly the same: this is indeed an executable shell
script, but it is no longer a proper AppleScript, meaning you loose
all the benefits mentioned above. For example, you wouldn't be able
to edit and run this "here document" script in "Script Editor"!!!
As mentioned earlier, I'm new to AppleScript, so maybe I'm missing
something here, but I would have thought it to be very easy to make
AppleScript treat '#' as it treats '--' ie. as a token to identify
single-line comments?!? This seems like a pretty simple change to
make to the AppleScript language & supporting tools, and would allow
for elegant shebang support in osascript.
</snippet>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden