Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

example of reading stdin and writing stdout in Applescript

2,478 views
Skip to first unread message

Dan LaBell

unread,
Feb 1, 2005, 4:49:04 PM2/1/05
to
The following bash-applescript reads a single line from stdin as a unix
path name to file, and prints out the comment of input file to stdout.
It demonstrates a method of passing arguments to osascript, addressing
this portion of the osascript man page:
BUGS
osascript does not yet provide any way to pass arguments to
the
script.

Apologies if this has been discussed in group at length before, but I
found nothing via google, and hacked this out late last night, at any
rate I thought it might useful to share, if your interested in calling
applescript from shell or other c programs, and passing arguments.

I can call this at a unix prompt like:
echo ~/testfile | comment.bash , and it will print to stdout file's
comment. Save as variable w/ X="$( echo $file1 | comment.bash)", etc.
At any rate, it provides 'a' way of passing arguments in and out.


----comment.bash---------------------------------
#! /bin/bash
osascript 10<&0 <<EOF

set linefeed to ASCII character 10
set u to "/dev/fd/10"
set p to POSIX file u
open for access p
copy result to stdin
set u to "/dev/fd/1"
set p to POSIX file u
open for access p with write permission
copy result to stdout

read stdin before linefeed as text
copy result to linebuf
set lp to POSIX file linebuf
tell app "Finder"
get comment of (lp as file)
copy result to file_comment
end tell
write (file_comment & linefeed ) to stdout

EOF
----------------end comment.bash --------------

Writing to stdout is easy, but osascript closes stdin (file descriptor
0) multiple times ( I used ktrace && kdump ), the trick is to
dup stdin to another descriptor, prior to calling osacript,
the '10<&0' is the bash way to to this, creating filedescriptor 10
as duplicate to 0, this could be done in C as well.
One caveat, is that open for access and the like, don't like symlinks,
in fact applescript read acts like unix readlink(), if you used
"/dev/stdin" (a symlink to /dev/fd/0) , you read the contents of the
link "/dev/fd/0", instead of the actual file. BTW, its possible to read
/dev/random and /dev/zero, in apple script as well.

I noticed only a slight speed improvement by using a compiled script,
rather than passing osascript the file as above (tenth of a second
improvment), its actually kind of slow, but I think adapting to read a
list of files from stdin, like the output of unix find command, would
speed things up, and actaully be generally, more useful. Could also
try null terminated records, and find's -print0 option to handle all
possible filenames. Something like find . -type f | setcomment
"Copyright 2005" , should be possible. With setcomment being a wrapped
applescript.

D. Grady

unread,
Dec 15, 2010, 11:41:42 PM12/15/10
to
Doesn't this also work?

---ascat.sh---
#!/usr/bin/env osascript

set stdin to do shell script "cat"

"The following was read by AppleScript: " & stdin
--------------

$ echo hello | ./ascat.sh
The following was read by AppleScript: hello

Maybe this fails with more complicated input, or is only possible in newer versions of AppleScript. Anyhow, it's been working for me; this discussion was highly ranked on Google and I wanted to point out this other possibility.

-Daniel

0 new messages