AutoHotkey script to search Google for selection, or open URL (from any program)
Thread poster: Michael Beijer
Michael Beijer
Michael Beijer  Identity Verified
United Kingdom
Local time: 09:58
Member (2009)
Dutch to English
+ ...
Jun 13, 2014

Ever wanted to be able to just select some text and click a shortcut to launch a Google search in a browser? If so, you might enjoy this AutoHotkey script. It works anywhere, that is, you can just select a word or phrase in your CAT tool, in Word, ... anywhere.



;-----------------------------------------------------------***
;### Search Google for selection, or open URL ###
browser="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
... See more
Ever wanted to be able to just select some text and click a shortcut to launch a Google search in a browser? If so, you might enjoy this AutoHotkey script. It works anywhere, that is, you can just select a word or phrase in your CAT tool, in Word, ... anywhere.



;-----------------------------------------------------------***
;### Search Google for selection, or open URL ###
browser="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

#g::
;Copy Clipboard to prevClipboard variable, clear Clipboard.
prevClipboard := ClipboardAll
Clipboard =
;Copy current selection, continue if no errors.
SendInput, ^c
ClipWait, 2
if !(ErrorLevel) {
;Convert Clipboard to text, auto-trim leading and trailing spaces and tabs.
Clipboard = %Clipboard%
;Clean Clipboard: change carriage returns to spaces, change >=1 consecutive spaces to +
Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "\s+","+")
;Open URLs, Google non-URLs. URLs contain . but do not contain + or .. or @
if Clipboard contains +,..,@
Run, %browser% www.google.co.uk/search?q=%Clipboard%
else if Clipboard not contains .
Run, %browser% www.google.co.uk/search?q=%Clipboard%
else
Run, %browser% %Clipboard%
}
;Restore Clipboard, clear prevClipboard variable.
Clipboard := prevClipboard
prevClipboard =
return
;-----------------------------------------------------------***



Michael

[Edited at 2014-06-14 08:11 GMT]
Collapse


 
Riccardo Schiaffino
Riccardo Schiaffino  Identity Verified
United States
Local time: 02:58
Member (2003)
English to Italian
+ ...
Very useful script, Michael, thank you! Jun 14, 2014

Very useful script Michael, thank you!

If I may make a suggestion for other users: Michael's script could be personalized to better answer your needs.

For example,


Run, %browser% www.google.co.uk/search?q=%Clipboard%



Could be changed to use your local version of Google, for example:


... See more
Very useful script Michael, thank you!

If I may make a suggestion for other users: Michael's script could be personalized to better answer your needs.

For example,


Run, %browser% www.google.co.uk/search?q=%Clipboard%



Could be changed to use your local version of Google, for example:


Run, %browser% www.google.it/search?q=%Clipboard%


for Italian,

or even more complex searches, like:


Run, %browser% www.google.it/search?as_q=%Clipboard%&lr=lang_it&cr=countryIT


To search the Italian version of Google for pages in Italian coming from Italy.
Collapse


 
Michael Beijer
Michael Beijer  Identity Verified
United Kingdom
Local time: 09:58
Member (2009)
Dutch to English
+ ...
TOPIC STARTER
Thanks for the suggestion Riccardo! Jun 14, 2014

I should also have mentioned that you can of course change the shortcut used to trigger it. I currently have this set to: Windows key + G.

Here are a few useful things to know when writing AHL scripts, and which might help you to read this one:

! = Alt
^ = Control
# = windows
+ = shift
ClipWait = pauses the script for a specified time, to make it work better

I didn't write this myself, I found it on
... See more
I should also have mentioned that you can of course change the shortcut used to trigger it. I currently have this set to: Windows key + G.

Here are a few useful things to know when writing AHL scripts, and which might help you to read this one:

! = Alt
^ = Control
# = windows
+ = shift
ClipWait = pauses the script for a specified time, to make it work better

I didn't write this myself, I found it on the AHK forum, where I find many of the useful scripts I use in my daily work. This particular one is from here: http://www.autohotkey.com/board/topic/13404-google-search-on-highlighted-text/

Incidentally, I am working on a new website where I will collect a bunch of useful AHK scripts that might come in handy to translators. Stay tuned!

Michael
Collapse


 
Emma Goldsmith
Emma Goldsmith  Identity Verified
Spain
Local time: 10:58
Member (2004)
Spanish to English
Reinventing the wheel? Jun 14, 2014

I use IntelliWebSearch to do this. It's based on AutoHotKey scripts too, so you can set up searches in Google itself, directly access other websites, or even local dictionary resources, all by selecting a single word and using a shortcut.

It has a nice interface for those of us who are put off by complex-looking scripts. You obviously don't fall into this category, Michael!

One of the reasons I particularly like IWS is that, as you say, you can launch a search from any
... See more
I use IntelliWebSearch to do this. It's based on AutoHotKey scripts too, so you can set up searches in Google itself, directly access other websites, or even local dictionary resources, all by selecting a single word and using a shortcut.

It has a nice interface for those of us who are put off by complex-looking scripts. You obviously don't fall into this category, Michael!

