Every time you use the "open with" command, the text representation of your selection gets passed as a parameter to the service that is launched.

Monday
5 Feb 2007

Creating Your Own Open Commands With Enso Launcher

Our Products

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.

by Atul Varma



COMMENTS

25 Voices Add yours below.


I really like this and trying to learn how to make my own commands. I tried what you posted and need more noobish intrustions.

I like to make one you can search on IMDB.com or WOWHead.com stuff like that.

I think skinning whould be cool too. Maybe options to center the box and or move to left right bottom or top etc. Change colors to match with desktop themes. Be cool to make it center and match destop LOL like a Star Trek LCARS style screen look.

Anyways I really hope see more cutmizing stuff and I sure would pay for this.

Keep up good work!


I must be missing something. I created both files and ran the learn as open wiki command. I am able to get Wikipedia to open in my browser, but it won’t use any selected text. For example, if I select this text coffee and type open wiki, ENSO opens up the default page, not the Coffee entry.

Has anyone been able to implement this successfully?

Also, it would be nice to be able to use a search term in the ENSO command line (much like the google command) in addition to selected text. I’m guessing this can’t be done with this technique and will have to wait for custom-command support.

Thanks,
shastafir


I had to install Python to get this to work for me. After which, I have the same issue as shastafir.


Shastafir and Brent, you’ve both implemented this correctly—the only problem is that you have to use “open with wikipedia” to open your current selection with Wikipedia. “open wikipedia” will always open the Wikipedia home page.

Thanks for the compliments, Ryan; the instructions provided in this blog post are really geared towards programmers—once we roll-out the mechanism that allows users to make their own arbitrary commands, you’ll start seeing commands like the ones you suggested for IMDB and Wowhead.


Brilliant Atul! Works fine. Not so brilliant on my part. It’s clearly stated above that I need to use open with.

I look forward to custom commands. This takes 16 keystrokes to execute. I can achive this in 2 with my Windows key.

Thanks for the help.
shastafir


This is great and works well. Now could you give us an example of a command that replaces text in a document, like the “calculate (four function)” command? Thanks!


I’d also like to echo Shastafir’s comment that it would be *great* to have the ability to send arguments to Enso commands simply by typing them after the command name in the quasimode, without having to select text. It gets pretty messy if you first have to type some text, then select it, and then type “open with X”.

I also can’t wait until we can substitute shorter command names. I completely agree that it makes sense to start people out with plain English commands, but given that Enso is likely to be popular among rather computer-savvy types, I would expect many users to want to customize (i.e. shorten) the command names.


Man, I thought I was going to be all Ruby all the time. Now, where’s that darned Python tutorial again?

Nice work, guys. Good to see some fun stuff with style, grace, and (most of all) real utility for the Windows platform.


Ryan,

Here’s all you need to search IMDB with.

from EnsoHelpers import gotoUrl

gotoUrl (	"http://imdb.com/",
         	"http://imdb.com/find?s=all&q=${TARGET}" )

If your interested, let me know and I can walk you through the install…

shastafir


I would like to say thanks to the Humanized team for a terrific product in Launcher.

Until we get more exposure to published Enso APIs, I wanted to post a simple solution in regards to the thread above that extends
Atul’s original Wikipedia example and provides the functionality some have
expressed having.

In EnsoHelpers.py:

import webbrowser
import sys
import urllib

def gotoUrl( url, openWithUrl, searchText ):
    """
    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 in the form of the searchText string from the CMD prompt or a
	highlighted string.	The string "${TARGET}" in this URL is replaced with
	the text contents of the command-line parameter.
    """
    if len ( searchText ) > 1:
        webbrowser.open ( constructUrl ( openWithUrl, searchText ))
    else:
        if len( sys.argv ) > 1:
				webbrowser.open ( constructUrl ( openWithUrl, sys.argv[1] ))
        else:
                webbrowser.open( url )

"""
	Returns a properly constructed URL that contains the contents of the
	replacementText embedded in the given urlToMash
"""
def constructUrl ( urlToMash, replacementText ):
	replaceText = urllib.quote( replacementText )
	return urlToMash.replace("${TARGET}", replacementText)

Rename the Wikipedia.pyw file to Wikipedia.py (removing the w) to ensure
command prompt interaction.

In Wikipedia.py:

from EnsoHelpers import gotoUrl

strInput = raw_input("Search Wikipedia for: ")
if (strInput > 1):
        gotoUrl("http://en.wikipedia.org", "http://en.wikipedia.org/wiki/${TARGET}", strInput)
else:
        gotoUrl("http://en.wikipedia.org", "http://en.wikipedia.org/wiki/${TARGET}", "")

Now simply use Enso Launcher’s learn as open command as mentioned before to attach to the
Wikipedia.py file.

