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:
osascript does not yet provide any way to pass arguments to
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.
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
write (file_comment & linefeed ) to stdout
----------------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