One of the reasons I particularly like IWS is that, as you say, you can launch a search from any Windows application. Other tools do this - memoQ, XBench and Studio (through plug-ins) - but I prefer to have a method that works across all applications. So IntelliWebSearch or your AutoHotKey script are much more flexible.
Collapse


 
Michael Beijer
Michael Beijer  Identity Verified
United Kingdom
Local time: 09:58
Member (2009)
Dutch to English
+ ...
TOPIC STARTER
a bunch of possibly useful AHK scripts Jun 14, 2014

Emma Goldsmith wrote:

I use IntelliWebSearch to do this. It's based on AutoHotKey scripts too, so you can set up searches in Google itself, directly access other websites, or even local dictionary resources, all by selecting a single word and using a shortcut.

It has a nice interface for those of us who are put off by complex-looking scripts. You obviously don't fall into this category, Michael!

One of the reasons I particularly like IWS is that, as you say, you can launch a search from any Windows application. Other tools do this - memoQ, XBench and Studio (through plug-ins) - but I prefer to have a method that works across all applications. So IntelliWebSearch or your AutoHotKey script are much more flexible.



Hi Emma,

Good point. And, yes, I too use IWS. However, I like to keep as many of my ‘translation workflow solutions’ in one place as possible: I have a single AHK script that auto-starts when I boot up my computer that contains all kinds of useful little scripts.

E.g.:

;--------------------------------------------------*
; surrounds selection with double, straight quotes: "text" ; useful for Google searches

^+2::
Send ^c
Sleep 100
clipboard = "%clipboard%"
Send ^v
return

;-----------------------------------------------------------***
; surrounds selection with single, curly quotes: ‘text’

^+'::
Send ^c
Sleep 100
clipboard = ‘%clipboard%’
Send ^v
return

;-----------------------------------------------------------***
; shortcut to print date in the following format: 2014-06-14

^+F4::
Send %A_YYYY%-%A_MM%-%A_DD%
return

;-----------------------------------------------------------***
; deletes selection and moves cursor one space to the left (useful in CAT tools)

; ^+d::
; Send, {Del}{Space}{Left}
; return

;-----------------------------------------------------------***
; Ctrl+Shift+comma moves cursor one space to the left (useful in CAT tools)

^+,::
Send, {Space}{Left}
return

;-----------------------------------------------------------***
; move your caret to a misspelled word that is underlined and click this to automatically select the correct spelling (it does the following: moves mouse pointer to caret, right-clicks, clicks down, once, and enters) usually, the first option down is the correct one. (this script is volatile; may need some work to get working)

#IfWinActive, CafeTran
#Persistent
CoordMode, Caret, Screen

^+#:: ; Ctrl+Shift #
MovePosX:=A_CaretX +550 ; Adjust 5 pixels to the right of A_CaretX position
MovePosY:=A_CaretY -10 ; Adjust 10 Pixels above A_CaretY position
MouseMove, %MovePosX% , %MovePosY%

SendInput {Click,Right}
Send, {Down}{Enter}

Return

;-----------------------------------------------------------***
; Some special symbols

