Thursday 4 October 2012

Google search from keyboard shortcut

Plan

I want to be able to run a google search based on text contained on the clipboard, this allows me to quickly search for text snippits I come across

Windows 

From this site: 
http://blogs.technet.com/b/heyscriptingguy/archive/2004/12/15/how-can-i-grab-a-url-from-the-clipboard-and-then-open-that-web-site-in-a-browser.aspx

I found a snippet of VBS script got me most of the way, the only thing to add is to adapt it so that it appends the string to the end of a google seach URL. 

The final script looks like this 

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
strURL = objIE.document.parentwindow.clipboardData.GetData("text")
objIE.Quit

Set objShell = CreateObject("Wscript.Shell")
strSearchURL = "http://www.google.co.uk/search?q=" & strURL
objShell.Run(strSearchURL)
You can then save this script with a vbs file extension and have it called when you press a certain keyboard shortcut or macro key.

By default this will prompt to allow the script to access the clipboard. You need to add "about:blank" to the trusted internet zone and set the "Programmatic clipboard access" to enabled not prompt. 

Ubuntu


After looking into google searching from gnome-terminal the only solution looked a bit risky http://blog.ubuntu-tweak.com/2010/11/16/gnome-terminal-with-google-search-support.html

As a result I decided to look into a safer and more general option, that was using xsel and ubuntu keyboard shortcuts to open a browser tab searching for whatever is selected.

#!/bin/bash    # Steve Fry http://cdtfry.blogger.com
 # 04/Oct/2012
 
 # Search based on selected text
 search_string="http://www.google.com/search?q=$(xsel)"
 
 # Search based on clipboard contents
 # search_string="http://www.google.com/search?q=$(xsel --clipboard )"  
 
 # Open a chromium tab based on the search string.
 chromium-browser "$search_string"  

This can then be saved to a text file such as /home/steve/bin/google_from_clipboard.sh and then allocated to a keyboard shortcut using the ubuntu keyboard menu.


Steve