8

I'm on macOS. I have a script that, after asking for confirmation using read in Terminal, uses grep to check whether /dev/disk1 is mounted, then formats that disk. That's a dangerous script, hence why asking if it's okay first is vital.

Eventually, I would like to have this script be an executable that the user can double-click on. But rather than having the user type "y" and Return into a Terminal window, I would rather a display dialog appear with "yes" and "no" buttons, have them choose, then have the script run based on their answer. Is this possible in bash?

I'm working in an environment in which I don't have administrative access, so while I can write an AppleScript Service to accomplish what I want to do and to integrate this elegantly into the user interface, I can't integrate that Service into the environment without the admin password (since I can't edit ~/Library/Services for the user without it). Also, I cannot download or install any new libraries, applications — anything, really — in the environment; I must only use native bash in Mac OS X.

Here is what I have:

read -p "Are you sure you want to partition this disk? " -n 1 -r # Can I make this be a dialog box instead?
echo 

if [[ $REPLY =~ ^[Yy]$ ]] # Can this accept the result as a condition?
then
    if grep -q 'disk1' /dev/ && grep -q 'file.bin' ~/Downloads; then
        echo # redacted actual code
    else
        osascript -e 'tell app "System Events" to display dialog "The disk is not mounted."'
        exit 1
    fi
else
    exit 1
fi

Thanks very much for your help.

dispepsi
  • 270
  • 3
  • 15
  • Use `osascript` ? Possibly helpful: http://alvinalexander.com/mac-os-x/applescript-shell-unix-command-line-bash-dialog – Paul R May 26 '15 at 07:58
  • That should be fine, but I am not sure how to have bash take the output of osascript and use it in its if statement. I think that you can have osascript say "set variable to (display dialog box ...)", but I am not sure if you can have bash then say "if $variable = true" ... I am not sure if bash can use a variable set by osascript. – dispepsi May 26 '15 at 08:01
  • [How do I make a Mac Terminal pop-up/alert? Applescript?](http://stackoverflow.com/questions/5588064/how-do-i-make-a-mac-terminal-pop-up-alert-applescript) shows an example using the output of an AppleScript command in a bash shell script. The one with 5 points, at the moment. – Thomas Dickey May 26 '15 at 08:21
  • If you want text entry, check out [this question](https://apple.stackexchange.com/q/239764/164737) on Ask Different. Also uses heredocs for longer `osascript` blocks. – Michael Sep 16 '19 at 17:13

2 Answers2

15

Yes, it is possible in bash to take the output of an osascript dialog. Here’s an example with a Yes/No dialog box:

#!/bin/bash

SURETY="$(osascript -e 'display dialog "Are you sure you want to partition this disk?" buttons {"Yes", "No"} default button "No"')"

if [ "$SURETY" = "button returned:Yes" ]; then
    echo "Yes, continue with partition."
else
    echo "No, cancel partition."
fi

If you run this script, the script should echo the appropriate line depending on which button was pressed.

It also shows how to set the default button, which I am assuming for the example is “No”.

If you have a more complex dialog, you would most likely use a regex to detect the responses, as you do in your own sample; though depending on your use case you might want to guard against spoofing responses.

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
  • 1
    Thanks so much! This did the trick exactly. I'll upvote you when I have the reputation. – dispepsi May 26 '15 at 21:28
  • Also, learned from another person that Bash will not run in a graphical mode whatsoever. Seems obvious, but working in a Mac OS X environment has spoiled me in that regard, I suppose... – dispepsi May 26 '15 at 21:32
2

If you're happy with a text-mode (but cross-platform and proven) solution, try using ncurses, and in particular a utility called dialog.

dialog --yesno "Are you sure you want to partition this disk?" 5 50
answer=$?       # Returns: 0 == yes, 1 == no

More details in this tutorial.

declension
  • 4,110
  • 22
  • 25
  • Thanks for your answer, I appreciate it. This is an interesting solution. I should have clarified that I am not able to install any additional packages in the environment in which I am working. I must only use native bash. I've edited my original question a bit in light of your answer. – dispepsi May 26 '15 at 08:08