Posted on 11-26-2019 10:28 AM
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?
Solved! Go to Solution.
Posted on 11-26-2019 12:23 PM
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)
Posted on 11-26-2019 10:38 AM
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)
Posted on 11-26-2019 10:39 AM
So that asks for the user to enter something. How can I just get what button they click?
Posted on 11-26-2019 11:04 AM
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 $?
Posted on 11-26-2019 11:17 AM
So just trying to test here is what I get.
Posted on 11-26-2019 12:23 PM
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)
Posted on 12-04-2020 08:26 AM
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?
Posted on 12-06-2020 11:46 PM
@user-DnpfQOvJBx Are you trying to run that from an AppleScript or app?