Tuesday, September 15, 2009

Raise a window to the front

I wanted to be able to detect in a python program if it was already running (pygtk) and bring it to the front if so. Thought this code could be useful to others:

#!/usr/bin/env python2.6
import sys, re, subprocess as sp, os
if __name__ == "__main__":
  program_name = os.path.basename(sys.argv[0])
  window_title_starts_with = 'My Application' # Change to the window title of your application
  p = sp.Popen([ '/usr/bin/pgrep', program_name ], stdout = sp.PIPE)
  out = p.communicate()[0]
  if p.returncode == 0:
    p = sp.Popen([ '/usr/bin/wmctrl', '-lp' ], stdout = sp.PIPE)
    out = p.communicate()[0]
    if p.returncode == 0:
      result = [ a[0] for a in 
        [ re.split('\s+', l, 4) for l in re.split('\n', out) ]
        if len(a) == 5 and a[4].startswith(window_title_starts_with) ]
      if len(result):
        print 'Application is already running, bringing to front'
        sp.check_call([ '/usr/bin/wmctrl', '-ia', result[0] ])
        sys.exit(0)

Note that paths are the ones used by Ubuntu. Requires pgrep and wmctrl to be installed.



© Copyright 2009 - Andrew Robinson.
Please feel free to use in your applications under the LGPL license (http://www.gnu.org/licenses/lgpl.html).

Wednesday, April 8, 2009

Links in Thunderbird will not open in existing Firefox

Just recently, Thunderbird was causing Firefox to open my ProfileManager instead of using my existing running firefox instance. So in order to address this, I did some research and found that there are "hidden" environment variables passed that cause Firefox to do this. I hate this behavior, so I found a work-around: Open the Thunderbird configuration editor and set the following:
  • network.protocol-handler.app.http to "/home/username/bin/ff"
  • network.protocol-handler.app.https to "/home/username/bin/ff"
(Replace the username with your username)

Then create the file "/home/username/bin/ff" as:

#!/bin/bash
unset MOZ_NO_REMOTE
unset MOZ_LAUNCHED_CHILD
unset MOZ_PIS_API
unset MOZ_PIS_MOZBINDIR
unset MOZ_PIS_SESSION_PID
unset MOZ_PIS_USER_DIR
/usr/bin/firefox $@

Remember to make it executable

Viola, it now works as it should.


© Copyright 2007 - Andrew Robinson.
Please feel free to use in your applications under the LGPL license.

Monday, February 16, 2009

Create a menu item for aptitude

I love Aptitude, it is the best UI package manager for Ubuntu and Debian functionality wise. Since it is a curses library, it doesn't have a menu item to run it. So for those that also like Aptitude and want to be lazy about creating a menu for it, here you go.

Just save this content to ~/.local/share/applications/aptitude.desktop:

  [Desktop Entry]
  Type=Application
  Encoding=UTF-8
  Name=Aptitude
  GenericName=
  Comment=Aptitude
  Icon=/usr/share/icons/oxygen/64x64/apps/system-software-update.png
  Terminal=false
  Exec=gksu -- /usr/bin/gnome-terminal --title=Aptitude --command=/usr/bin/aptitude --geometry=150x37
  Categories=System

Just tweak the geometry, which terminal you like and the Icon as you see fit.