Make GUI in MacOS Terminal

How make CLI apps with GUI on MacOS without coding*

Fullstack CTO
5 min readNov 9, 2022

Tired of displaying raw text on Console? I tell you how to modernize your Bash/ZSH scripts by adding some GUI.

Sometimes you need a simple program for some kind of automation on MacOS and you don’t want to write a full-fledged application. Zsh and Bash are powerful enough tools, but if you need to provide such an application for “people” rather than programmers, in that case it is better to add at least some simple GUI.

MacOS has a powerful tool in the form of Apple Script, but you have to learn how to write it. I know a lot about Bash scripting. Therefore, it is easier for me to write the basic logic code in pure Bash. But to add GUI you can use either AppleScript or some additional libraries.

I will tell you not only about the third-party libraries, but also how to use the built-in tools and give you my own little GUI tools written in Bash and using the applescript calls.

You are able to include GUI (Graphical User Interface) based input/output components into your next bash script

Show error message

Type in bash/zsh terminal or create file with next code:

alert() {
osascript << APPLESCRIPT
tell app "System Events"
display dialog "$1" buttons {"OK"} default button 1 with title "$2" with icon caution
return -- Suppress result
end tell
APPLESCRIPT
}

Usage:

$ alert "Shit happens!"

Prompt

prompt() {
title="${3:-Input}"
osascript << APPLESCRIPT
tell app "System Events"
text returned of (display dialog "$1" default answer "$2" buttons {"OK"} default button 1 with title "$title")
end tell
APPLESCRIPT
}

Usage:

value="$(prompt 'Enter number:' '42' 'Some title')"

Colorpicker

colorpicker() {
osascript << APPLESCRIPT
tell app "System Events"
set theColor to choose color default color {255, 255, 255}
end tell
APPLESCRIPT
}

File prompt

selectfile() {
osascript << APPLESCRIPT
tell app "System Events"
set theDocument to choose file with prompt "Please select a document to process:"
end tell
APPLESCRIPT
}

Result of select in specific format (path to file):

Select box with list

list() {
list='"'$(printf '%s","' $@)'"'
osascript << APPLESCRIPT
tell app "System Events"
set theList to {$list}
set theChoice to choose from list theList with title "Title" with prompt "Prompt"
return theChoice
end tell
APPLESCRIPT
}

Notification

notify () {
osascript << APPLESCRIPT
tell app "System Events"
display notification "$1" with title "${2:-Notify}" subtitle "${3:-}" sound name "Funk"
end tell
APPLESCRIPT
}

The documentation explains how the notifications will be displayed.

Notification Center offers another opportunity for providing feedback during script execution. Use the Standard Additions scripting addition’s display notification command to show notifications, such as status updates as files are processed. Notifications are shown as alerts or banners, depending on the user’s settings in System Preferences > Notifications. See Figure 24-1 and Figure 24-2.

Figure 24–1A banner notification

Figure 24–2An alert notification

To show a notification, provide the display notification command with a string to display. Optionally, provide values for the with title, subtitle, and sound name parameters to provide additional information and an audible alert when the notification appears, as shown in Listing 24-1 and Listing 24-2.

But there are limitations and you cannot configure notifications. Therefore, you can use the utility alerter for this purpose.

Alerter

Alerter is a command-line tool to send Mac OS X User Alerts (Notifications), which are available in Mac OS X 10.8 and higher. (even catalina) the program ends when the alerter is activated or closed, writing a the activated value to output (stdout), or a json object to describe the alert event.

Alerts are OS X notifications that stay on screen unless dismissed. Two kinds of alert notification can be triggered : “Reply Alert” or “Actions Alert”

Zenity

This repo includes:

  • a cross-platform Go package providing Zenity-like dialogs (simple dialogs that interact graphically with the user)
  • a “port” of the zenity command to both Windows and macOS based on that library.

Implemented dialogs:

Behavior on Windows, macOS and other Unixes might differ slightly. Some of that is intended (reflecting platform differences), other bits are unfortunate limitations.

Notify in iTerm tab

And finally, a little trick for your scripts: notifying the tab in iTerm2

sleep 5; tput bel

For example:

ping -c 3 www.google.com && tput bel &

My GUI snippets

--

--

Fullstack CTO
Fullstack CTO

Written by Fullstack CTO

CTO and co-founder at NEWHR & Geekjob

Responses (1)