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

Using redirects to run AppleScripts in the Terminal UNIX
I got really fed up with having to use osascript -e when running AppleScripts remotely via the Terminal. So I thought that I'd give the 'redirect' command a try -- thus allowing me to type and view the script in a way that is much easier to bugfix than a single line. So to run a remote script I now do:
 $ cat << done | osascript
And on hitting Enter, I get a new prompt which allows me to enter the script line-by-line and then finish with whatever word I choose to indicate that the input has ended (in this case, done). What I like about this method is the fact that I can cut and paste huge scripts into the Terminal window and have them run right away. Note that the Terminal will choke on tabbed lines like the set end... one below:
set volumeNames to {}
repeat with i from 1 to (count of disks)
      set end of volumeNames to name of disk i
end repeat
But all you need to do is remove all of the indents before pasting the script into the Terminal window. If you don't fancy running the script immediately, you can easily adapt the commands to save the script to disk first and then run the file:
 $ cat << done > myScript.txt
 $ osascript < myScript.txt
NB: If you are using special characters that may be interpreted wrongly by the shell you should add quote marks to your "end of file" term:
 $ cat << 'done' | osascript
You can do that with either version, obviously.
  • Currently 2.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)

Using redirects to run AppleScripts in the Terminal | 5 comments | Create New Account
Click here to return to the 'Using redirects to run AppleScripts in the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
easier way
Authored by: ngb on May 06, '04 12:16:49PM
At the prompt type:

% osascript
When you hit enter, you get a blank line with no prompt. Start typing your script (or paste it in). When you're done, hit ^D (Control-D) and the script will execute. The advantage of this approach is that indented lines are correctly interpreted, and no special escape characters are needed for double-quotes within the script.

[ Reply to This | # ]
easier way, forgot to mention...
Authored by: ngb on May 06, '04 12:34:23PM

Forgot to mention, hitting return at the end of a line lets you continue your script on a new line. After the last line of your script, you need to hit Control-D on a new blank line.

[ Reply to This | # ]
easier way
Authored by: orpy on May 06, '04 02:24:43PM

Yes thanks for mentioning that - I was going to post a follow-up myself, but you've beaten me to it :-(

I still think that the "<<" is worth mentioning as it can be used with other commands that require multiple lines of input (like ftp etc) or for creating quick script files on remote machines without using a specific editor.



[ Reply to This | # ]
Using redirects to run AppleScripts in the Terminal
Authored by: tarball on May 06, '04 12:47:06PM

i may be overlooking something obvious about your hint, but when i run applescripts remotely, i typically will just run the script from a file.

if you have shell access (which you obviously do), and you know the code works, you can just open up a command line text editor and paste your code in (pico works well for copying and pasting), save it, and run it like you would on your local machine.

osascript filename

osascript doesn't care if it's in binary script format or plaintext format, and you don't have to worry about the shell messing with characters or anything else that way.

[ Reply to This | # ]
Using redirects to run AppleScripts in the Terminal
Authored by: olivesoft on May 06, '04 02:27:24PM
cat is one of the most abused commands in UNIX. The best way is probably just typing to standard in as the first poster suggested. But if you insist upon using here documents to contain the script that will be used as standard input into the osascript command, just use the following:
osascript << done
I would also suggest using some other end tag than 'done'.

I tend to think of [Mac] OS X as Linux with QA and Taste.
-James Gosling, Java Architect

[ Reply to This | # ]