Topic: Bash at your fingertips: nifty notify-send

First the How, then the What and the Why:

How:

For the impatient:

Copy

#!/bin/bash

info=$(eval "$*" 2>/dev/null)
notify-send -t $((1000+300*`echo -n $info | wc -w`)) -u low -i gtk-dialog-info "$*" "$info" || exit 2

into a text editor, then save it with a suitable name, say send.sh or note.sh. (I've named it "send", for reasons explained below.)

Make this script executable ('chmod +x send').
It also helps to put it somewhere in your $PATH. (~/bin, in my case)

Usage:
Hit Alt-F2, type in:

send your-bash-command-here

What:

A trivial script to run a Bash command and display the output as a Gnome notification. The time for which the notification stays is proportional to the length of the displayed output.

A typical example:

send acpi -V

http://omploader.org/tMXBmMwhttp://omploader.org/tMXBmNQ

For a hack this simple, it is more versatile than you may think; examples at the end of this post illustrate.
If you're turned off by the idea for some reason, do glance through the possible improvements listed at the end.

Why:

I'm a terminal junkie- I tend to gyrate towards CLI applications and commands whenever given a graphical option. Despite this, the following situation holds:

1. I spend most of my PC time in front of a (graphical flash/java enabled) browser or an IM window, or at a text editor. None of these offer easy access to terminal commands.

2. When I do need to verify or find some info at the command line, I'm forced to open a terminal emulator, enter the command, read the output (and possibly append to a file or copy to clipboard) and exit the terminal emulator.

There is thus no easy access to run non-interactive commands. Launching a terminal emulator is painful; Terminator takes about three seconds to launch on my semi-old hardware.

Several partial workarounds exist. For instance:

1. Amp up gmrun to run terminal commands. This doesn't work with non-interactive commands like 'ls'- the launched terminal quits immediately after ls exits, forcing me to type in 'ls && cat' and exiting the terminal with ^D after reading the output.

2. Use Conky to display all aspects of hardware status, frequently used files, etc. Apart from bloating up Conky, this fails to achieve the kind of utility required- see examples at the end.

3. Use Yakuake, Tilda, Guake or similar "roll-down" terminals. These eat into system resources, add bloat to #!, and finally, the temptation is strong to run interactive or 'persistent' commands (like 'tail -f' and 'htop') that effectively deny you Bash access.

4. Use Vim or Emacs. Both provide easy shell access from within the editor environment. Not a universal solution.

Hence this script.
What exactly it does is provide a querying system for your frequent needs. You ask Bash for information, it sends it across to you without interrupting your work-flow or requiring a special application. Also, it lingers exactly long enough for you to read it, so it takes away the tedium of dispensing with a window you no longer need.

Most of the script is self-explanatory. The $((1000+300*`echo -n $info | wc -w`)) part specifies the duration for which the notification should last in milliseconds. I assume the average reader takes 300 ms to read a word (180 words per minute) and have added an additional 1 second as a buffer. (Change the numbers to ones that match your reading speed.)

Examples:

It works especially well with scripts you already have. For instance, I have two Python scripts ("define" and "gcalc") that query Google for definitions of words/phrases and act as a front-end for Google Calculator, respectively. Combined with "send", these produce fantastic results:

send define rambunctious


http://omploader.org/tMXBmbQ

send gcalc "24500 INR in USD"


http://omploader.org/tMXBmbg

send gcalc "4/3 \* pi \* radius of sun^3 in km^3"

http://omploader.org/tMXBncQ

Some other nifty uses:

send df / -H


http://omploader.org/tMXBmcQ

send mocp --info


http://omploader.org/tMXBmcw

send "ls * | grep '\.sh$' "

http://omploader.org/tMXBncw
Note that typing in 'send ls | grep \.sh$' (without the double quotes) will not work- Bash will end up evaluating 'send ls' and piping the output to 'grep \.sh$'.

send echo 'Go to bed!' | at midnight

In this case, the quotes have been left out- because the intended behaviour is indeed what Bash will do anyway.

I suppose it's easy to find more examples.

Possible Issues & Improvements:

1. Having to type Alt-F2 and then enter the 'send' command is still too much work for us lazy ones. Perhaps someone can whip up a quick Zenity app that can be bound to a key-combination? The problem with this is that Zenity does not autocomplete entries like Gmrun (or whatever application launcher you use) does.

2. Better unicode support. Notifications fail to show if the output text contains non-ASCII characters.

3. I haven't tested this on the new notification system in use in Ubuntu 9.04 and variants. (I'm running 8.10). Let me know if it works!

4. Since the script is just two lines, it might be a good idea to define it as a function in ~/.bashrc instead of a script, assuming every system you use will run the Gnome notification daemon. It certainly beats mucking round with $PATH.

Any feedback, suggestion or improvement is welcome.

Last edited by karthik (2009-05-21 10:13:34)

Re: Bash at your fingertips: nifty notify-send

