Google Search Action for GNOME Do

Google search action in GNOME Do
As more people are using GNOME Do, I keep getting nagged about how GNOME Do doesn’t do this or that. This is exactly why GNOME Do has a plugin system for adding to the items and actions GNOME Do provides. For example, many people have said “Launchy/Quicksilver/Katapult/my mom has a Google search action, why doesn’t GNOME Do have one? This is a bug!” Well, even though documentation is sparse at the moment, here’s an example of how easy it is to create a Google action for GNOME Do.

Below, I create a simple Google search action by implementing the IAction interface from the Do.Addins library. I give the action a name, a description, and I use the stock “search” icon. I specify that this action only supports the ITextItem type, which is simply a type representing the text the user entered. In the Perform method, I assemble the URL for performing a Google search using the text from the first item (I know it’s an ITextItem because that’s all this action supports). Finally, I open that URL with a method provided by the Do.Addins.Util.Environment class.*


using System;
using Do.Addins;
using Do.Universe;

namespace GoogleAction
{
  class GoogleAction : IAction
  {
    public string Name { get { return "Google"; } }
    public string Description { get { return "Search Google."; } }
    public string Icon { get { return "search"; } }

    public Type[] SupportedItemTypes {
      get { return new Type[] { typeof (ITextItem) }; }
    }

    public Type[] SupportedModifierItemTypes {
      get { return null; }
    }

    public bool ModifierItemsOptional {
      get { return false; }
    }

    public bool SupportsItem (IItem item) {
      return true;
    }

    public bool SupportsModifierItemForItems (IItem[] items, IItem modItem) {
      return false;
    }

    public IItem[] DynamicModifierItemsForItem (IItem item) {
      return null;
    }

    public IItem[] Perform (IItem[] items, IItem[] modItems) {
      string query;

      query = "http://www.google.com/search?q=" + (items[0] as ITextItem).Text;
      Util.Environment.Open (query);

      return null;
    }
  }
}

Then, compile the plugin, install it, and restart GNOME Do:


  $ gmcs -target:library -r:System -r:/usr/lib/do/Do.Addins GoogleAction.cs
  $ mv GoogleAction.dll ~/.do/plugins
  $ killall gnome-do
  $ gnome-do

Get it? Got it? Good.

*Note: Real plugins should be more robust, do some error checking, have comments, etc. This is only a sample.

11 comments

  1. nate wrote,

    I like how this works a lot. It seems more logical to type “Google blah”. Let’s say I want to send an IM to someone whose screen name is Gimp or something. Rather than having to type Gimp (in which case I would get either the GIMP or their name, I find it more logical to do “im gimp” for the IM and “gimp” for the program. Just my opinion obviously.

  2. Rick wrote,

    This is very cool to know. I hadn’t realized that commands actually came up in the first pane. I thought only IItems could. This opens up some possibilities.

    Thanks for taking a sec for this quick intro.

  3. immensewok wrote,

    Awesome. Thanks for spelling this out. I was able to make some simple changes to the template and search Wikipedia and my del.icio.us bookmarks. I tried to set this up for ubuntuforums.org but I’m not sure how to convert my text into their searchid.
    I did have a little trouble getting this to work. I had installed Gnome-Do through Synaptic and I got a permission error when I ran gmcs. I removed it and then built from the trunk and it worked fine. Is this normal or is this a lack of understanding on my part?

  4. Dave wrote,

    “I got a permission error when I ran gmcs” – weird. I have no idea what happened!

    I suggest you make an abstract “WebSearchCommand” class, and then you could easily create new web search commands by subclassing it, and implementing a SearchURLWithQueryString method. This way you could create your Wikipedia, del.icio.us, etc commands in 5-10 lines each.

  5. Bo wrote,

    Huh, interesting, how come my gnome-do is rectangular boxed? instead of round boxed :(

  6. Bo wrote,

    Apologies, didn’t know I can’t put HTML in comments

    Here’s a snapshot of my gnome-do, weird behavior indeed
    http://img337.imageshack.us/my.php?image=gnomedoiu9.png

  7. Dave wrote,

    You need compositing turned on for the pretty window.

  8. Peter wrote,

    The command to download and install at http://do.davebsd.com/?q=content/download has a typo. Either the && or the ; should be there, but not both. The line’s right on the launchpad page, wrong on http://do.davebsd.com/?q=content/download.

    Nice little bit of software!

  9. Guillermo wrote,

    Using Gnome Do in Ubuntu Gutsy, that path for Do.Addins library is wrong. I have tha library just in “/usr/lib/do/”.
    And works fine! :)

  10. ygallardo wrote,

    Wow, it’s very easy make a addins for some web search with the GoogleCommand, making some little changes on the code I make a Youtube search videos with Gnome Do.
    Very nice and easy.

    Bye

  11. sathyz wrote,

    Hi, I’m using DO 0.8.1. I have modified the code.
    following the above steps, I got

    ERROR: Could not read add-in file: /home/skumar/.local/share/gnome-do/plugins-0.8.1/GoogleSearch.dll

    On googling, found that we need resource addin.xml to be included so I did,

    $ gmcs -target:library -r:/usr/local/lib/gnome-do/Do.Universe.dll GoogleSearch.cs “/res:GoogleSearch.addin.xml”

    and started gnome-do

    it showed,

    Creating package Do.GoogleSearch_0.1.mpack
    Installing GoogleSearch v0.1

    But the plug-in is not found in the GNOME Do preferences.

Trackbacks/Pingbacks (2)

  1. tecosystems » Do or Do Not Do, There is No OS X on Tuesday, January 15, 2008 at 11:51 pm

    [...] a simple command on the browser’s location bar. Do would be faster. And rather than build a Google Search plugin, a Yubnub extension would allow you to query Google, MSN, Wikipedia, or Yahoo. Or Amazon. Or eBay. [...]

  2. gnome-do plugin [part 1]: create an action « l-Satz on Monday, May 11, 2009 at 7:48 am

    [...] lets start writing the plug-in, the simplest plug-in that I found on net is Google-Search. There are few changes required (we need to include .addin.xml for the plug-in and we should [...]