ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I recently discovered notify-send which is great for easily displaying notifications from bash scripts. However it doesn't support adding actions to the notifications so I went looking for a way to do that and found pynotify. I have never used python and pynotify seems to be utterly lacking in supporting documentation, but from various examples I've found online I've managed to put together this:
If I run that and click on the button in the notification marked 'foo' then the notification closes, the python script exits and prints out 'blah'. Which is great. The problem is, if I close the notification by clicking on the X in the corner, or just clicking anywhere on the notification other than the 'foo' button, then the python script doesn't exit. And that's no good. I need the python script to exit when the notification is closed, not matter how the notification is closed.
Can anyone tell me how to achieve that?
I'm working in openSUSE 11.1 and there are no examples included in the python-notify package. Having found someone mention that the Ubuntu package includes example scripts I installed Ubuntu in a virtual machine and took a look. Unfortunately the example scripts suffer from the same flaw that my cobbled together attempt does - the scripts don't exit if the notification is closed by any means other than clicking on an action button.
Someone not registered here was kind enough to email me recently and gave me a solution that has partially worked for me. That being to added a default action. So now I have
With this, the script exits if the notification is clicked on anywhere other than the foo button or the X in the corner. Which is good. However I'm still stuck with the problem that if the notification is closed by clicking on the X in the corner, the script doesn't exit.
OK, I've solved this. Well not so much solved it found a script someone else wrote which does exactly what I need. It's called notify-more. It's basically notify-send implemented in python and with the additional massively useful functionality of allowing you to specify actions.
I've attached the script to this post because it is no longer hosted anywhere live. (It's called notify-more.txt because of the restrictions on the the names of uploaded files, rename it to notify-more.py or just notify-more.) I found the script mentioned on an archive of a mailing list where the author posted about it. The url they provided no longer exists but I did find a copy of it at the Internet Archive: http://web.archive.org/web/200710140...x/notify-more/
Example of using in a bash script with a couple of actions and then doing something based upon which action the user clicks:
Code:
#!/bin/bash
whattodo=$(notify-more -t 0 "hello" "world" -n "foo" -x "echo 'baaaa'" -n "moo" -x "echo 'goo'")
case "$whattodo" in
"baaaa" ) zenity --info --text "you clicked on foo";;
"goo" ) zenity --info --text "you clicked on moo";;
* ) zenity --info --text "you dismissed the notification without choosing an action";;
esac
Cheers that was helpful. I've downloaded the notify-more.py. But in your original code, what about if you get rid of all the gobject stuff? I.e. Don't import it, don't call gobject.MainLoop(), don't call loop.run() and don't call loop.quit(). I think that behaves OK and exits properly?
If I run that and click on the button in the notification marked 'foo' then the notification closes, the python script exits and prints out 'blah'. Which is great. The problem is, if I close the notification by clicking on the X in the corner, or just clicking anywhere on the notification other than the 'foo' button, then the python script doesn't exit. And that's no good. I need the python script to exit when the notification is closed, not matter how the notification is closed.
Can anyone tell me how to achieve that?
I'm working in openSUSE 11.1 and there are no examples included in the python-notify package. Having found someone mention that the Ubuntu package includes example scripts I installed Ubuntu in a virtual machine and took a look. Unfortunately the example scripts suffer from the same flaw that my cobbled together attempt does - the scripts don't exit if the notification is closed by any means other than clicking on an action button.
I've learned with the incredible notify-more what to do make your script work
With the help of this thread, I got what I want, which is very similar to what the original poster wanted. I just wanted an example of how to use it right though, so I thought I'd share that, without the additional py script.
Code:
#!/usr/bin/python
import pynotify
import gobject
def OnClicked(notification, signal_text):
print '1: ' + str(notification)
print '2: ' + str(signal_text)
notification.close()
global loop
loop.quit()
def OnClosed(notification):
print 'Ignoring fire'
notification.close()
global loop
loop.quit()
def Main():
pynotify.init('ProgramName')
global loop
loop = gobject.MainLoop()
notify = pynotify.Notification('Fire!', 'I\'m just kidding...')
# optional, just changes notification color
notify.set_urgency(pynotify.URGENCY_CRITICAL)
# optional, it will expire eventually
notify.set_timeout(pynotify.EXPIRES_NEVER)
notify.add_action('You Clicked The Button', 'Remove Fire', OnClicked)
notify.connect("closed",OnClosed)
notify.show()
loop.run()
if __name__ == '__main__':
Main()
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.