;^+3::Send, {U+00A3} ; Pound symbol ( £ )
; ^+2::Send, {U+20AC} ; Euro symbol ( € )
^+-::Send, {U+2014} ; Em dash symbol ( — )
^+6::Send, {U+2013} ; En dash symbol ( – )
;^+::Send, {U+2212} ; Minus sign ( − )
^+3::Send, {U+0022}{U+0022} ; QUOTATION MARK ( " " )
^+o::Send, {U+2022} ; Bullet point ( • )
^+9::Send, {U+2018} ; Single, curly, opening quotation mark ( ‘ )
^+0::Send, {U+2019} ; RIGHT SINGLE QUOTATION MARK ( ’ ) ; messes up Hunspell
;^+0::Send, {U+02BC} ; MODIFIER LETTER APOSTROPHE ( ʼ ) ; works with Hunspell
^+[::Send, {U+201C} ; Double, curly, opening quotation mark ( “ )
^+]::Send, {U+201D} ; Double, curly, closing quotation mark ( “ )
; ^+,::Send, {U+275D} ; Double, curly, dingbat opening quotation mark ( ❝ )
; ^+.::Send, {U+275E} ; Double, curly, dingbat closing quotation mark ( ❞ )
^+x::Send, {U+00D7} ; Multiplication symbol ( × )
^+e::Send, {U+2026} ; ellipsis (…)
^+`::Send, {U+00A9}
^Space::Send, {U+200B} ; invisible spaces ('ZERO WIDTH SPACE') (often used to secretly mark text for later identification)

;-----------------------------------------------------------***
; mouselessly insert highlighted term from source window in memoQ

; script to automatically move your mouse pointer to the caret so you can double-click on a highlighted term in the source segment in order to insert it WITHOUT USING YOUR MOUSE. before clicking: hit Tab (to change the focus to the src segment), and then use the arrow keys to place the caret in/on the desired word.

; with a little help from Robert Ilbrink: http://superuser.com/questions/591697/move-mouse-pointer-to-caret-with-shortcut-in-win8

#IfWinActive, memoQ
#Persistent
CoordMode, Caret, Screen

^+i:: ; Ctrl+Shift+/
MovePosX:=A_CaretX +5 ; Adjust 5 pixels to the right of A_CaretX position
MovePosY:=A_CaretY -10 ; Adjust 10 Pixels above A_CaretY position
MouseMove, %MovePosX% , %MovePosY%

Click 2 ;double-click

Return

;-----------------------------------------------------------***
; Map F13 to Print screen

F13::PrintScreen

;-----------------------------------------------------------***

etc.!

Michael


 
2nl (X)
2nl (X)  Identity Verified
Netherlands
Local time: 10:58
Solution for OS X Jun 15, 2014

In native OS X apps you can press the right mouse button and select "Search with Google".



Some tools offer a way to add Google searches (e.g. CafeTran does).

In other apps you can add your own search feature with Keyboard Maestro and other automation tools. If anyone is interested: I can post an example here:

http://www.proz.com/forum/apple_mac_operating_systems-68.html


 
neilmac
neilmac
Spain
Local time: 10:58
Spanish to English
+ ...
Explanation for dummies, please. Jun 17, 2014

Riccardo Schiaffino wrote:

Very useful script Michael, thank you!

If I may make a suggestion for other users: Michael's script could be personalized to better answer your needs.

For example,


Run, %browser% www.google.co.uk/search?q=%Clipboard%



Could be changed to use your local version of Google, for example:


Run, %browser% www.google.it/search?q=%Clipboard%


for Italian,
...


I'm interested in this function (to get Google results in English rather than my local Spanish, which it tends to default to), but have no experience or more than a vague idea of how to set it up. I'd need a detailed explanation of how to do so, step by step. From the start.


 
Riccardo Schiaffino
Riccardo Schiaffino  Identity Verified
United States
Local time: 02:58
Member (2003)
English to Italian
+ ...
Searches in English Jun 17, 2014

neilmac wrote:
I'm interested in this function (to get Google results in English rather than my local Spanish, which it tends to default to), but have no experience or more than a vague idea of how to set it up. I'd need a detailed explanation of how to do so, step by step. From the start.


That means that for you Google defaults to www.google.es.

A few search strings that should work for you (to be inserted in the script in the appropriate place):

Run, %browser% www.google.co.uk/search?q=%Clipboard%

That runs your search using the English version of Google from the UK

Run, %browser% www.google.com/search?q=%Clipboard%

That should run your search using the American English version of Google

Run, %browser% www.google.co.uk/search?as_q=%Clipboard%&lr=lang_en&cr=countryUK

That should run an advanced search using the UK-ENglish version of Google, with language set to English and country the UK.

For a more complete guide to the various Google search operators, you could check

http://moz.com/ugc/the-ultimate-guide-to-the-google-search-parameters


 
Silviu Lungu
Silviu Lungu
Romania
Local time: 11:58
English to Romanian
Hi Michael, Apr 7

Is there a way to use the code with autohotkey version 2?
Thank you!


 
Michael Beijer
Michael Beijer  Identity Verified
United Kingdom
Local time: 09:58
Member (2009)
Dutch to English
+ ...
TOPIC STARTER
here you go! Apr 7

Silviu Lungu wrote:

Is there a way to use the code with autohotkey version 2?
Thank you!



^/::GoogleSearch() ; Ctrl+/ triggers GoogleSearch

UriEncode(Uri) {
Uri := StrReplace(Uri, " ", "%20") ; Replace spaces with '%20'
; Add other characters you want to replace here, if necessary
return Uri
}

GoogleSearch() {
A_Clipboard := "" ; Clear clipboard variable
Send "^c" ; Copy selected text to clipboard
if !ClipWait(2) {
MsgBox "Failed to copy text to clipboard."
return
}
CopiedText := A_Clipboard
SearchURL := 'http://www.google.co.uk/search?hl=en&safe=off&q="' UriEncode(CopiedText) '"'
Run SearchURL ; Open the default web browser with the search URL
}


 
Silviu Lungu
Silviu Lungu
Romania
Local time: 11:58
English to Romanian
Thank you Michael, Apr 9

In tried ahk v2 script converter and got a working variant from your initial code.
It works but I didn't tested very much.


 


To report site rules violations or get help, contact a site moderator:


You can also contact site staff by submitting a support request »

AutoHotkey script to search Google for selection, or open URL (from any program)






Wordfast Pro
Translation Memory Software for Any Platform

Exclusive discount for ProZ.com users! Save over 13% when purchasing Wordfast Pro through ProZ.com. Wordfast is the world's #1 provider of platform-independent Translation Memory software. Consistently ranked the most user-friendly and highest value

Buy now! »
Trados Business Manager Lite
Create customer quotes and invoices from within Trados Studio

Trados Business Manager Lite helps to simplify and speed up some of the daily tasks, such as invoicing and reporting, associated with running your freelance translation business.

More info »