20

I'm porting some simple X windows/bash scripts to Mac OSX, but I cannot find a build of Zenity (a Gnome app for basic GUI dialogs) for OSX. Is there some other command line driven dialog utility I can use? Basic operations: notification dialog

  • List item
  • (i.e. basic title, text, icon, OK button)
  • error notification
  • yes/no or ok/cancel prompts
  • select 1 or n items from a list
  • input text string

I don't (yet) know AppleScript (but it seems pretty verbose for the file maintenance tasks I'm interested in).

0

9 Answers 9

11

Check out cocoaDialog:

cocoaDialog is an OS X application that allows the use of common GUI controls such as file selectors, text input, progress bars, yes/no confirmations and more with a command-line application. It requires no knowledge of Cocoa, and is ideal for use in shell and Perl scripts (or Ruby, or Python, or... etc).

It's a pretty simple concept — pass arguments to the executable to create a dialog, and it returns a result string. There are some good examples as well as documentation.

Unfortunately the main site seems to have gone but the source code is still there. And Internet Archive does have some of the pages

The executable can be got from package manager like Macports - after installing Macports then sudo port install cocoadialog

2
  • 1
    @mklement0 cocoaDialog as of November 2017 is back in active development: github.com/cocoadialog/cocoadialog
    – user166989
    Commented Nov 8, 2017 at 14:45
  • 2
    Unfortunately cocoadialog appears to be unmaintained and abandoned
    – kitsune
    Commented Feb 23, 2021 at 7:34
7

Homebrew provides the zenity package. It uses the MacOS X11 server (emulation) Xquartz.

Makes it easier to have cross-OS implementations, but it is not native MacOS X gui.

1
  • For those who are interested, zenity is available on x11 tap. Run brew install homebrew/x11/zenity to install it.
    – C--
    Commented Jan 4, 2016 at 12:14
7

Not a drop-in replacement and not directly compatible with bash/zenity, but if you are ok with a non-portable native Mac OOTB solution with no extra dependencies then consider AppleScript (or, as of Yosemite, Javascript JXA).

Below is a typical scenario of an AppleScript that builds a dialog from the output of a shell command (emulator -list-avds which lists android emulator images), and then executes another command based on the selected item (emulator -avd <image_name> which launches the selected emulator image):

set avds to paragraphs of (do shell script "~/Library/Android/sdk/emulator/emulator -list-avds")
set avd to (choose from list avds with prompt "Please select an AVD to start" default items "None" OK button name {"Start"} cancel button name {"Cancel"})
do shell script "~/Library/Android/sdk/emulator/emulator -avd " & avd & " -no-boot-anim > /dev/null 2>&1 &"

You can run the script from the ootb Script Editor.app or from Automator.app. Both apps also allow saving a script as a native MacOS application bundle.

To run the script above from a bash script you could use:

osascript -e '
set avds to paragraphs of (do shell script "~/Library/Android/sdk/emulator/emulator -list-avds")
set avd to (choose from list avds with prompt "Please select an AVD to start" default items "None" OK button name {"Start"} cancel button name {"Cancel"})
do shell script "~/Library/Android/sdk/emulator/emulator -avd " & avd & " -no-boot-anim > /dev/null 2>&1 &"
'

Make sure you include the single quote on the last line. You can also use the heredoc syntax or turn into a pure applescript executable script with the shebang line #!/usr/bin/osascript (for AppleScript) or #!/usr/bin/osascript -l JavaScript for Javascript JXA correspondingly.

Result:

applescript start android AVD

Reference

For examples of AppleScript code check out the pre-installed scripts provided by Apple at:

/Library/Scripts

To see the built-in API documentation for AppleScript/Javascript(JSX) start Script Editor.app, pick File > Open Dictionary and then select StandardAdditions.osax in the dropdown.

Using Javascript instead of AppleScript

As of Yosemite instead of AppleScript you can also use Javascript (JXA).

Tutorials and example of using Javascript JXA

Saving as a MacOS application

From the Script Editor.app you can also convert your script (applescript or javascript) into a native MacOS application by selecting File > Export > File format: Application so you'll be able to run the script as a regular mac app.

5

On your terminal

brew install zenity && zenity --info --text 'You did it!'

:)

4

Try also Pashua.

Pashua is a tool for creating native Aqua dialog windows from programming languages that have none or only limi­ted support for graphic user inter­faces on Mac OS X. Currently, it supports Apple­Script, Perl, PHP, Python, Groovy, Rexx, Ruby, shell scripts and Tcl—and if your favourite language is not included in this list: writing the glue code for communicating with Pashua is pretty simple.

1
  • +1; Pashua is powerful, but the API style is different from Zenity's in that instead of separate command-line arguments (multi-line) configuration strings describing the desired GUI must be passed. Probably takes longer to get started.
    – mklement0
    Commented Apr 14, 2014 at 17:08
3

It's been years, but I'm working on a zenity “port” to both macOS and Windows.

You can get it here:
https://github.com/ncruces/zenity/releases

Read more:
https://github.com/ncruces/zenity

Some screenshots from https://github.com/ncruces/zenity/wiki/Message-dialog :

Yes/no dialog

Error dialog

Installing through Homebrew is simple:

brew install ncruces/tap/zenity

On macOS the only dependency is osascript (with JXA, because JavaScript is easier to work with than AppleScript).

On Windows there are no dependencies, not even the Explorer shell (which means it will work on Server Core).

2

MacOS comes with Tcl/Tk built-in. Mac's python comes with the Tkinter layer to use it, but you can also use it in the shell and X11.

1

There is a zenity compatible implemention called qarma, written in qt, which can be compiled on mac os

https://github.com/luebking/qarma/issues/15

0

There is a another cross-platform package from the Githooks project:

https://github.com/gabyx/Githooks#dialog-tool

You can install it by downloading the release and bin/dialog options --option 1 --option 2 --title "asd" --text "bla bla"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .