Preview Top Hit in Pop-Up Browser Window (without activating browser)

This macro (available here) leverages KM to chain together the functionality of some disparate productivity tools, including Automator, LaunchBar and Brett Terpstra's SearchLink tool, both of which must be installed to make this work. There is probably an elegant coding solution to this but ... I am not a coder! Anyway, it's fun tinkering!

(Side note: I'm not going to go into all of the functionality of SearchLink. I will let you all explore SearchLink on Brett Terpstra's site and follow the instructions for installation and use. Suffice it to say that it is an outstanding tool for exploiting Markdown syntax to its fullest. It was designed to facilitate quick inputting of links into documents prepared for web publishing.)

OK, so we sometimes like to do a quick web search while reading an article, say, in order to get up to speed quickly on an item with which we are unfamiliar appearing in the text of the article. Or maybe we're writing something in a text editor and we want to do a quick search on an item to which we've referred in the text. Instead of activating your browser in order to execute the search and view results, this macro takes a text selection as a search query, runs a search and returns the top hit to a pop-up browser window which does not require Safari activation and can be dismissed immediately with the Escape key.

First, the macro copies the text selection, let's say it's "Ed Gillespie" (cough ... loser ... cough). It then wraps the clipboard text in SearchLink syntax. The syntax will cause SearchLink to run a top hit search and return the top hit URL to the clipboard (as opposed to outputting a Markdown link, for example). The syntax looks like this:

!g Ed Gillespie !!^

The macro then executes a workflow...

... which sends the selected text, now copied and wrapped, to LaunchBar, as a text item.

The macro now hits the TAB key to point the text item in Launchbar to an action. Another workflow is called which points LaunchBar to the SearchLink action (SearchLink must be accessible via Launchbar). (The SearchLink action is a service workflow that SearchLink installs in your Services directory).

LaunchBar will look like this for a second:

The macro then executes the SearchLink action by hitting Return. SearchLink returns a top hit URL to the clipboard.

The macro then activates an Automator application I made called "Preview URL App" (available here and see my KM Macro Library post here). This app pops up a browser window, without opening Safari, using any URL in the clipboard. It will now be used to pop up a browser window for the top hit URL returned by SearchLink.

Finally, the macro clicks the center of the screen, and thus presumably the center of the pop up window, in order to bring it to the front (for some reason, the pop up is never at the front when it materializes). This allows you to dismiss the window with the Escape key or either the Cancel or OK button on the bottom of the window.

Here it is in action:

Of course, if you don't like the top result, you can always go ahead and do an ordinary search using the full powers of your browser. This macro is built for speed (I like to think), not necessarily accuracy!

You can also fool around with it and set it up to accept a search query, which you could then narrow by adding, for example, site:wikipedia.org Ed Gillespie, which SearchLink would turn into
!g site:wikipedia.org Ed Gillespie !!^
and you would almost certainly get the Wikipedia page as the top hit.

Cheers!

1 Like

Thanks for sharing your macro.

Also, thanks for the reference to Brett Terpstra's SearchLink tool. This tool looks very interesting.

My pleasure. KM has become absolutely indispensable. Brett Terpstra’s Markdown tools – SearchLink is but one – are wonderful, and not just for professional bloggers and the like. Enjoy!

What a great idea for a macro. Thanks for sharing this! I hope you don't mind, but I took the liberty of modifying it so that it no longer requires LaunchBar, along with a couple of other changes designed to make it faster and more reliable:

Preview Top Hit (Selected Text) 1.1.zip (163.6 KB)

Automator Workflow

I've included the Automator workflow the macro uses in the zip file, so anyone interested can download that to get both the macro and the Automator workflow it depends on. It combines Brett Terpstra's original SearchLink workflow and @BKammer's Preview URL workflow to get the clipboard's contents, run them through SearchLink, and preview the resulting URL in a popup window. I decided to make this step a workflow rather than an app both to prevent Gatekeeper code-signing issues and to make it not require that an app be launched or running, which also helps with reliability and the resulting popup (in my testing, there was no need to click at the end to bring the popup to the front, so I was able to leave those actions out and improve the macro's simplicity).

Thanks again for the great macro idea, and for introducing us to SearchLink!

Thanks, this is what I was hoping might come from posting the original macro!

Did you alter the Searchlink script so that it only does the top hit “Google” search and return it to the clipboard?

Finally, do you think you’d like to take a whack at similarly modifying the “Top Hit Legal Case” preview macro I posted?

Great! I'm glad I could help then :slightly_smiling_face:

I didn't touch the SearchLink script; returning the top Google search hit is its default behavior. This way it will still search Google by default, but you can also use the other prefacing options SearchLink supports, like !wiki for Wikipedia, !s for software, and so on for more flexible searches. This version also doesn't output anything to the clipboard, since its entire purpose is just to display the resulting URL in the popup.

I did make one error with my modification, though; I was mistaken about the popup, and it does appear in the background by default. In order to have the macro click the center of the screen and bring it to the foreground with this version, I had to create a sub-macro exclusively for that purpose and run it asynchronously. This is because KM is designed to perform the next action once the previous one concludes, but the Automator workflow action doesn't conclude until the popup is closed (because it's constantly running to display the popup, as you can see from the gear icon that appears in the menu bar while it does so) so placing the pause and click action at the end of this version end up not doing anything until the popup is manually dismissed (by which point they're of course not needed). Fortunately, by running the same actions asynchronously, they no longer wait for the other macro actions to complete, and thus are able to perform as expected. Here's a new version of the macro, packaged with the mouse clicking sub-macro:

SEARCHLINK PREVIEW ( ^⇧L ) Macros.kmmacros (45.6 KB)

I can certainly do that if you like, but first, might I suggest you try modifying it yourself? You should be able to remove all of the same actions from that macro that I removed from this one and substitute them with the same Automator workflow I included in my earlier post (though don't forget to include the sub-macro I added with the latest version). Aside from that, all you'd need to do is change the "WRAP CLIPBOARD IN SEARCHLINK SYNTAX" action to !ls %SystemClipboard% (i.e. delete the end space and !!^, since they're no longer necessary with this version) and you should be set. If you can't get it working as you like, then I'll be glad to give it a shot myself.

I’ll take a shot at it… Thanks!

I was able to make it work by simply wrapping the clipboard in the special argument syntax and letting SearchLink take it from there. Worked automagically!

1 Like