Pergunta

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).

Foi útil?

Solução

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.

Outras dicas

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.

On your terminal

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

:)

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.

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

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.

Not a drop-in replacement and not directly compatible with bash/zenity, but if you want a non-portable native Mac OOTB solution with no extra dependencies then you might want to consider AppleScript.

Below is a typical scenario of a script 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

Result:

applescript start android AVD

Reference

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

/Library/Scripts

It's been years, and this is a work in progress, 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

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).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a apple.stackexchange
scroll top