Re: OS X AppleScripts and Unix pipes?
Re: OS X AppleScripts and Unix pipes?
- Subject: Re: OS X AppleScripts and Unix pipes?
- From: Christopher Nebel <email@hidden>
- Date: Tue, 28 Nov 2000 19:27:13 -0800
On Monday, November 27, 2000, at 12:46 PM, Phi Sanders wrote:
>
Did I miss the answer to this?
>
>
I'm keenly interested in marrying these two techs...
>
>
On 11/25/00, Scott Johnson {email@hidden} said the following :
>
>
>Can AppleScripts communicate with Unix process via pipes like Bourne and
>
>other shell scripts?
I haven't seen anyone else step up to answer this, so here we go.
The answer as of Public Beta is yes, a little. The key is the osascript command (it has a real man page -- read it!), which lets you execute scripts, either compiled or plain text, from the command line. For example, you can say:
% echo 2+3 > foo.as
% /usr/bin/osascript foo.as
5
osascript can also read the script from standard input, so the previous example could be written like this:
% echo 2+3 | /usr/bin/osascript
5
That's the good news. The bad news is that (1) because AppleScript doesn't understand "#" as a comment character, it's difficult to make a self-contained osascript executable like you can with awk or perl, and more seriously, (2) osascript doesn't have any provision for passing arguments to the script, nor does the script have any way to read from standard input, so it's difficult to use osascript as anything but a source, i.e., the first thing in a pipe.
This is not to say you can't do it -- a co-worker of mine suggested a moderately twisted workaround for (2). (Richard23, are you listening? You'll appreciate this.) What you do is to write another little script that takes standard input and spits it back out looking like an AppleScript string property, cat that together with the script source, and pipe the whole mess to osascript. The resulting script can look at its "input" property and parse it as necessary. (Those wise in the ways of Unix will realize immediately that this sucks from a pipeline perspective, because osascript won't be able to do anything until it gets *all* the input, which means everything upstream has to finish before osascript can start, and because osascript doesn't generate any output until the script completes, nothing downstream from osascript can start until osascript finishes. But hey, it works!) The following script (call it osapipe) will do the trick:
#/bin/sh
sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' \
-e '1s/^/property input : "/' \
-e '$s/$/"/' | \
cat - $* | tr "\n" "\r" | /usr/bin/osascript
In action:
% echo "every word of my input" > foo.as
% echo one two three | osapipe foo.as
{"one", "two", "three"}
Phew! By the way, the "tr" is in there because AppleScript currently doesn't deal well with lf-delimited source. If it causes you trouble, remove it, but you may wind up with a different set of problems.
This whole situation isn't likely to change much before Mac OS X first ships. (I'm almost certainly going to change the default output style to human-readable rather than source, so you won't have to deal with the quotes and character escapes. Maybe I'll get to the bit about "#" as comment-to-end-of-line, but don't hold your breath.)
However, we're well aware that lots of people are interested in this sort of thing, so we've got a bunch of stuff involving Unix integration under consideration for future versions. Your suggestions are welcome.
--Chris Nebel
AppleScript Engineering