Skip to main content
Solved

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


shMorganson
Forum|alt.badge.img+4

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?

Best answer by cdenesha

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 original
Did this topic help you find an answer to your question?

7 replies

Forum|alt.badge.img+15
  • Esteemed Contributor
  • 719 replies
  • November 26, 2019

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
Forum|alt.badge.img+4
  • Author
  • New Contributor
  • 3 replies
  • November 26, 2019

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


Forum|alt.badge.img+15
  • Esteemed Contributor
  • 719 replies
  • November 26, 2019

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
Forum|alt.badge.img+4
  • Author
  • New Contributor
  • 3 replies
  • November 26, 2019

So just trying to test here is what I get.


Forum|alt.badge.img+14
  • Honored Contributor
  • 862 replies
  • Answer
  • November 26, 2019

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
Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • December 4, 2020

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?


Mauricio11
Forum|alt.badge.img+11
  • Valued Contributor
  • 173 replies
  • December 7, 2020

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings