I am running into issues passing multiple variables between AppleScript and bash.
I can get one variable using something like this
updatedName="$(osascript -e 'set the answer to text returned of (display dialog "What is your name" with title "Question" default answer buttons {"Cancel", "Save"} default button "Save")' return answer)"
How do I get both the answer to the text and which button was selected? I basically want to exit the entire script when the cancel button is selected but I can't seem to get both the text entered and which button was pressed.
I have tried something like this but the Cancel button returns "26:246: execution error: User canceled. (-128)" not the exit 0 from the if statement.
question="$(osascript -e 'set theResultReturned to (display dialog "Enter your nane" with title "Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")' return answer)
set theTextReturned to the text returned of theResultReturned
set theButtonReturned to the button returned of theResultReturned
if theButtonReturned is "Cancel" then
exit 0
end if" || exit
I've also tried adding an additional if statement after the question script but I can't get that to work either.
if [ "$question" = "theButtonReturned:Cancel" ];
then
exit 0
fi
If I print $question I get a full output of
button returned:Rename, text returned:Test
set theTextReturned to the text returned of theResultReturned
set theButtonReturned to the button returned of theResultReturned
if theButtonReturned is Cancel then
exit 0
end if
Which is giving me the returned text and the button output but then it is also printing out the entire osascript.
I'm sure I'm missing something simple. I've looked all over google and stack overflow but all the examples are for only one variable.
CodePudding user response:
You say:
"I basically want to exit the entire script when the cancel button is selected but I can't seem to get both the text entered and which button was pressed."
Firstly, it's not possible to return both the text entered and which button was pressed in a scenario whereby the user has clicked the "Cancel" button. This is simply because when cancelling a dialog AppleScript produces the -128
error that you have mentioned.
Consider taking a different approach by utilizing the following shell script instead. It essentailly exits the bash script early with an exit code of 1
if the user selects the "Cancel" button. However, when the "OK" button is clicked any text entered (i.e. the user name) is assigned to the username
variable.
#!/usr/bin/env bash
if ! username=$(osascript 2> /dev/null <<-EOF
return text returned of (display dialog "Enter your name" with title ¬
"Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")
EOF
); then
exit 1
fi
# Continue to do something with $username...
echo "$username"
Explanation
The
$(...)
part (command substitution) is utilized to assign the result from theosascript
command to theusername
variable.The
2> /dev/null
part redirectsstderr
to/dev/null
. This is what essentially prevents the AppleScript-128
error message from being printed to the console when the user clicks the "Cancel" button.The
<<-EOF
part (a Here document) is utilized to pass the AppleScript code to theosascript
command. It's particularly useful to use a Here document when passing multiple lines of code, (AppleScript in this insance) to the shell.The conditional
if
statement begins with an apostrophe, (a!
expression), which means true if the expression is false. Essentially in this scenario it means if the result of theosascript
command is false (i.e. the user has clicked "Cancel") then excute theexit 1
statement.
Additional Note:
The aforementioned example code runs succesfully on macOS Monterey (12.2.1), however on some older versions on OSX you may need to include an AppleScript "System Events" tell
block and activate
statement. When I tested the script (above) on an old machine running OSX (10.6.8) the dialog did not appear without them. For example you may need to do the following which did work successfully on OSX (10.6.8):
#!/usr/bin/env bash
if ! username=$(osascript 2> /dev/null <<-EOF
tell application "System Events"
activate
return text returned of (display dialog "Enter your name" with title ¬
"Question" default answer "" buttons {"Cancel", "Rename"} default button "Rename")
end tell
EOF
); then
exit 1
fi
# Continue to do something with $username...
echo "$username"