osascript. I've given an example of this approach before.
However, there is something that I've never really been clear on. How does the AppleScript portion of the shell script read from
stdin? Standard I/O is fundamental to Unix programming, so it is essential that AppleScript be able to access
stdin. Searching with Google hasn't enlightened me. There seems to be no useful parallel with
stdout, which works as you'd hope, with
osascriptwriting transparently to it.
After a bit of thinking, I came up with this:
stdinto a temporary file using
cat, then read it back out in the AppleScript portion, again using
cat. As an example, I just open a new TextEdit document with the text from
stdinas its contents.
Saving this as
minimal.sh, I can then create a new TextEdit document from the shell with:
This works, so I've managed to get at
stdin. It seems pretty roundabout though. Is there a better or recommended approach?
Update: I suppose it is worth mentioning that one could use
pbpasteto avoid using the temporary file. However, doing so modifies the clipboard, so I prefer the approach shown.
Further, it would be possible to read the temp file using AppleScript commands, instead of calling
do shell script. That's too fiddly for a minimal example. Beyond that, I don't see much point to it, since I'd do any processing in the shell, just using AppleScript to pass the text to an application. I can't think of any applications where a stream approach would buy us anything.
Update: Modified example of using
minimal.shto actually use