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: nilarimo...@gmail.com
Thank you!
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: you.hear...@gmail.com
You should really push this into Debian main repositories, cause its such a great idea.
Keep up the good work!
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: hobbs....@gmail.com
Great work. +1 for pushing through to repo's Here's a Desktop Colour-Picker written for yad, for your pleasure! http://code.google.com/p/colour-picker/
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: zac...@free.fr
Very good utility to create very simple dialogs (the goal)
some ideas :
1) Would it be possible to have gui events (key pressing, mouse clicking, object content change, ...) call an external script or binary (once), able to communicate with the gui through stdin/stdout ?
widgets changes requests sent by program stdout to the gui widgets status/contents enquiry sent by program stdout to the gui
asynchronous events sent by the gui to the program stdin widgets status sent by the gui to the program stdin
usage examples : a "clear field" button, dimming or hiding some object when a checkmark is clicked, calculating a field from other ones ?
2) would it be possible to arrange widgets in std hbox/vbox gtk way ? and to have gtk frames to visually group objects ?
(or perharps is all this already possible ?)
thank you very much for this tool.
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: stu...@gmail.com
Hi Ananasik, Let me start by saying how great Yad is, i have been using it a lot on Puppy Linux to great effect - i wonder though if it would be possible to request an added feature for Yad - for more complex GUI's that require quite a lot of user inputs an option that allows user inputs or vertical columns of user inputs to be side by side in the same GUI to make the most of available screen space? example: field=(left) field1=(right) So you you have two vertical rows from left to right in the same GUI increasing user inputs from 14 on my 15 inch laptop screen to 28 user inputs.
I hope this makes sense? Thank you.
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
thanks for idea. i think, it would be better to specify a number of columns in form and direction how to fill cells with fields
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: de067018@googlemail.com
Hi Ananasik, great stuff & kudos to you. It's far better than zenity I used before. I have two questions: Is there a windows port of yad and how can I show access keys in yad dialogs. Thx and keep up your work.
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: azd...@googlemail.com
Hi Ananasik,
Just want to say thanks for yad. I had used zenity but now I have moved to using yad. I have an application that needs to display notifications via dialogs on-top and yad does the job really well.
My problem is with multiple dialos. If I display one dialog, then moments later I display another, at the moment the first dialog is the only one visible on top (the other one is hiding underneath). I'd really like it that the latest dialog always get to the top. Any suggestions?
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
placing windows is a window manager tasks. yad's --on-top option just set a corresponding window property, to tell wm how to place this window. you may play with setting window names of your dialogs and set right placement policies for them in your window manager's configuration
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: christri...@gmail.com
Hello Ananasik,
I have this code below and i need when i press a button to run a function, to connect every button with a different function, how i can do 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
in a short - just use extra arguments for sets commands for buttons in the same order as fields specified. for example:
yad --forme --field "button1:btn" --field "button2:btn" --field "button3:btn" "echo 'button1 pressed'" "echo 'button2 pressed'" "echo 'button3 pressed'"
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: christri...@gmail.com
i cannot make it work...could you point out my mistake?
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 wish to use function as a command, you must export it and call as a "sh -c function". so your example will be
button1 () { echo "button 1 is pressed" } export -f button1
yad --form --field "button1:btn" --field "button2:btn" --field "button3:btn" "sh -c button1" "echo 'button2 pressed'" "echo 'button3 pressed'"
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: christri...@gmail.com
i will try it...thanks! yad is awesome! :)
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: christri...@gmail.com
i copy&paste your code and i get
syntax error: unexpected end of 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
this is bad formatting
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: christri...@gmail.com
now i get
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: christri...@gmail.com
i replace sh with bash and its ok now... i will came up later with other questions :P
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: christri...@gmail.com
hello again :P now i have 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: ananasik
just press ok button. or do you want to get current field value without closing dialog?
p.s. and i think, the maillist (http://groups.google.com/group/yad-common) is a more correct place for such discussions
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: MeissS...@gmail.com
Simple alert tool, wrote this yad, will i share here ;)
Enjoy, and thanks for yad
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: tiong1...@gmail.com
Hi~ Is there any version of yad for microsoft windows?
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
there was an issue about successful build of yad on ms windows - http://code.google.com/p/yad/issues/detail?id=56
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: satyajit...@gmail.com
Hi. The color chooser dialog doesn't return the transparency value. Can it be made to output the value in rgba instead of hex?
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: oren.bod...@gmail.com
Hi all. Does "yad" works with RedHat? OS ?