Snip
|
In unix, most scripting languages accept line-initial # ...
|
---|
Categories |
|
---|
For Snip |
loading snip actions ... |
---|---|
For Page |
loading url actions ... |
However, there is a convention in unix that executable files that do not have a recognizable header (of which #! is one) are passed to /bin/sh for execution. This allows considerable flexibility for use with AppleScript. The most basic idea is to use a first line containing:
exec osascript <<\EOF
and then follow it with an arbitrary AppleScript. Voila, a seemingly direct execution of an AppleScript file. Trivial example:
exec osascript <<\EOF
tell app "Safari"
activate
end tell
Now, it is difficult with osascript to pass command-line parameters to the AppleScript. exec osascript <<EOF
tell app "Safari"
activate
if not "$1" = "" then
if not (exists document 1) then
make new document at the beginning of documents
end if
set the url of the front document to "$1"
end if
end tell
This yields a command you could call "safari" that will either run the browser with its default start page, or go to the page given as the first command-line arg. There are other expansions that will happen, and some of them may need to be escaped--see sh(1) for more details. Finally, if additional setup is required before the AppleScript gets run, there is no limit on the lines that can precede the "exec osascript" line. And, if you need to do cleanup afterwards, omit the "exec" and put a line containing EOF after the AppleScript, then you can do more shell lines:
shell line
shell line
osascript <<EOF
AppleScript line
AppleScript line
EOF
shell line
shell line
Well, that's the idea. I hope someone finds it helpful.HTML |
In unix, most scripting languages accept line-initial <tt>#</tt> as a comment marker. This allows the file-inital shebang sequence (<tt>#!</tt>) to be used to automatically exec the interpreter on a script. I wanted this functionality for AppleScript (which I am trying to learn), but AppleScript accepts only <tt>(*...*)</tt> and <tt>--</tt> as its comment markers, so it is basically incompatible with the shebang method. <p> However, there is a convention in unix that executable files that do not have a recognizable header (of which <tt>#!</tt> is one) are passed to /bin/sh for execution. This allows considerable flexibility for use with AppleScript. The most basic idea is to use a first line containing: </p><pre><code> exec osascript <<\EOF </code></pre> and then follow it with an arbitrary AppleScript. Voila, a seemingly direct execution of an AppleScript file. Trivial example: <pre><code> exec osascript <<\EOF tell app "Safari" activate end tell </code></pre> Now, it is difficult with osascript to pass command-line parameters to the AppleScript.<br><br>However, using this method, if the <tt>\</tt> is omitted before the EOF, then shell variable expansion will be done on the AppleScript text before it is passed to osascript, and this is enough for most command line parameter usage. Less trivial example: <pre><code> exec osascript <<EOF tell app "Safari" activate if not "$1" = "" then if not (exists document 1) then make new document at the beginning of documents end if set the url of the front document to "$1" end if end tell </code></pre> This yields a command you could call "safari" that will either run the browser with its default start page, or go to the page given as the first command-line arg. There are other expansions that will happen, and some of them may need to be escaped--see sh(1) for more details. Finally, if additional setup is required before the AppleScript gets run, there is no limit on the lines that can precede the "exec osascript" line. And, if you need to do cleanup afterwards, omit the "exec" and put a line containing EOF after the AppleScript, then you can do more shell lines: <pre><code> shell line shell line osascript <<EOF AppleScript line AppleScript line EOF shell line shell line </code></pre> Well, that's the idea. I hope someone finds it helpful. |
---|