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.
return answerbelongs on a separate line; In your initial line, you are missing the""afterdefault answer; Finally, when using osascript, each line of applescript should get a-e, not only the first one.trystatement to handle the error but I think you will still end up without knowing what the user typed before cancellingtellblock (andactivate) was for historic reasons. When I tested the script on an old machine running OSX (10.6.8) the dialog did not appear without them. However running on macOS Monterey (12.2.1) they're not actually necessary - just this reduced example runs successfully on Monterey (...and the previous example too). Essentially as I don't know which OS version is being used in this instance I just provided the code/example that run successfully in both OS versions.