Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use pbcopy to direct stdout to osascript UNIX
A serious drawback to the command-line osascript tool is that it doesn't provide a way to pass arguments to a script. A workaround is to use the clipboard to store data (via pbcopy), then retrieve it from AppleScript's the clipboard object. For example:

echo 'Test text' | pbcopy; osascript -e 'tell application 
 "Finder"' -e 'activate' -e 'set theMessage to the clipboard' 
 -e 'display dialog theMessage buttons "OK" default button 1'
 -e 'end tell'
 
The above is one long line; replace each line break with a single space when copying and pasting. You can substitute any command (or series of piped commands) for echo 'Test text'.
    •    
  • Currently 3.33 / 5
  You rated: 4 / 5 (3 votes cast)
 
[5,791 views]  

Use pbcopy to direct stdout to osascript | 3 comments | Create New Account
Click here to return to the 'Use pbcopy to direct stdout to osascript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
shell scripts
Authored by: hayne on Nov 27, '03 05:01:32PM
You will probably want to put these sorts of things into a shell script rather than just running them on the command line. And if you do so, there is another way that you can get parameters into your AppleScript - use shell variables.

Here's how you would do the example from the hint:

#!/bin/sh

text="Test text"

/usr/bin/osascript <<EOT
tell application "Finder"
activate
set theMessage to "$text"
display dialog theMessage buttons "OK" default button 1
end tell
EOT
I have used the "here document" feature of the shell (with <<EOT) to make the script easier to write and to read.

Of course, you could make the shell variable take its value from the clipboard as in the hint:

text=`/usr/bin/pbpaste`


[ Reply to This | # ]
shell scripts
Authored by: mstillwell on Nov 28, '03 01:46:44AM

I agree that in most cases, it's probably easiest to put $1, $2, etc. directly into the here document. However, here's another idea, from Chris Nebel of AppleScript engineering.

Also note that if you're outputting text from your AppleScript, you'll need to feed it through tr to map the \r's to \n's:


#!/bin/sh

/usr/bin/osascript << END | tr "\r" "\n"

-- your script here

END



[ Reply to This | # ]
race condition
Authored by: SOX on Nov 27, '03 11:30:47PM

As I understand it pasdteboard is a global object not process specific. in which case this could set up a race condition if two processes are pasting simultaneously.



[ Reply to This | # ]