Elegant: no :(
Sexy: no :(
Effective: :)

To use:
open *your learn as command*: Opens a command-prompt (hence the not elegant
rating) and allows you to type a search team to be sent to Wikepedia on hitting
the return key. If you enter nothing, it simply opens Wikipedia in your default
browser.

open with *your learn as command*: If you highlight some text in a document or
web page, open with will also open a command-prompt (hence the not
sexy rating) but simply hitting the return key will execute a search of the
highlighted term in Wikipedia.

There may be more elegant ways to do this in python; however, I had to revisit
the language after many years in other languages to post this example. I
welcome how new products and technologies encourages developers to explore other directions which I am sure brings smiles to the Humanized team. Enjoy Enso, I do!


Thanks to the Humanized Team for such an amazing product. I haven’t come across a more useful app in a really really long time (not since I discovered Cygwin).

Anyways, I like to do things in Perl and hated the idea of installing Python just for this one thing. So here is a Non-Python implementation of the same feature. It uses VB Script. I don’t like VB Script much, but its already installed on an XP Box. So why not!

Dim WSHShell

if (WScript.Arguments.Count>0) Then
	Set WSHShell = WScript.CreateObject("WScript.Shell")
	WSHShell.run "http://en.wikipedia.org/wiki/" + WScript.Arguments.Item(0)
else
	msgBox "Enso Helper Script to launch Wikipedia Page"
end if

Save the above as EnsoWikipedia.vbs and follow the rest of the process outlined in the original solution.


I read the post by Codegnosis and it is a really good idea. So here is the new implementation. This one prompts for a search term if none is specified.

Dim WSHShell

if (WScript.Arguments.Count=0) Then
   searchText = inputBox("Enter search text")
else
   searchText = WScript.Arguments.Item(0)
end if

Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.run "http://en.wikipedia.org/wiki/" + searchText

Thanks.


I added something to Naresh’s code. Now, if you don’t supply an input into the command box (just press enter) it will use the clipboard as the search term:

Dim WSHShell
Dim objHTML

Set objHTML = CreateObject(”htmlfile”)

if (WScript.Arguments.Count=0) Then
searchText = inputBox(”Enter search text”)

else
searchText = WScript.Arguments.Item(0)
end if

if searchText = “” then searchText =

objHTML.ParentWindow.ClipboardData.GetData(”text”)

Set WSHShell = WScript.CreateObject(”WScript.Shell”)
WSHShell.run “http://en.wikipedia.org/wiki/” + searchText

—-
Note to enso developers. While I understand the ideas behind all the natural language stuff with “open” and “open with”, which you can still keep, I will suggest what I think the default behavior to be for a command:
You have a command e.g. wiki
If nothing is selected, and nothing else typed, it goes to the default site e.g. wiki homepage
If nothing is selected, and things are typed after the command, it does a search based on that text e.g. you can pass parameters to wiki by typing afterwards, e.g. like the current google command, as suggested above
If text is selected, it passes a parameter to that website based on the text

So basically, you extend the current google command system to new commands rather than having to do the “open with x” palava, and do something useful instead of saying “no text selected” when nothing is selected.

Alternatively, if you don’t type anything rather than going to the home site it searches using the clipboard (which I would prefer), or you could have a clipboard parameter like “clipboard” e.g. wiki clipboard will search wikipedia with the clipboard as search term.

So rather than select some text, hold caps and then type “open with wiki”, instead, I select text, hit caps and w and bingo(given a good autocomplete system where w=wiki is the default if it is the most frequent command beginning with w).


Thank you Shane for the update. I must say I am honored to find out that some one has found this useful.

I agree with Shanes comments. Also, I would like to see intelligent ordering of choices - automatic or manual. That is if I type open with w and I select wiki 90% of the time, Enso should automatically pop wiki in command completion instead of wallpaper (alphabetical sorted).


I’ve really enjoyed watching this thread grow. It?s amazing how a group of knowledgeable people can quickly come up with fixes and new feature ideas. This process, should the Enso guys artfully guide its course, should result in an amazing product.


Btw, I was able to get enso to do something I thought was difficult but is not. So thought I’d let all you guys know.

Purpose: Open a Command Prompt with the directory already cd’ed to.

1. Create a shortcut in the start menu some where and name it Cmd . The Target should be %SystemRoot%\system32\cmd.exe /k CD
Note: Nothing after CD

2. Set Start in to C:\ (or your default)

3. Now select a folder and try ‘open with cmd’.

A command prompt opens and the directory is CD’d to.

Enjoy!

Btw Atul: I see a lot of good recommendations from lots of people here. Will this info somehow get to the ‘How do I…’ section some day?


This makes it that much more neat.


Regarding Nareshs CMD command: It won’t work with other drives but C:. Add the /d parameter.

So use:
%SystemRoot%\system32\cmd.exe /k CD /d


I downloaded the standard enso software for launching programs etc. But at first it was a bit fiddly, I wanted it to remember my entries rather than telling it to. The awkwardness of the using the caps lock when typing still bothers me, its ok for short entires but to frustrating for longer words and if you make a typo during the entry it becomes real combersome. To me nothing beats the mouse, it really comes down to how users organise themselves on their PC’s and what steps they take to make their experience as easy as possible. Enso’s a great idea but forces a new approach and for the non-keyboard typing pro it would be too hard to grasp.
My absolute most best wish would be some little widget that sits anywhere on the desktop and is dragable also. It would feature all the things that enso does in a 3d rotating object or something that sits off screen and slides accross when hovered. users like to point and click, thats the tradition, its really hard to break away from something that is the easiest way to navigate a computer.


Nascif Abousalh-Neto
October 23rd, 2008 1:31 pm

This thread seemed to have gone cold, which is a shame given how interesting this topic is.

I wonder if using Python’s support for regular expressions and its integration with COM one could do both clipboard fallback (a really useful trick) but also take a page from Emacs thing-at-point - have one command that does different depending on pattern matching.

The less verbs the better, teach the machine to read your mind!


just wondering if it was possible to run something more command prompty from it - to run something with parameters, i guess.

i keep a command prompt open all the time for just the one program, but it’s “atl blah blah blah” each time.

sorry if i haven’t made any sense =/


okay, they’re called command line options, or switches, i think..


did a little playing, and i can get a set switch to work by doing the learn open as thing with the command in a notepad file, but still not sure how to do a different switch each time..


i was wrong, that doesn’t work.. =P


come to think of it, a “run …” commmand in enso would do the same thing..


POST A COMMENT

Please respect this public space


 Required

 Required



 

Live comment preview