how to run an AppleScript from a shell script

bogus, i’m sure there are better ways, but this works…

  • touch myscript

  • chmod 755

  • add shebang line, and osascript with input from a here document

example:

#! /bin/sh
/usr/bin/osascript - \<\<EOM
tell app "Finder"
        activate
        display dialog (do shell script "ls /tmp")
end tell
say "ls /tmp"
EOM

get a return value from a dialog box button:

#!/bin/sh
[ -n "$1" ] && arg="$1" || arg="hello world"
button=`/usr/bin/osascript - \<\<EOM
tell app "Terminal"
say "$arg"
beep
display dialog "$arg" buttons {"hi", "bye"} default button "bye" giving up after 5
set result to button returned of result
end tell
EOM`
echo "button was: $button"

get a return value from a dialog box text field:

#!/bin/sh
[ -n "$1" ] && arg="$1" || arg="black"
prompt="what is your favorite color?"
val=`/usr/bin/osascript - \<\<EOM
tell app "Terminal"
say "$arg"
beep
display dialog "$prompt" buttons {"OK"} default answer "$arg" default button "OK"
set result to text returned of result
end tell
EOM`
echo "favorite color is: $val"