Folks,
Thank you for your posts. I am relatively new to AutoHotkey but wanted to contribute the following code that deals with non-alphanumeric characters, e.g. bill & mary.
Code:
;
; Searches for the highlighted text with Google in Firefox.
; If the text is a URL, it goes directly there.
;
#g::
ClipSaved := ClipboardAll ; Save the entire clipboard
Clipboard = ; Clear the clipboard (useful for when there is no highlighted text)
Send ^c ; Copy the highlighted text to the clipboard
ClipWait 1 ; Wait up to 1 second for the clipboard to contain data
if ErrorLevel ; If there is no data in the clipboard after 1 second, exit
{
MsgBox, The attempt to copy text onto the clipboard failed.
return
}
SearchString := Clipboard ; Assign the clipboard content to the variable
Clipboard := ClipSaved ; Restore the original clipboard
ClipSaved = ; Free the memory in case the clipboard was very large
;
; Trim whitespace at either end
;
SearchString := RegExReplace(SearchString, "^\s+|\s+$")
;
; Prepare string for Google
;
if RegExMatch(SearchString, "\w\.[a-zA-Z]+(/|$)") ;contains .com etc
{
if SubStr(SearchString, 1, 4) != "http"
SearchString := "http://" . SearchString
}
else
{
;
; Google search does not deal directly with certain characters in the search string
; so they need to be replaced with their equivalent ASCII codes
;
StringReplace, searchString, searchString, `%, `%25, All ; This needs to be at the start
StringReplace, SearchString, SearchString, %A_Tab%, `%09, All
StringReplace, SearchString, SearchString, ", `%22, All
StringReplace, SearchString, SearchString, `#, `%23, All
StringReplace, SearchString, SearchString, $, `%24, All
StringReplace, SearchString, SearchString, &, `%26, All
StringReplace, SearchString, SearchString, +, `%2B, All
StringReplace, SearchString, SearchString, `,, `%2C, All
StringReplace, SearchString, SearchString, /, `%2F, All
StringReplace, SearchString, SearchString, :, `%3A, All
StringReplace, SearchString, SearchString, `;, `%3B, All
StringReplace, SearchString, SearchString, =, `%3D, All
StringReplace, SearchString, SearchString, ?, `%3F, All
StringReplace, SearchString, SearchString, @, `%40, All
StringReplace, searchString, searchString, %A_Space%, +, All
;
; Setup the Google search with an unquoted string
;
SearchString := "http://www.google.com/search?&q=" . SearchString
;
; Uncomment the line below to setup the search with a quoted string
;
; SearchString := "http://www.google.com/search?&q=" . "%22" . SearchString . "%22"
}
;
; Open up new tab in Firefox with the Google search results
;
Run firefox.exe -new-tab %SearchString%
return
I hope someone finds this helpful.
Cheers,
Philip