Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Go to anything: browse/explore/Google the selected text


  • Please log in to reply
9 replies to this topic
joelpt
  • Members
  • 23 posts
  • Last active: Dec 09 2011 10:05 PM
  • Joined: 24 Jun 2009
This handy little script will "go to" anything when you select some text that can be copied to clipboard. URLs, email addresses, Windows paths, or just Googling the selected text are supported.

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.



;; 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.

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
Nice script! I have implemented similar functionality in 7plus:
[*:2sa8qql7]A WIN+G hotkey for googling selected text.
[*:2sa8qql7]In next version: Huge update to the "Accessor" program launcher that also recognizes the type of selection. It will insert the text in the query of the launcher, and supports things like URLs, file paths, registry paths. In addition, web search engines can be defined with shortcuts, i.e. you can select some text, open Accessor and type "a" for Amazon and press enter to search the selected text there.

Guesting
  • Guests
  • Last active:
  • Joined: --
Simple, and awesome, thanks!

JohnGH
  • Members
  • 4 posts
  • Last active: May 16 2012 11:01 AM
  • Joined: 16 May 2012
I like this, and I used it to write my own function for launching PuTTY (thanks!)

I did note though that it clobbers whatever you have in your clipboard with the text that you select.

We're only using the clipboard because it's the means we have of using the selected text, so throwing away what you had been using in the clipboard is a bit of an unwanted side-effect.

I found a way to use it without doing this (and I also changed the ^C to ^{Insert} which works in a terminal window too.)

Have a look here:

Also, could you explain the purpose of the:

    KeyWait LWin
    KeyWait Alt
    KeyWait Shift
    KeyWait Ctrl
    ...
    ClipWait, 1000
please?

I found when I used that on text selected in a chat window in pidgin, it didn't work unless I then copied the selected text.

Once I stripped all that out it worked fine.

joelpt
  • Members
  • 23 posts
  • Last active: Dec 09 2011 10:05 PM
  • Joined: 24 Jun 2009
The idea behind the KeyWait statements is to wait until all of those keys are released by the user before sending ^C. If one of them happens to be held down when ^C is sent, AHK may end up invoking some other keybind, e.g. Ctrl-Win-C. If you hold Win, press G, wait a couple seconds, then release Win, you can see the effect. Not sure why that's interfering with the Pidgin copying. "Keywait LWin" is probably the only one that is really needed though if you use the Win-G keybind. I just included the others in case a user uses a different bind.

ClipWait just waits for %Clipboard% to contain some text before continuing. This makes things more reliable in cases where the system is running slowly and it takes a little while for the system to react to the copy request. Though I just looked at the docs for ClipWait and the first argument is seconds to wait rather than milliseconds as I obviously assumed here ;)

joelpt
  • Members
  • 23 posts
  • Last active: Dec 09 2011 10:05 PM
  • Joined: 24 Jun 2009
Script 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.

JohnGH, I decided to leave the KeyWait events in since I haven't run into an issue myself with these. I would be interested to know what happens if you remove the KeyWait events one at a time, starting with the last one, and testing that. I'm wondering if just one of these happens to be hosing things up in Pidgin. Also, thanks for your improvements to CopyToClipboard() which are now integrated.

EDIT: also just changed ^C to ^{Insert} for broader app copying support. If I find some apps that support ^C but not ^{Insert}, I may change this code to 'SendInput ^C^{Insert}', which will probably cause a copy in all apps, but could conceivably upset some apps, and also will copy twice in most apps which is redundant work. Since that approach is more aggressive, I'm just sticking with ^{Insert} for now unless I do find an app that doesn't recognize ^{Insert}.

Scoox
  • Members
  • 194 posts
  • Last active: Jun 09 2017 03:34 AM
  • Joined: 28 Nov 2010
I just stumbled upon this thread and couldn't help commenting, since I implemented something very similar last month. My version is functionally very similar to yours except that it can additionally go to registry paths. To achieve this, I wrote a simple function called RegJump().

Another tip is to trigger the go-to code by means of a hotkey such as
LButton & g::
. This way you select a path with the mouse, and while holding LButton down hit 'g' to go to the selected path.

RegJump() - Jump to registry path in Regedit

HoverScroll() - HoverScroll() - Scroll controls without focus


james062
  • Members
  • 75 posts
  • Last active: Jan 08 2016 02:11 PM
  • Joined: 05 Oct 2009

Works great , Thanks , Very handy :-)


All`s well that ends well :-)
I will not appreciate sarcastic or personal insults in reply s to my posts . Those are the acts of cowards being brave from afar .If you cant be productive don't be destructive .

Kangaroo
  • Members
  • 8 posts
  • Last active: Nov 04 2014 12:39 PM
  • Joined: 05 Dec 2012

Hi Joelpt,

 

Thanks for your script. As I'm a newby to AHK and programming, would you be so kind to elaborate a bit on your ExtractAddress function?

 

for example:    if (RegExMatch(str, "S)((http|https|ftp|mailto:)://[\S]+)", match))
 

can you please explain step by step how this works?

 

thanks very much, Kangaroo



zhotkey
  • Members
  • 34 posts
  • Last active: Feb 04 2016 02:06 PM
  • Joined: 02 Dec 2010

Your script doesn't work when you select "", it removes the spaces between "".