Handy Linux tools With Python

by Ostatic Staff - May. 09, 2013

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.

 #!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
import webbrowser

class TextEntry:

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(" ", "+")
webbrowser.open_new_tab(query)
exit()
return

# Main program to draw GUI
def __init__(self):

# create a new window
app_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
app_window.set_size_request(500, 100)
app_window.set_border_width(10)
app_window.set_title("Ducky")
app_window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
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.set_max_length(80)
entry_b.set_width_chars(60)
entry_b.connect("activate", self.enter_callback_b, entry_b)
entry_b.set_text(entry_checker_default_b)
entry_b.select_region(0, len(entry_b.get_text()))
entry_b.show()
hbox_b.pack_start(entry_b, False, False, 0)

hbox_b.show()
app_window.show()
return

def main():
gtk.main()
return 0

if __name__ == "__main__":
TextEntry()
main()

Drop me a line in the comments!