6

Google suggests

echo "input" | osascript filename.scpt

with filename.scpt

set stdin to do shell script "cat"
display dialog stdin

However, I could get only blank dialog: it has no text. How can I get stdin from AppleScript at the version?

My OS version is OSX 10.8 Mountain Lion.

4 Answers 4

9

According to this thread, as of 10.8 AppleScript now aggressively closes standard in. By sliding it out of the way to an unused file descriptor, it can be saved. Here's an example of doing that in bash.

Here we get at it again with a cat subprocess reading from the magic fd.

echo world | osascript 3<&0 <<'APPLESCRIPT'
  on run argv
    set stdin to do shell script "cat 0<&3"
    return "hello, " & stdin
  end run
APPLESCRIPT

Will give you:

hello, world
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your information! The latest way is very helpful and I could work your sample code at my PC.
It should, but I can't verify. The FD redirection trick is older than the hills, so if the rest works on prior versions then I would expect this to as well.
I'd love to have an explanation of what 3<&0 means. I consider myself an intermediate shell scripter and have never seen this before.
Copy and pasted this gives the error syntax error: The run handler is specified more than once, or there were top-level commands in addition to the run handler. (-2752
@SridharSarnobat 3<&0 assigns the standard file descriptor 0 to another number 3 (higher than 1 for stdout and 2 for stderr). When AppleScript runs, it reads the script from stdin, then shuts the file descriptor 0 close, so the script can't read from it anymore. However, 3 stays open and also points to stdin. cat 0<&3 again swaps the descriptors back, sliding in 3 as cat's stdin. More generally: open files are passed to programs under numbers, where 0 to 2 are standard input, output and error output; however a parent process can also pass arbitrary more file descriptors.
6

Sorry, not enough reputation to comment on answers, but I think there's something important worth pointing out here...

The solution in @regulus6633's answer is not the same as piping data into osascript. It's simply stuffing the entire pipe contents (in this case, echo output) into a variable and passing that to osascript as a commandline argument.

This solution may not work as expected depending on what's in your pipe (maybe your shell also plays a part?)... for example, if there are null (\0) characters in there:

$ var=$(echo -en 'ABC\0DEF')

Now you might think var contains the strings "ABC" and "DEF" delimited by a null character, but it doesn't. The null character is gone:

$ echo -n "$var" | wc -c
    6

However, using @phs's answer (a true pipe), you get your zero:

$ echo -en 'ABC\0DEF' | osascript 3<&0 <<EOF
>   on run argv
>     return length of (do shell script "cat 0<&3")
>   end run
>EOF
7

But that's just using zeros. Try passing some random binary data into osascript as a commandline argument:

$ var=$(head -c8 /dev/random)
$ osascript - "$var" <<EOF
>   on run argv
>     return length of (item 1 of argv)
>   end run
>EOF
execution error: Can’t make some data into the expected type. (-1700)

Once again, @phs's answer will handle this fine:

$ head -c8 /dev/random | osascript 3<&0 <<EOF
>  on run argv
>    return length of (do shell script "cat 0<&3")
>  end run
>EOF
8

Comments

4

I know that "set stdin to do shell script "cat"" used to work. I can't get it to work in 10.8 though and I'm not sure when it stopped working. Anyway, you basically need to get the echo command output into a variable which can then be used as an argument in the osascript command. Your applescript needs to handle arguments too (on run argv). And finally, when you use osascript you must tell an application to "display dialog" otherwise it will error.

So with all that said here's a simple applescript which handles arguments. Make this the code of filename.scpt.

on run argv
    repeat with i from 1 to count of argv
        tell application "Finder"
            activate
            display dialog (item i of argv)
        end tell
    end repeat
end run

Here's the shell command to run...

var=$(echo "sending some text to an applescript"); osascript ~/Desktop/filename.scpt "$var"

I hope that helps. Good luck.

1 Comment

Thank you very much! I was very helped your detailed answer! I could get stdin now ;)
0

Late to this, but the original AppleScript seems to try to do something not allowed with osascript.

If in the original filename.scpt this line:

display dialog stdin

Is changed to:

tell application "System Events" to display dialog stdin

Then passing a value via stdin (as opposed to command line arguments) definitely still works in 10.7.5 Lion, maybe 10.8 Mountain Lion too.

1 Comment

stdin appears to be closed on 10.8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.