Now this I've got to try as soon as I have time!
And for an improvement, how 'bout rethinking it so it can be added into the obmenu? You'll have to put it there initially, ofc, but then it'll only ever be a menu away smile

"History is the fiction we invent to persuade ourselves that events are knowable and that life has order and direction."

Re: Bash at your fingertips: nifty notify-send

I'll give this a try in a few minutes with the new notifications...I'm pretty sure they don't let you specify a timeout though, and I don't know how they handle overflow since I don't think they resize....let you know in a little....Oh btw awesome work

edit: do you mind posting your scripts to the pastebin also? they sound usefull

Last edited by iggykoopa (2009-05-20 19:00:20)

I say never be complete, I say stop being perfect, I say lets evolve, let the chips fall where they may.

Re: Bash at your fingertips: nifty notify-send

Savonlinna wrote:

Now this I've got to try as soon as I have time!
And for an improvement, how 'bout rethinking it so it can be added into the obmenu? You'll have to put it there initially, ofc, but then it'll only ever be a menu away smile

Isn't it already just a menu away? ([Obmenu]>Run Program>"send my-bash-command")

No matter how lazy the user gets, (Obmenu or not), she/he will still need to invoke a text box of some kind, and fill in the "send" command. smile

Re: Bash at your fingertips: nifty notify-send

Very nerdy l33t and ub3r c00l.  I may have some uses for this type O' thang.

I view KDE like I view snow. It looks fun and marvelous, it's fun to play in, but after a while I just want someone to take it all away.

Re: Bash at your fingertips: nifty notify-send

@iggykoopa:

define.py : queries Google for word definitions. As a side note, this is over 200 lines long (HTML processing is a pain) and took me an entire weekend to cook up. Feels unwieldy in Pastebin, any idea where I can put this up so it's easily accessible to someone searching on Google for this sort of thing?

gcalc.py : Command line Google Calculator (not my script).

Re: Bash at your fingertips: nifty notify-send

after a little bit of testing it still seems to be working pretty well with the new notifications...I'm pretty sure it doesn't honor the time you give it though.

I say never be complete, I say stop being perfect, I say lets evolve, let the chips fall where they may.

Re: Bash at your fingertips: nifty notify-send

gcalc and define are both working good here, the text from define tends to get cut off after about a paragraph though...but it may be my small screen. Also both of the scripts had a bad character after the #!/usr/bin/python line
bash: /home/eric/bin/gcalc: /usr/bin/python^M: bad interpreter: No such file or directory

but it was easy to fix. For hosting the files you could try dropbox or something like that, I've used it in the past for sharing files. If anyone has ideas for other plugins/scripts I'd be happy to work on them as I need the practice too.

edit: forgot to say how I fixed it for anoyone else...just go to the end of python and delete both newlines until the comments, then just hit enter twice to put them back and save it...must be something the pastebin did to it.

Last edited by iggykoopa (2009-05-20 19:44:39)

I say never be complete, I say stop being perfect, I say lets evolve, let the chips fall where they may.

Re: Bash at your fingertips: nifty notify-send

iggykoopa wrote:

after a little bit of testing it still seems to be working pretty well with the new notifications...I'm pretty sure it doesn't honor the time you give it though.

I concur.

"History is the fiction we invent to persuade ourselves that events are knowable and that life has order and direction."

Re: Bash at your fingertips: nifty notify-send

iggykoopa wrote:

after a little bit of testing it still seems to be working pretty well with the new notifications...I'm pretty sure it doesn't honor the time you give it though.

I intend to upgrade to 9.04 soon. Will look up the (new) notify-send man page and see if there's any way to specify a timeout.

(Also, 'send fortune' is very amusing, works great if you set up your crontab to run it every twenty minutes.)

Re: Bash at your fingertips: nifty notify-send

from what I have read there currently isn't anyway to change the timeout on the new updates. They did it that way purposely, not sure why though.

I say never be complete, I say stop being perfect, I say lets evolve, let the chips fall where they may.

12

Re: Bash at your fingertips: nifty notify-send

I just wanted to point this out because it makes me feel witty

Run this in a terminal or Alt+F2

send send send send send send send ls

Re: Bash at your fingertips: nifty notify-send

karthik wrote:
send "ls * | grep '\.sh$' "

http://omploader.org/tMXBncw
Note that typing in 'send ls | grep \.sh$' (without the double quotes) will not work- Bash will end up evaluating 'send ls' and piping the output to 'grep \.sh$'.


But isn't that fairly easy to fix?
Wouldn't you just type this instead?

 ls * | send grep '\.sh$'

?

Last edited by michael.h4lios (2009-06-17 15:54:42)

14

Re: Bash at your fingertips: nifty notify-send

This is great - I'd been looking for something useful to do with notify-send, and this is exactly it.
Thanks for sharing!

Re: Bash at your fingertips: nifty notify-send

michael.h4lios wrote:

But isn't that fairly easy to fix?
Wouldn't you just type this instead?

 ls * | send grep '\.sh$'

?

D'oh. Can't believe I missed that. Thanks, I'll update the opening post.