Get the button returned from an osascript to a variable for a shell script.

shMorganson
New Contributor II

I am trying to set a variable based on if the user clicks OK or Cancel and I want forward the variable to a shell script so I know if I can proceed in the script or not. Does anyone know how to do this?

1 ACCEPTED SOLUTION

cdenesha
Valued Contributor II

Try using 'button returned of' - you'll get the actual text of the button, i.e. "OK" or "Quit". You'll want to specify your buttons and not use "Cancel" which will throw an error. So modifying the first code example in this post:

myVariable=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set myAnswer to button returned of (display dialog "Press Ok or Quit" buttons {"Ok", "Quit"})
end tell
return myAnswer
END)

View solution in original post

7 REPLIES 7

cbrewer
Valued Contributor II

Store your osascript code as a variable.

Example:

computerName=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set the answer to text returned of (display dialog "Enter new computer name" with title "Computer Name Change" default answer "$(scutil --get ComputerName)")
end tell
END)

shMorganson
New Contributor II

So that asks for the user to enter something. How can I just get what button they click?

cbrewer
Valued Contributor II

Gotcha. Try checking the exit code after your osascript command. In my brief testing I get exit code 0 after clicking the first button and exit code 1 after clicking the second button.

New example (runs osascript as current logged in user):

#!/bin/bash

# Get currently logged in user
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

# Get the logged in UID
loggedInUID=$(id -u $loggedInUser)

/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<-EOF
tell application "System Events"
activate
display dialog "Computer name set to " & host name of (system info) buttons {"OK", "Cancel"} default button 1 with title "Computer name"
end tell
EOF

echo $?

shMorganson
New Contributor II

So just trying to test here is what I get. d7dc25e32b0a43d395407537a1504907

cdenesha
Valued Contributor II

Try using 'button returned of' - you'll get the actual text of the button, i.e. "OK" or "Quit". You'll want to specify your buttons and not use "Cancel" which will throw an error. So modifying the first code example in this post:

myVariable=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set myAnswer to button returned of (display dialog "Press Ok or Quit" buttons {"Ok", "Quit"})
end tell
return myAnswer
END)

Vir_Illustris
New Contributor

I need the same thing to accomplish but from within do shell script. I wasn't able to find out how to escape all quotes and special characters if you write in AppleScript, and signed up just to ask for help. The code in question is

do shell script "_status=$( csrutil status | grep -Eio '[a-z\.]+$' ) ;  action_alert=$(if [[ $_status ==  disabled?   ]] ; then echo "System Integrity Protection: disabled" ; fi) ; if [[ $action_alert != "" ]] ; then echo "' display alert \"$action_alert\"  as warning message \"Restart to Recovery\" & return & return & \"While in Recovery start Terminal and run\" & return & \"\\\"csrutil enable ; nvram -d recovery-boot-mode ; reboot\\\"\" buttons {\"Abort\", \"Proceed\"}  ' " ; fi | xargs -I % osascript -e %"

As long as there's only 1 string item in braces following buttons parameter, the code works, but when I add another list item delimited with a comma I get an error. I infer it has something to do with the braces expansion: in one Bash guide I read that prefixing braces with the dollar sign or escaping them prevents expansion but that didn't work out either. So what am I doing wrong?

Mauricio
Contributor III

@user-DnpfQOvJBx Are you trying to run that from an AppleScript or app?