For example, if you highlight this sentence and press Win+G, amazon.com will open in your browser.
If you highlight "these three words" and press Win+G, you'll perform a Google search for "these three words".
The script extracts usable "go-to" targets from the currently selected text. You don't have to select a URL or path precisely as long as it can be found somewhere within the selection. If there's no recognizable address in the selection, a Google search is performed using the entire selected text instead.
The following "go to" operations work:
- browse to URLs, e.g. <!-- m -->http://somewhere.com<!-- m --> (also works for https:, ftp:, mailto:)
- browse to URLs that have no leading http bit, e.g. <!-- m -->http://www.amazon.co...duct/B001K3IXW8<!-- m -->
- browse to bare domain names, e.g. amazon.com
- send mail to email addresses, e.g. <!-- e --><a href="mailto:billg@microsoft.com">billg@microsoft.com</a><!-- e -->
- explore to local Windows paths, e.g. c:\windows\system32
- explore to UNC paths, e.g. \\localhost\c$\windows\system32
- failing any of the above, perform a Google search of the highlighted text
Try it out. Run the script, highlight any of the above lines, and hit Win+G, and the script will magically "go to" as described.
I use this script all the time. Probably its most used function for me is actually the "Google the highlighted text" function, but the others are often useful, e.g. going to unlinked URLs in webpages simply by selecting the paragraph containing the URL and pressing Win+G.
https://gist.github.com/2400685
;; Go to anything that is in the currently selected text: URLs, email addresses, Windows paths, or just "Google it"
$#G::
;Tip("Clipping...") ;; include my mouse-tip library for this https://gist.github.com/2400547
clip := CopyToClipboard()
if (!clip) {
return
}
addr := ExtractAddress(clip)
if (!addr)
{
; Google it
;Tip("Searching for [" SubStr(clip, 1, 50) "] ...")
addr := "http://www.google.com/search?q=" . clip
}
else {
; Go to it using system's default methods for the address
;Tip("Going to " Substr(addr, 1, 25) " ...")
}
Run %addr%
return
;; utility functions
;; Safely copies-to-clipboard, restoring clipboard's original value after
;; Returns the captured clip text, or "" if unsuccessful after 4 seconds
CopyToClipboard()
{
; Wait for modifier keys to be released before we send ^C
KeyWait LWin
KeyWait Alt
KeyWait Shift
KeyWait Ctrl
; Capture to clipboard, then restore clipboard's value from before capture
ExistingClipboard := ClipboardAll
Clipboard =
SendInput ^{Insert}
ClipWait, 4
NewClipboard := Clipboard
Clipboard := ExistingClipboard
if (ErrorLevel)
{
MsgBox, The attempt to copy text onto the clipboard failed.
;Tip("The attempt to copy text onto the clipboard failed.")
return ""
}
return NewClipboard
}
;; Extracts an address from anywhere in str.
;; Recognized addresses include URLs, email addresses, domain names, Windows local paths, and Windows UNC paths.
ExtractAddress(str)
{
if (RegExMatch(str, "S)((http|https|ftp|mailto:)://[\S]+)", match))
return match1
if (RegExMatch(str, "S)(\w+@[\w.]+\.(com|net|org|gov|cc|edu|info))", match))
return "mailto:" . match1
if (RegExMatch(str, "S)(www\.\S+)", match))
return "http://" . match1
if (RegExMatch(str, "S)(\w+\.(com|net|org|gov|cc|edu|info))", match))
return "http://" . match1
if (RegExMatch(str, "S)([a-zA-Z]:[\\/][\\/\-_.,\d\w\s]+)", match))
return match1
if (RegExMatch(str, "S)(\\\\[\w\-]+\\.+)", match))
return match1
return ""
}
Updated 2012 05 17: Fixed CopyToClipboard() so it waits up to 4 seconds for copying to take place, and notifies the user if it didn't work. Modified CopyToClipboard() so it restores the original clipboard's content after performing the copy, and uses ^{Insert} instead of ^C for broader app copying support (thanks JohnGH!). Minor refactoring of the #G code. Fixed indentation in ExtractAddress() to match rest of script.




