Monday
5 Feb 2007
Creating Your Own Open Commands With Enso Launcher
We’ve received a lot of requests from our programming-oriented users asking us when they’ll be able to create their own commands with Enso.
As Jono mentioned quite emphatically in the Enso Tour Movie, the answer is: soon!
But for the time being, you can already implement certain kinds of commands for Enso Launcher, as long as you know a programming language that can operate on command-line arguments. I’ll show you how to make a simple one with a little code in our language of choice, Python. Even if you don’t know Python, you should be able to get the general gist of what’s going on and be able to port it to your favorite programming language, as Python is notoriously easy to read.
Enso Launcher is a versatile tool for starting any kind of service. And every time you use the “open with” command, whatever your current selection is—whether it’s a file in Windows Explorer or some text in Microsoft Word or Mozilla Firefox—the text representation of that selection gets passed as a parameter to the service that is launched. If you have a file selected, the absolute pathname of the file is passed to the program as a parameter; if you have text selected, that text is passed as the parameter. You can create small programs that take advantage of this mechanism to create your own commands that operate on selections.
As an example, we’ll create a simple Wikipedia-oriented service that can be invoked through the commands “open wikipedia” and “open with wikipedia”. If “open wikipedia” is invoked, the Wikipedia home page is opened in the user’s web browser; if “open with wikipedia” is invoked, then the Wikipedia entry for the selected text is displayed (or search results, if there are no exact matches).
We’ll start out with a utility function, which we’ll place in a module that we’ll call EnsoHelpers.py:
import webbrowser
import sys
import urllib
def gotoUrl( url, openWithUrl ):
"""
Depending on the command-line parameters, opens one of the two given URL's.
url -- The URL that is launched if no command-line parameters are supplied.
openWithUrl -- The URL that is launched if a command-line parameter is
supplied. The string "${TARGET}" in this URL is replaced with the
text contents of the command-line parameter.
"""
if len( sys.argv ) > 1:
replaceText = urllib.quote( sys.argv[1] )
openWithUrl = openWithUrl.replace(
"${TARGET}",
replaceText
)
webbrowser.open( openWithUrl )
else:
webbrowser.open( url )
The purpose of this function is pretty self-explanatory, given its docstring.
The EnsoHelpers module really does the bulk of our work. To create the service, just make a file called
Wikipedia.pyw that contains the following code:
from EnsoHelpers import gotoUrl
gotoUrl( "http://en.wikipedia.org",
"http://en.wikipedia.org/wiki/${TARGET}" )
Now just select Wikipedia.pyw in Explorer and issue the “learn as open wikipedia” command, and you’re done. You can now select any text, anywhere, anytime, and use it to look up the name of an article in Wikipedia.
It’s not hard to think up lots of uses for a mechanism like this; feel free to post any practical ones you come up with here.
COMMENTS
25 Voices Add yours below.