A Mind-Reading Script

What if you could highlight any text, press a hotkey, and have Windows do what you want almost every time? You'd have my top productivity hack.

A Mind-Reading Script

I love AutoHotKey.  In this series, I'm posting some of my favorite script snippets. If you've never used AutoHotKey before, here's the quick start guide.


This is probably the hotkey I use more than any other.  I use this one little script at least a dozen times every day.

Here's how the script works:

  1. Highlight some text
  2. Press the hotkey
  3. Magic happens
    - URLs open in the default browser
    - folder paths are opened in File Explorer
    - full file paths are opened in their default application
    - everything else gets searched with Google

This is my number one time-saving productivity hack.  Let's break down the three steps above a bit further.

Highlight some text

Any text.  In any program.  If pressing [Ctl] + [C] copies that text to the clipboard, then it will work in this script.

Press the hotkey

I've assigned the following hotkeys to this script:

  • [Menu] + [G]
  • [Ctl] + [Win] + [G]

I prefer to use the [Menu] key on keyboards that have it, but not very many do anymore.  I chose [G] for Google, but feel free to choose whatever hotkey will be most meaningful to you.  

The hotkeys are defined in the first two lines of the script snippet.  If you want to use a different hotkey, you just have two change one or both of those lines.

Magic happens

The string parsing is pretty basic.  The URL check in particular could be improved upon greatly with a well-built regular expression.  I'll leave that as an exercise for the reader.  The script as is has served me well for years, so there's no need to change it if you don't want to.

Feel free to use a different search engine, if you wish.  I use DuckDuckGo as my default search engine in my browser.  It should be pretty obvious how to swap in your preferred search engine.

Sample code

In addition to the script snippet itself, I've got a handy function (GetHighlightedText()) that takes care of retrieving highlighted text into the script without wiping out the existing clipboard contents.  

AppsKey & g::  ;Universal Google search on selected text
    text := GetHighlightedText()
    if (InStr(text, ".com")
     Or InStr(text, ".org")
     Or InStr(text, ".edu")
     Or InStr(text, ".net")
     Or InStr(text, ".us")
     Or InStr(text, "http://")
     Or InStr(text, "https://"))
        Run %text%  ; open what appears to be a url in default browser
    } Else If (SubStr(text, 1, 2) = "\\"
            Or SubStr(text, 2, 2) = ":\")
        Run %text%   ; open what appears to be a file path with default program
    } Else {
        text := uriEncode(text) ;urlEncode special characters and search in google
        Run http://www.google.com/search?q=%text%
GetHighlightedText() {
    ClipSaved := ClipboardAll   ; Save the entire clipboard to a variable of your choice.
    clipboard =  ; Start off empty to allow ClipWait to detect when the text has arrived.
    Send ^c
    ClipWait 1  ; Wait for the clipboard to contain text.
    ClipText := Clipboard
    Clipboard := ClipSaved   ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
    ClipSaved =   ; Free the memory in case the clipboard was very large.
    Return %Cliptext% 

Image by nvodicka from Pixabay