I consider ditionaries, either in printed or electronic form, as one of the most useful sources of valuable information and an indispensable companion when reading. Lately, I’ve been reading lots of stuff, which involved scientific terminology in the English language, so it was very often required to lookup words on various online dictionaries. I did most of the reading using epiphany or evince. Epiphany has a very handy feature which can send the selected text to a smart bookmark or even to gnome-dictionary. Unfortunately, evince lacks such features, so I had to manually copy and paste words to the web browser in order to look them up on the web. This, combined with evince’s performance issues (version 0.6.0 – Fedora Core 6), led me to look for a more universal method of looking up words or phrases, regardless of the document viewer. And, usually, when your sole ambition becomes to overcome an annoying situation, a solution is on the way.
What I needed was the following:
- Select some text with the mouse (easy :P)
- Trigger a dictionary lookup for the selected text.
- Get the word’s definitions in a browser window.
After some research on the web, I came up with a decent solution. I discovered that when some text is selected under X, it is temporarily stored in a location called “Primary Clipboard“. The text could be somehow retrieved from that location and could be sent to an online dictionary by pressing a specific keyboard combination. This is totally independent of the application which the text had been selected from. This method also makes dictionary lookups as easy and fast as it can get.
But, this was just in theory. I needed a way of retrieving the selected text from the primary clipboard programmatically. This was the most difficult part, as X or GTK programming is out of my league…
Fortunately, I was told that this exact functionality exists in the deskbar applet. The user can select some text, then press Alt-F3 and the deskbar applet displays a list of pre-defined places (epiphany smart bookmarks included) to search for the selected text, which also includes gnome-dictionary in order to look up the word on a dictionary server.
Using this deskbar applet’s functionality for the dictionary lookups could be the perfect solution. But, on the other hand, being able to specify different key combinations for different dictionaries and not having to pick a search location from the deskbar’s list would be more than perfect. Grepping into deskbar applet’s source code I soon found what I was looking for in the first place: that is the way to programmatically retrieve the selected text from the primary clipboard. Of course, I shamelessly stole that small piece of code :P
Provided that some text has been selected, its retrieval is possible with the following python snippet:
import gtk clipboard = gtk.clipboard_get(selection = "PRIMARY") text = clipboard.wait_for_text() print text
The rest was easy. What was needed was a key combination to trigger the execution of the above script. Unfortunately, a custom command cannot be set in the gnome-keybinding-properties application. So, a workaround was needed even for that!! After some research, it turned out that one can set a custom command and assign a keyboard shortcut for it in GNOME’s window manager, metacity.
So, finally, it worked! Read on for detailed info…
How to set things up
First of all, create a small script:
#! /usr/bin/env python # -*- coding: utf-8 -*- command = "epiphany -n http://en.wiktionary.org/wiki/%s" #command = "epiphany -n http://dictionary.reference.com/search?q=%s" import gtk import os from urllib import quote clipboard = gtk.clipboard_get(selection = "PRIMARY") text = clipboard.wait_for_text() text = text.strip() if text: text = quote(text) # This is basically for multiword selections os.popen(command % text)
Save the script to a directory of your choice. The rest assumes that the script has been saved to:
What this does is to retrieve the selected text from the “primary clipboard” and execute the specified command. In this case, the command is actually a system call to the epiphany browser which is instructed to load a page with the possible definitions for the selected text. The English version of Wiktionary or Dictionary.com (whichever is uncommented) is used for the lookup. Note that multiword selections are supported by both the script above and the aforementioned online dictionaries.
Second of all, a global keyboard shortcut needs to be set in metacity for the reasons I explained previously.
First, the custom command needs to be set. Open the gconf-editor and browse to the key:
Of course use any command key you wish. Here
command_1 is used. Set the following value:
Then set up the keybinding for that command. Browse to the key:
And set the combination (the following is an example):
Test it by selecting some text anywhere and by pressing
Ctrl-Alt-g. A new tab will appear in the browser window with a page with the selected text’s definitions (if any).
Appendix I: Using a dictionary server
Instead of looking the selected text up on a web-based dictionary, you can retrieve definitions from a dictionary server. Adjust the script accordingly. For example:
#! /usr/bin/env python # -*- coding: utf-8 -*- command = "echo 'DEFINE * \"%s\"' | nc dict.org 2628 | zenity --text-info --title='Dict Lookup' --width=500 --height=500" import gtk import os clipboard = gtk.clipboard_get(selection = "PRIMARY") text = clipboard.wait_for_text() text = text.strip() if text: os.popen(command % text)
The following happens when the keyboard shortcut is pressed:
- The following dictionary protocol specific command: DEFINE * “selectedtext” is piped to netcat (see some netcat examples).
- Netcat opens a connection to the
dict.orgdictionary server, sends the command and retrieves the results.
- The results are piped to zenity and are displayed in an information textbox.
Obviously, in order to successfully run the above command you will need netcat (nc) and zenity.
A whole dictionary application can be built around the selected text that is stored at the primary clipboard. The above is the fastest and most effective way of retrieving word/phrase definitions from any possible dictionary. It is independent of the application from which the text is selected and no time is wasted in meaningless copy-pasting. If you know a better way of looking up words, please let me know.
Dictionary Lookups Anywhere by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2007 - Some Rights Reserved
You could also use xclip to get the X selection:
This is very nice. I was not aware of xclip and I guess it would have taken me ages to find out about it :P
Now, creating a BASH script is rather trivial.
Thanks for your comment.
Not a program, but my favorite online dictionary tool is onelook. It’s a dictionary metasearch that searches 900+ dictionaries, so it basically always has a definition. It searches the two dictionaries that you used, also the jargon file and the online etymology dictionary, which i find helps me remember words much better than just reading the definition. The only problem is that for some words the first screen that you get doesn’t have a definition, and you have to click through to get a definition. Most of those words just aren’t defined in most dictionaries though, so it’s actually often less work. And it’s a small price to pay to be able to quickly see multiple definitions and for always getting the answer.
Should this work on windows? Whenever I run the first script with some text selected, ‘None’ is always the output…
I have not tried it. I would expect that the gtk functions would have been adjusted to the mechanisms that Windows use to deal with the selected text. I recommend asking this question to a gtk mailing list for a better answer. If you find out anything interesting and if it’s not trouble for you, please make another comment with your findings.