1

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.

9
  • 1
    You have a few issues that are probably contributing: your parentheses are unbalanced; return answer belongs on a separate line; In your initial line, you are missing the "" after default answer; Finally, when using osascript, each line of applescript should get a -e, not only the first one. Commented Feb 25, 2022 at 22:05
  • Also, when you 'cancel', the script stops executing. You could try using a try statement to handle the error but I think you will still end up without knowing what the user typed before cancelling Commented Feb 26, 2022 at 3:51
  • 1
    You say "basically I want to exit the entire script when the cancel button is selected". I assume that you also want to continue the script when the "OK" button is selected and then do something with the text returned from the prompt. In which case consider doing something like this example. Commented Feb 26, 2022 at 14:04
  • 2
    @Mockman - Good question. The reasoning for including the "System Events" tell block (and activate) 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. Commented Feb 27, 2022 at 16:19
  • 1
    @RobC Interesting, as the command is built into the language and isn't a component of system events. Sometimes commands just don't work the way you think they should. Makes sense, and thanks for clarifying. Commented Feb 27, 2022 at 16:30

1 Answer 1

3

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 the osascript command to the username variable.

  • The 2> /dev/null part redirects stderr 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 the osascript 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 the osascript command is false (i.e. the user has clicked "Cancel") then excute the exit 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"
Sign up to request clarification or add additional context in comments.

1 Comment

Really appreciate the time you took to do this write up. It was super helpful. The first example here was exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.