Snippets
From Woofgui
(Difference between revisions)
Line 1: | Line 1: | ||
+ | ==is script run from desktop or console== | ||
+ | |||
+ | #!/usr/bin/env python | ||
+ | import os | ||
+ | |||
+ | # Returns '1' for /sbin/init if our script was launched from a desktop-environment and will return a process-id higher than 1 if the script was launched from a console - This way, we now have a method to deliver GUI-output / console-output accordingly! | ||
+ | parentprocessid = os.getppid() | ||
+ | |||
==create notify-bubble using dbus== | ==create notify-bubble using dbus== | ||
Revision as of 13:38, 15 March 2009
Contents |
is script run from desktop or console
#!/usr/bin/env python import os # Returns '1' for /sbin/init if our script was launched from a desktop-environment and will return a process-id higher than 1 if the script was launched from a console - This way, we now have a method to deliver GUI-output / console-output accordingly! parentprocessid = os.getppid()
create notify-bubble using dbus
#!/usr/bin/env python import dbus import sys # Use session bus session_bus = dbus.SessionBus() # Use notifications object notifications_object = session_bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications') notifications_interface = dbus.Interface(notifications_object, 'org.freedesktop.Notifications') # Sample notification notification_id = notifications_interface.Notify(sys.argv[0], 0, , 'Sample summary', 'Sample body', dbus.Array([], signature='s'), dbus.Array([], signature='(sv)'), -1) # Sample notification with actions and hints notification_id = notifications_interface.Notify(sys.argv[0], 0, , 'Sample summary', 'Sample body', ['sample', 'Sample action'], {'x': 100, 'y': 100}, -1)
create icon in systray with notification-bubble using pynotify
#!/usr/bin/env python import gtk import pygtk import pynotify def callback(icon): notification.show() pynotify.init("Woofgui") notification = pynotify.Notification("Title", "Text in body", "dialog-warning") # the dialog-warning specifies icon type notification.set_urgency(pynotify.URGENCY_NORMAL) notification.set_timeout(pynotify.EXPIRES_DEFAULT) # _NEVER sets timeout to never, user has to click icon = gtk.status_icon_new_from_stock(gtk.STOCK_ABOUT) icon.connect('activate', callback) notification.attach_to_status_icon(icon) gtk.main()
test for desktop-environment
#!/usr/bin/env python import os # Returns 'gnome' or 'kde' or ... desktoptype = os.environ.get('DESKTOP_SESSION')
find out user-language
#!/usr/bin/env python import os # e.g. returns de_DE.UTF-8 usedlanguage = os.environ.get('LANG')
get user-home directory
#!/usr/bin/env python import os # Returns /home/username homepath = os.environ.get('HOME')
example wxpython textentry
#!/usr/bin/env python import wx app = wx.PySimpleApp() dlg = wx.TextEntryDialog(None, 'Enter value', 'Title', ) if dlg.ShowModal() == wx.ID_OK: val = dlg.GetValue() # this line should be indented dlg.Destroy() print "you have entered %s" % val
(OLD: test if kde or gnome)
#!/usr/bin/env python import subprocess as data p1 = data.Popen(["ps", "-e"], stdout = data.PIPE) p2 = data.Popen(["grep", "kdesktop"], stdin = p1.stdout, stdout = data.PIPE) if p2.stdout.read() != : print 'kde' else: print 'gnome'