LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-11-2009, 01:53 PM   #1
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
pynotify and getting a python script to exit


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:

Code:
#!/usr/bin/python
import gobject
import pynotify


def foo_cb(n, action):
        print "%s" % (action)
        n.close()
        loop.quit()


if __name__ == '__main__':
        pynotify.init ("foo")
        loop = gobject.MainLoop ()
        n = pynotify.Notification("title", "text")
        n.set_urgency(pynotify.URGENCY_CRITICAL)
        n.set_timeout (pynotify.EXPIRES_NEVER)
        n.add_action ("blah", "foo", foo_cb)
        n.show()
        loop.run ()
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.
 
Old 07-15-2009, 01:42 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Original Poster
Rep: Reputation: 198Reputation: 198
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

Code:
#!/usr/bin/python
import gobject
import pynotify


def foo_cb(n, action):
       print "%s" % (action)
       n.close()
       loop.quit()

def default_cb(n, action):
 n.close()
 loop.quit()


if __name__ == '__main__':
       pynotify.init ("foo")
       loop = gobject.MainLoop ()
       n = pynotify.Notification("title", "text")
       n.set_urgency(pynotify.URGENCY_CRITICAL)
       n.set_timeout (pynotify.EXPIRES_NEVER)
       n.add_action ("blah", "foo", foo_cb)
       n.add_action("default", "Default Action", default_cb)
       n.show()
       loop.run ()
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.
 
Old 07-23-2009, 04:54 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Original Poster
Rep: Reputation: 198Reputation: 198
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
Attached Files
File Type: txt notity-more.txt (8.8 KB, 74 views)
 
Old 08-12-2009, 01:51 PM   #4
Myrmornis
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 3

Rep: Reputation: 0
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?
 
Old 04-28-2010, 10:29 PM   #5
tristangrimaux
LQ Newbie
 
Registered: Apr 2010
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by arizonagroovejet View Post
...

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

Code:
n.connect("closed",yourfunction_cb,"action")
before the n.show and that's it!
 
Old 01-25-2012, 06:36 PM   #6
jeffeb3
LQ Newbie
 
Registered: Jun 2007
Posts: 3

Rep: Reputation: 0
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()
Tested working on SLED SP1.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python exit loop by 'enter' sycamorex Programming 7 08-29-2008 09:44 PM
logout/exit script ?? aherrys Linux - Software 3 12-30-2006 11:42 AM
call another script and have the inital script exit mjtice Programming 1 09-11-2005 11:54 AM
Embedding Python in C - adding a exit routine to a module? redarrow Programming 0 08-10-2005 01:20 PM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration