Notification Icon: change on click

385 views
Skip to first unread message

slo.sleuth

unread,
Sep 19, 2012, 11:59:09 AM9/19/12
to yad-c...@googlegroups.com
I'm trying to write a script that will change the notification icon when a condition is met (the existence of file).  To understand the yad --notification option, I am modeling the script at http://code.google.com/p/yad/wiki/NotificationIcon:

#!/usr/bin/env bash

# create a FIFO file, used to manage the I/O redirection from shell
PIPE=$(mktemp -u --tmpdir ${0##*/}.XXXXXXXX)
mkfifo $PIPE

# attach a file descriptor to the file
exec 3<> $PIPE

# add handler to manage process shutdown
function on_exit() {
    echo "quit" >&3
    rm -f $PIPE
}
trap on_exit EXIT

# add handler for tray icon left click
function on_click() {
    echo "icon:gtk-yes" >&3
}
export -f on_click

# create the notification icon
yad --notification                  \
    --listen                        \
    --image="gtk-no"              \
    --text="Notification tooltip"   \
    --command="bash -c on_click" <&3

The only change I made was to the on_click function, highlighted above.  I receive a bash error when I click the icon: 
bash: 3: Bad file descriptor

Can anyone explain to me what I'm doing wrong and tell me what I need to do to fix it?

Thanks,
John

slo.sleuth

unread,
Sep 19, 2012, 1:38:34 PM9/19/12
to yad-c...@googlegroups.com
It would appear, from the error, the fd 3 is not seen by the on_click function.  This confuses me, since its a sub-process to the process creating the fd.

slo.sleuth

unread,
Sep 19, 2012, 1:55:22 PM9/19/12
to yad-c...@googlegroups.com
Ananasik,  I see you addressed this issue in the notification wiki with:

bash file descriptors are not exported with function, so if you want to change state of notification icon from exported function, you must reopen $PIPE in functions body. and don't forget to export PIPE too

But, could you show what you mean by reopening and exporting $PIPE?

Thank you.

slo.s...@gmail.com

unread,
Sep 19, 2012, 2:47:44 PM9/19/12
to yad-c...@googlegroups.com
OK, I understand now:

#!/usr/bin/env bash

# create a FIFO file, used to manage the I/O redirection from shell
PIPE=$(mktemp -u --tmpdir ${0##*/}.XXXXXXXX)
mkfifo $PIPE

# attach a file descriptor to the file
exec 3<> $PIPE

# add handler to manage process shutdown
function on_exit() {
    echo "quit" >&3
    rm -f $PIPE
}
trap on_exit EXIT

# add handler for tray icon left click
function on_click() {
    exec 3<> $PIPE
    echo "icon:gtk-yes" >&3
}
export -f on_click
export PIPE

# create the notification icon
yad --notification                  \
    --listen                        \
    --image="gtk-no"                \
    --text="Notification tooltip"   \
    --command="bash -c on_click" <&3

I had to export the PIPE variable to be seen by the function, and then reopen the pipe to fd3 in the function.  The the icon command is then piped to yad which is listening for data from fd3.
Reply all
Reply to author
Forward
0 new messages