libbash.sh GUI features some functions to display dialogs and notifications for scripts with graphical interfaces.
GUI tools are commands used to display GUI dialogs and notifications to the user.
libbash.sh GUI currently supports the following GUI tools:
- kdialog
- zenity
- osascript (Apple script for macOS)
- cscript (VBscript for Windows)
- dialog
libbash.sh GUI is loaded with -g
or --gui
option:
source "/path/to/libbash.sh" -g
Then call the functions described below.
If you plan to execute a script from a cron job, dialogs (like notifications) may no be printed.
It's because the $DISPLAY
variable is not set in the cron job context.
To avoid that, you have to manually set the DISPLAY variable in your script with the command:
export DISPLAY=":0"
You can get users current display with the following command (replace myuser
by your user):
who | grep "^myuser .*(:[0-9])$" | head -1 | sed "s/.*(\(:[0-9]*\))$/\1/g"
Note: If you set the DISPLAY variable AFTER integrate libbash GUI, then you have to reset the GUI tool with calling the lbg_set_gui
function (see usage below).
All functions are named with the lbg_
prefix.
Functions with a *
are not fully supported on every OS yet (may change in the future).
- GUI tools
- Messages and notifications
- User interaction
- Files and directories
Get the current GUI tool name (see available tools above).
lbg_get_gui
- 0: OK
- 1: No GUI tool available on this system
gui_tool=$(lbg_get_gui)
Set a GUI tool to be used (see the supported tools above).
By default, the first tool available in the list is used. If no tool specified, the default one will be set. If multiple tools are given, the first available will be set.
e.g. if kdialog
and zenity
are installed, kdialog
will be used over zenity
,
that's why you can set zenity
if you prefer.
lbg_set_gui [GUI_TOOL...]
- 0: GUI tool set
- 1: GUI tool not supported
- 3: GUI tool not available on this system
- 4: GUI tool available, but no X server is currently running (stay in console mode)
if lbg_set_gui zenity ; then
echo "Using zenity for dialogs."
fi
Display an info message dialog.
lbg_display_info [OPTIONS] TEXT
or
lbg_info [OPTIONS] TEXT
Note: you can also give TEXT
argument using stdin or pipes.
-t, --title TEXT Set a title to the dialog
- 0: OK
- 1: Usage error
- 2: Unknown dialog error
lbg_info "This is an info message."
Displays a warning message dialog.
lbg_display_warning [OPTIONS] TEXT
or
lbg_warning [OPTIONS] TEXT
Note: you can also give TEXT
argument using stdin or pipes.
-t, --title TEXT Set a title to the dialog
- 0: OK
- 1: Usage error
- 2: Unknown dialog error
lbg_warning "This is a warning message."
Displays an error message dialog.
lbg_display_error [OPTIONS] TEXT
or
lbg_error [OPTIONS] TEXT
Note: you can also give TEXT
argument using stdin or pipes.
-t, --title TEXT Set a title to the dialog
- 0: OK
- 1: Usage error
- 2: Unknown dialog error
lbg_display_error "This is an error message."
Displays a critical error mesage dialog.
lbg_display_critical [OPTIONS] TEXT
or
lbg_critical [OPTIONS] TEXT
Note: you can also give TEXT
argument using stdin or pipes.
-t, --title TEXT Set a title to the dialog
- 0: OK
- 1: Usage error
- 2: Unknown dialog error
lbg_critical "This is a critical error message."
Displays a debug info message dialog.
lbg_display_debug [OPTIONS] TEXT
or
lbg_debug [OPTIONS] TEXT
Note: you can also give TEXT
argument using stdin or pipes.
-t, --title TEXT Set a title to the dialog
- 0: OK
- 1: Usage error
- 2: Unknown dialog error
lbg_debug "This is a debug message."
Displays a notification popup.
lbg_notify [OPTIONS] TEXT
Note: you can also give TEXT
argument using stdin or pipes.
On Linux systems, the notify-send
command is used by default over zenity --notification
command.
We chose it because it is more powerful and have a better integration on every desktop environments.
But if you want, you can use the --no-notify-send
option to not use it and use your chosen GUI tool.
-t, --title TEXT Set a title to the notification
--timeout SECONDS Timeout before notification disapears (if not set, use default system)
This option is NOT available on macOS
--no-notify-send Do not use the notify-send command if exists*
* See above for more details
- 0: OK
- 1: Usage error
- 2: Notification command error
lbg_notify --timeout 5 "This notification will disapper in 5 seconds..."
Displays a dialog to ask a question to answer by yes or no.
lbg_yesno [OPTIONS] TEXT
-y, --yes Set Yes as default button (not available on kdialog and zenity)
--yes-label TEXT Change Yes label (not available on zenity and Windows)
--no-label TEXT Change No label (not available on zenity and Windows)
-t, --title TEXT Set a title to the dialog
- 0: Yes
- 1: Usage error
- 2: No
if ! lbg_yesno "Do you want to continue?" ; then
exit
fi
Displays a dialog to ask user to choose one or multiple options.
Chosen IDs are set into the $lbg_choose_option
(array) variable.
lbg_choose_option [OPTIONS] CHOICE [CHOICE...]
-d, --default ID[,ID...] Option(s) to use by default (IDs starts to 1)
-m, --multiple Allow user to choose between multiple options
-l, --label TEXT Set a question label (default: "Choose an option:")
-t, --title TEXT Set a title to the dialog
- 0: OK
- 1: Usage error
- 2: Cancelled
- 3: Bad choice
if lbg_choose_option --label "Choose a planet:" --default 1 Earth Jupiter ; then
chosen_planet=$lbg_choose_option
fi
if lbg_choose_option --multiple --label "Choose valid countries:" --default 1,2 France USA Neverland ; then
chosen_countries=(${lbg_choose_option[@]})
fi
Displays a dialog to ask user to input a text.
Input text is stored into the $lbg_input_text
variable.
lbg_input_text [OPTIONS] TEXT
-d, --default TEXT Default value
-t, --title TEXT Set a title to the dialog
- 0: OK
- 1: Usage error
- 2: Cancelled
if lbg_input_text --default $(whoami) "Please enter your username:" ; then
user_name=$lbg_input_text
fi
Displays a dialog to ask user to input a password.
WARNING: No password dialog is displayed on Windows (prompt in console).
lbg_input_password [OPTIONS] [QUESTION_TEXT]
Password is stored into the $lbg_input_password
variable.
-c, --confirm Display a confirm password dialog
--confirm-label TEXT Set the confirmation label (not available on zenity)
-m, --min-size N Force password to have at least N characters
-t, --title TEXT Set a title for the dialog
QUESTION_TEXT Set a label for the question (not available on zenity)
- 0: OK
- 1: Usage error
- 2: Cancelled
- 3: Passwords mismatch
- 4: Password is too short (if
--min-size
option is set)
if lbg_input_password ; then
user_password=$lbg_input_password
fi
Displays a dialog to choose an existing directory.
Path of the chosen directory is set into the $lbg_choose_directory
variable.
lbg_choose_directory [OPTIONS] [PATH]
-a, --absolute-path Return absolute path of the directory
-t, --title TEXT Set a title to the dialog
PATH Starting path (current directory by default)
WARNING: On Windows, starting path is not working when using cscript dialogs (but works in console mode).
- 0: OK
- 1: Usage error
- 2: Cancelled
- 3: Chosen path does not exists or is not a directory
if lbg_choose_directory /opt ; then
opt_path=$lbg_choose_directory
fi
Displays a dialog to choose an existing file.
Path of the chosen file is set into the $lbg_choose_file
variable.
lbg_choose_file [OPTIONS] [PATH]
-s, --save Save mode (create/save a file instead of open an existing one)
-f, --filter FILTER Set file filters
e.g. -f "*.jpg" to filter by JPEG files
-a, --absolute-path Return absolute path of the file
-t, --title TEXT Set a title to the dialog
PATH Starting path or default file path (open current directory by default)
WARNING: File filters are not supported yet with dialog command.
- 0: OK
- 1: Usage error
- 2: Cancelled
- 3: Chosen path does not exists or is not a file (if open mode) or invalid parent directory (if save mode)
- 4: Cannot get absolute path of the file*
* In this case, you can, however, retrieve the chosen path in the $lbg_choose_file
variable.
if lbg_choose_file --filter "*.txt" ; then
text_file=$lbg_choose_file
fi
Open directories in a graphical file browser.
lbg_choose_directory [OPTIONS] [PATH...]
-e, --explorer CMD Open directory with a custom application
PATH Directory path (current directory by default)
- 0: OK
- 1: Usage error
- 2: Explorer command does not exists
- 3: Unknown error (happens often on Windows)
- 4: One or more of the specified paths are not existing directories
Note: On some OS like Windows, this function may work but returns a bad exit code.
# open home directory
lbg_open_directory ~
libbash.sh is licensed under the MIT License. See LICENSE.md for the full license text.
Author: Jean Prunneaux https://jean.prunneaux.com