Handy Linux tools With Python
One handy tool I've mised since moving over to Ubuntu for my desktop workstation is Quicksilver's ability to perform a quick DuckDuckGo search using hotkeys. I looked into a few Linux alternatives like Gnome-Do, which I used to swear by, and the newer Synapse, but neither seemed to fit quite right for me. However, since Python is so well supported in Gnome, it turns out to be trivial to write my own.
Python's gui scripting support is very good. To get started, I grabbed a basic skeleton from YoLinux that popped up a box for text entry. Using this as a base, I modified the code to remove the "Enter text:" label and center the box on each launch. Next, I made sure that the code only grabbed the input when the user hit the return key. Once I had what the user was searching for, I replace the spaces with plus signs, and constructed a URL for a DuckDuckGo query. Finally, I used Python's "webbrowser" module to open the query in my default web browser, Firefox.
Once everything was running fine executing the script from the command line, I went hunting for a way to map the script to a keyboard combo. I initially thought I was going to have to start editing X11 config files, or install another daemon that listened for hotkeys, but I should have known better. It turns out that this is a trivial task, and I only needed to open the keyboard preferences and select the "Shortcuts" tab. Then I created a new custom shortcut mapping Ctrl+Space to my Ducky.py script. Works like a charm.
Right after I saw this work, a little lightbulb went off in my head. Mapping hotkeys to shell scripts? What else can I automate like this? You can use the script below if you care to try this out, and I'd love to hear what kind of custom keyboard shortcuts you use to enhance your Linux desktop.
def enter_callback_b(self, widget, entry_b):
entry_text_b = entry_b.get_text()
query = "http://duckduckgo.com/?q=%s" % entry_text_b.replace(" ", "+")
# Main program to draw GUI
# create a new window
app_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
app_window.connect("delete_event", lambda w,e: gtk.main_quit())
# Set default text in text entry box
entry_checker_default_b = "Search DuckDuckGo"
# Text label
hbox_b = gtk.HBox(False, 0)
app_window.add(hbox_b) # attach HBox to window
# Generate text entry box
entry_b = gtk.Entry()
entry_b.connect("activate", self.enter_callback_b, entry_b)
hbox_b.pack_start(entry_b, False, False, 0)
if __name__ == "__main__":
Drop me a line in the comments!