Apple - How can I run a shell script that prompts for user input from within Applescript

AppleScript embedded in a shell script is often messy, hard to read, and hard to quote properly.

I get around that by using this sort of construct.

#!/usr/bin/env bash

read -r -d '' applescriptCode <<'EOF'
   set dialogText to text returned of (display dialog "Query?" default answer "")
   return dialogText
EOF

dialogText=$(osascript -e "$applescriptCode");

echo $dialogText;

-ccs


I think what you need is:

set T to text returned of (display dialog "Query?" buttons {"Cancel", "OK"} default button "OK" default answer "")

If you want to do this from a bash script, you need to do:

osascript -e 'set T to text returned of (display dialog "Query?" buttons {"Cancel", "OK"} default button "OK" default answer "")'

I can explain it in more detail if you need me to, but I think it's pretty self-explanatory.

EDIT: If you want the result as a shell variable, do:

SHELL_VAR=`osascript -e 'set T to text returned of (display dialog "Query?" buttons {"Cancel", "OK"} default button "OK" default answer "")'`

Everything you type between the backticks (` `) is evaluated (executed) by the shell before the main command. See this answer for more information.

I agree that this is a little "hacky", and I'm sure there are better ways to do this, but this works.