Introduction
Here is set of examples demonstrate how to use yad in shell scripts.
Logout dialog
Show logout dialog.
Additional software:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 | #! /bin/bash
action=$(yad --width 300 --entry --title "System Logout" \
--image=gnome-shutdown \
--button="Switch User:2" \
--button="gtk-ok:0" --button="gtk-close:1" \
--text "Choose action:" \
--entry-text \
"Power Off" "Reboot" "Suspend" "Logout")
ret=$?
[[ $ret -eq 1 ]] && exit 0
if [[ $ret -eq 2 ]]; then
gdmflexiserver --startnew &
exit 0
fi
case $action in
Power*) cmd="sudo /sbin/poweroff" ;;
Reboot*) cmd="sudo /sbin/reboot" ;;
Suspend*) cmd="sudo /bin/sh -c 'echo disk > /sys/power/state'" ;;
Logout*)
case $(wmctrl -m | grep Name) in
*Openbox) cmd="openbox --exit" ;;
*FVWM) cmd="FvwmCommand Quit" ;;
*Metacity) cmd="gnome-save-session --kill" ;;
*) exit 1 ;;
esac
;;
*) exit 1 ;;
esac
eval exec $cmd
|
Run dialog
Run dialog with history, URI recognition and run-in-xterm functions
Additional software:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | #! /bin/bash
XTERM="xterm"
# create history file
mkdir -p ${XDG_CACHE_HOME:-$HOME/.cache}/
HISTFILE=${XDG_CACHE_HOME:-$HOME/.cache}/ix-run.history
touch $HISTFILE
# create and run dialog
TITLE="Run command"
TEXT="\nEnter command to execute:\n"
rcmd=$('yad --width=500 --center --window-icon="gtk-execute" --name="${0##*/}" --title="$TITLE" --text="$TEXT" --image="gtk-execute" --entry --editable --rest="$HISTFILE")
[[ -z "$rcmd" ]] && exit 0
# run command
case $rcmd in
http://*|https://*|ftp://*)
xdg-open $rcmd &
;;
mailto://*)
xdg-email $rcmd &
;;
man://*)
eval $XTERM -e "man ${rcmd#man://}" &
;;
telnet*|ssh*)
eval $XTERM -e "$rcmd" &
;;
*)
eval $rcmd &
;;
esac
# add command to history
grep -q -F "$rcmd" $HISTFILE || sed -i "1 i $rcmd" $HISTFILE
exit 0
|
Autostart editor
Edit content of $HOME/.config/autostart.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 | #! /bin/bash
config_dir=${XDG_CONFIG_HOME:-$HOME/.config}
results=$(mktemp --tmpdir autostart.XXXXXXXXXX)
for f in $config_dir/autostart/*.desktop; do
grep -m 1 -e '^[[:blank:]]*Exec' $f | cut -d = -f 2
grep -m 1 -e '^[[:blank:]]*Name' $f | cut -d = -f 2
grep -m 1 -e '^[[:blank:]]*Comment' $f | cut -d = -f 2
done | yad --width=500 --height=300 --title="Autostart editor" --image="gtk-execute" \
--text="Add/remove autostart items" --list --editable --print-all \
--multiple --column="Command" --column="Name" --column="Description" > $results
if [[ ${PIPESTATUS[1]} -eq 0 ]]; then
rm -f $config_dir/autostart/*.desktop
i=0
cat $results | while read line; do
eval $(echo $line | awk -F'|' '{printf "export NAME=\"%s\" COMMENT=\"%s\" COMMAND=\"%s\"", $2, $3, $1}')
cat > $config_dir/autostart/$i$NAME.desktop << EOF
[Desktop Entry]
Encoding=UTF-8
Name=$NAME
Comment=$COMMENT
Exec=$COMMAND
StartupNotify=true
Terminal=false
EOF
$((i++))
done
unset NAME COMMENT COMMAND
fi
rm -f $results
exit 0
|
Graphical frontend for su(1)
Run program as a different user (root by default). Ask password if needed
Additional software:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 | #! /bin/bash
# some defaults
user="root"
suargs="-p"
force="no"
# parse commandline
if [[ $# -eq 0 ]]; then
echo "Usage: ${0##*/} [-f] [-u user] [--] "
exit 1
fi
OPTIND=1
while getopts u: opt; do
case "$opt" in
f) force="yes" ;;
u) user="$OPTARG" ;;
esac
done
shift $((OPTIND - 1))
cmd="$*"
if [[ $force != "no" ]]; then
# check for sudo
sudo_check=$(sudo -H -S -- echo SUDO_OK 2>&1 &)
if [[ $sudo_check == "SUDO_OK" ]]; then
eval sudo $cmd
exit $?
fi
fi
# get password
pass=$(yad --class="GSu" \
--title="Password" \
--text="Enter password for user <b>$user</b>:" \
--image="dialog-password" \
--entry --hide-text)
[[ -z "$pass" ]] && exit 1
# grant access to xserver for specified user
xhost +${user}@ &> /dev/null
# run command
fifo_in="$(mktemp -u --tmpdir gsu.empty.in.XXXXXXXXX)"
fifo_out="$(mktemp -u --tmpdir gsu.empty.out.XXXXXXXXX)"
LC_MESSAGES=C empty -f -i $fifo_in -o $fifo_out su $suargs $user -c "$cmd"
[[ $? -eq 0 ]] && empty -w -i $fifo_out -o $fifo_in "word:" "$pass\n"
exit $?
|
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: peterpas...@gmail.com
Is there (or could there be) any provision for which element of the dialog has focus?
This was my main gripe with Zenity, that (for instance in a question dialog) you could not set which of the 2 buttons was the default selected (it was OK and I needed No as default).
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: georgian...@gmail.com
Hi. It is possible that the source of a combo box to be a file?
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: ananasik
yes. use --rest option
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: georgian...@gmail.com
thanks, work fine.
... and how do I set default value for Combo Box ?
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: n3tw0rk...@gmail.com
Last edit: Anonymous 2017-07-10
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: nerunblogs
PassoUm(){
When i use this code, pasta=$(echo "$PASTA_FORM" | cut -d"|" -f 1) is always null. Doesn't matter wich folder i choose. What happens?
Uh, and /tmp/teste.tmp always write /home/user/Desktop
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: nerunblogs
SOLVED: i removed: --button="gtk-close:0" --button="gtk-ok:1"\ don't need it
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: ananasik
if you set your own buttons, remember that only buttons with even exit codes prints result. this is default behavior that may be changed by specifying --always-print-result.
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: hpp3.mar...@gmail.com
A few changes to the Run dialog example... I couldn't get it to populate the history, so swapped '--entry' and '--editable'. Now history shows up, but doesn't populate properly (spaces seen as new lines?), so I used the --rest option instead and read the $HISTFILE directly, eliminating the need for eval'ing. I also used a trick to put the last command used at the head of the file, so it shows up as the first entry in the combo box.
I changed the grep line because if a command is buried down in the history, I wanted to allow it writing to the file even if the command exists farther down; for convenience. I also think the file should be kept trim with a 'head -n' command; I tried to feed my bash history to it and it threw an error. ;)
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: ananasik
yes, now --rest is a better choice than perl && eval. and for adding command at the begin of history file, i use sed - sed -i "1 i $rcmd" $HISTFILE
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: jbs...@gmail.com
Anyone can help me with this?
So basically this BASH script works fine, Debian boots into a minimal Matchbox WM environment and this script is autorun which displays a nice graphical prompt allowing entry of AD username and pass, and then the vars get passed to pyhoca-cli which logs the computer into a remote x2go-server linux terminal server.. it works without issue.
However what I want to do is add an additional drop-down selection to the YAD graphical prompt (after the first two fields), where the user can select the session (eg kde, xfce4, lxde & fluxbox), and then pass that selection to the pyhoca-cli command to replace startkde with something like start$frmdata (from the dropdown selection)...
Not sure exactly how to do this...
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: macke...@gmail.com
Here is a process killer to select one (or more) pids and kill them:
!/bin/bash
TMPFILE=/tmp/$$.pstmp OUTFILE=/tmp/$$.psdata EMSG=/tmp/$$.errmsg
gen_data() {
}
while true do
done
rm -f $TMPFILE rm -f $OUTFILE rm -f $EMSG
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: yarling....@gmail.com
Hi. I created a start and stop button interface. So when I clicked on the start button, my program is executing but the interface is gone. How can I make the interface remain so that I can stop my program using the stop button in the interface? Thanks.
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: ekia...@gmail.com
It would be great if yad could display icons/tiny pictures in "lists" - so that one can for example display a thumbnail in a column, and the name of the file in another. Would that be possible to implement ?
I've read the man page and examples here and elsewhere, but really, there should be quick example usage for each option. Right now, I can't get --show-uri to work. Can anyone post a simple example of --show-uri in the --text-info dialogue? This doesn't work:
I'm trying to use a numerical input field with a range of -5 to +10 and with an initial value of -2. I can get yad to set the range with the -5..10 range but I cannot get the initial value to be a negative number. It always seems to start with an initial value of 0.
What am I doing wrong?
yad --title="Negative number test" --form --field="number:NUM" "(-2)!-5..10!1!0"
Last edit: Brian Stewart 2016-04-04
don't use brackets. separate data from options with two dashes if you have data started with dash
yad --title="Negative number test" --form --field="number:NUM" -- "-2!-5..10!1!0"
Thank you, thank you, thank you!!!
I knew this had to be something stupid on my part.
I had tried using variables, expressions brackets etc.
I'm new to bash and didn't know about the "--". I won't forget it now.
I've been a frequent user of zenity and just a few days ago decided to see what I could do with yad. Here is the fruit – a simple random password generator that lets the user pick symbol sets and some constraints. I'm sure there are better ways to do some of the things here, but it may be a helpful example for future yad users. This works with yad 0.38.2 on Ubuntu 17.10 (Artful).