If it won't be simple, it simply won't be. [source code] by Miki Tebeka, CEO, 353Solutions

Wednesday, August 29, 2007

Simple launcher for XFCE


I'm using xfce4 as a window manager, it has a nice launcher called xfrun4, however it only tries to execute applications.

I've written a little launcher that uses exo-open to open it's argument. This way I can open PDF files, directories etc.
(It's not as fancy as Mac's QuickSilver or Windows SlickRun, but it does the job)

In order to get quick access, open the keyboard settings, create a new theme (you can't change the default) and add the launcher there. I usually go with "CTRL-SHIFT-K" to activate.


#!/usr/bin/env python
'''Simple lanucher'''

from Tkinter import Tk, Label, Entry
from tkFont import Font
from tkMessageBox import showerror

from os.path import exists, expanduser
from os import environ, popen

def launch(name):
name = expanduser(name)
if not exists(name):
fullname = popen("which %s 2>/dev/null" % name).read().strip()
if not fullname:
raise ValueError("can't find %s" % name)
name = fullname

popen("/usr/bin/exo-open \"%s\"" % name).read()


USER_CANCEL = 0
ROOT = None
COMMAND = None

def quit(event):
global USER_CANCEL

USER_CANCEL = 1
ROOT.quit()

def build_ui():
global ROOT, COMMAND

ROOT = Tk()
ROOT.title("Launchie")
ROOT.bind("<Escape>", quit)
COMMAND = Entry(width=80, font=Font(size=14))
COMMAND.pack()
COMMAND.bind("<Return>", lambda e: ROOT.quit())

def show_ui():
global USER_CANCEL

USER_CANCEL = 0
COMMAND.focus()
ROOT.mainloop()

return COMMAND.get().strip()


def main(argv=None):
if argv is None:
import sys
argv = sys.argv

from optparse import OptionParser

parser = OptionParser("usage: %prog")

opts, args = parser.parse_args(argv[1:])
if len(args) != 0:
parser.error("wrong number of arguments") # Will exit


build_ui()

while 1:
try:
command = show_ui()
if USER_CANCEL:
raise SystemExit

if not command:
showerror("Launchie Error", "Please enter *something*")
continue

launch(command)
break
except ValueError:
showerror("Lanuchie Error", "Can't launch %s" % command)

if __name__ == "__main__":
main()


The reason I chose Tkinter is that it's already installed with Python and it's good enough for simple stuff like this.
Post a Comment

Blog Archive