MACRO: Abbreviation-Based Web Searches

Our company, Many Tricks, makes an app called Butler that lets you do abbreviation-based web searches. Type "gm Tokyo" into Butler's input box, and it runs a search for Tokyo on Google Maps. There's also a Safari extension, SafariKeywordSearch, that does the same thing, though only from within Safari.

I decided to recreate this functionality in Keyboard Maestro, but with the added ability to optionally specify a browser to use for the search—if left unspecified, it uses the default browser:

The version on the left would use your default browser to search Apple's movies for those using the keyword disaster; the version on the right would do the same, but using the Edge browser.

I included a number of shortcuts, as seen in this image, but it's very easy to remove any of those, and relatively easy to add your own. There's also an in-app help screen you can display to help remember the shortcuts (and browsers) it supports.

There's more detail in this blog post: https://robservatory.com/search-sites-from-anywhere-via-abbreviations/

The macro is attached here—as usual, it's disabled, so enable then try it. It's working well for me; if others do try, please let me know if you have any issues with it.

Feb 10 2022: 3.3 is out (see post below - important update!)

Prior versions

Feb 8 2022: 3.2 is out
Feb 6 2022: 3.1 is out
Feb 2 2022: 3.0 is out
Jan 25 2022: 2.7 is out
Jan 25 2022: 2.6 is out
Jan 15 2022: 2.5 is out
Jan 14 2022: 2.4 is out
Jan 9 2022: 2.3 is out
Jan 4 2022: 2.2 is out
Jan 2 2022: 2.1 is out
Dec 29 2021: 2.0 is out

websearch_33.zip (170.2 KB)

-rob.

7 Likes

Version 1.1 is now available. there are in-macro release notes, but the big change is an optional dupe checking subroutine to find any duplicate shortcuts. It's a fast subroutine (about a tenth of a second), but it's still much slower than just showing the search input box (about a hundredth of a second), so it's off by default.

I'd suggest leaving it off unless you find that a shortcut is taking you to a site you don't expect—that's a hint that you've assigned two destinations to one shortcut.

Version 1.2 is now available, and the big news is an in-macro update checker and downloader. I'll be posting this separately, as I have a sample project you can download and use to add update checks to your own macros—it seems to work quite well, and requires minimal edits for each update.

There's a new search engine or two, and some other changes, but the version checker is the big thing—you should see the update to v1.3 automatically when I release it.

Check the above-linked blog post for more info on the updater (in the Release Notes section).

-rob.

2 Likes

@griffman, this is really cool. Launchbar includes Search Templates that can be used to do similar searches minus your option to specify the browser.

Thanks for sharing.

Version 2 is out now, and it's a total rewrite, with many thanks to @ccstone and others here. It's now fully customizable (add, remove, or change any shortcut, even those in the core macro), help is greatly improved, and the structure is set up to keep that customization across future updates.

2.0 is linked in the top post here, so I won't link to it again, but here's a shot of the search window (showing some shortcut stats) and improved help:

If you're using 1.2, you should see a message in the macro about the new version, and be able to download it from there.

-rob.

3 Likes

Terrific! Congratulations! Here are some tested shortcuts for the User-shortcuts macro. Don't forget to customize the shortcut at the start of each line.

GH•Github•https://github.com/search?q={SEARCH}
C•Cheetah 3D Forum•https://www.cheetah3d.com/forum/index.php?search&q={SEARCH}
T•Thingiverse•https://www.thingiverse.com/search?q={SEARCH}
P•Prehensileblog•https://prehensileblog.wordpress.com/?s={SEARCH}
MU•MacUpdate•https://www.macupdate.com/find/mac/context%3D{SEARCH}
1 Like

Version 2.1 is out now, and features live search of the shortcut list in help—this can help if, like me, you have a pretty extensive shortcut list and need a reminder sometimes!

Also some new shortcuts, and Brave Browser can be specified as a search choice. The in-app updater in the macro should show the new version, though the release notes may be wrong, due to aggressive caching (that I may have solved in v2.2).

2.1 is linked in the top post if you'd like to download it.

-rob.

3 Likes

Bravo! Everything is working!

I have an update request. For a future version, I'd like to be able to include sample search criteria which will be used by default if unchanged. For example, I like to routinely visit comments I've made at GitHub, but there are other users' comments that I sometimes want to find. The current URL to find all issues for which I've added comments is:

https://github.com/search?q=is%3Aissue%20commenter%3Aloshlee

So my custom shortcut could be (shortcut GIC for GitHub Issue Comments)

GIC•Github Comments by Author•https://github.com/search?q=is%3Aissue%20commenter%3A{SEARCH}

which would then require that I type in the GitHub user name. I'd like to be able to customize the shortcut so that if I used something like "••loshlee**" in the shortcut "definition", the search would be completed using default criteria if I enter the shortcut only, and would become customized If I enter something after the shortcut (as required now, such as any GitHub user name). The default criteria would appear after the name when searching user shortcuts of the web search macro.

Thanks again and again, congratulations!

I guess I'm just asking for default criteria, or search bookmarks (with finesse).

I'm not 100% clear on the request. Are you wanting to create a shortcut that can optionally take a username? For example, if you had these URLs...

https://some.url?search=tomorrow
https://some.url?search=tomorrow&username=fred

You want one shortcut that searches some.url, say it's S. If you type "S tomorrow" then it loads the first URL, but if you type "S tomorrow fred," it loads the second URL?

EDIT: Wait, I think I get it. Your default search is for fred, but you sometimes want to search for jane or susan or whatever, and specify that as part of what you type?

-rob.

Right, sort of. By default it's a saved search, but if I enter criteria the default is replaced by whatever I enter.

I should probably have said, "but if I enter criteria when performing the search, the default is replaced by whatever I enter."

I've thought about criteria, but it's pretty tricky to do well and make it work easily. One challenge lies in parsing the input (where is the data that's going to be replaced?) without making you type some sort of special characters. Then there's another challenge in using one shortcut to handle both parameter and non-parameter searches.

It would probably have to work something like this:

S•my shortcut to some site•https://some.site/?{SEARCH}###&name={PARAM1^^your-default-value}&job={PARAM2^^your-default-value}&title={PARAM3^^your-default-value}###

So you would set up:

GIC•Github Comments by Author•https://github.com/search?q=is%3Aissue%20commenter%3A{SEARCH}###%3A{PARAM1^^loshlee}###

If you then typed 'GIC' with nothing else, it'd search on loshlee; if you typed something else, it'd become PARAM1. And that's all great in theory, and works OK for Github. But many times, you want to search for something and a parameter, and that's where things get difficult. Now you have to be able to type the shortcut, the search term, and the parameters, and I have to be able to parse what's what.

Even worse, the parameter sections might not come all neatly at the end, so they'd be interspersed with the URL, making setup trickier and the regex to extract all the right bits quite a bit harder.

Basically, it wouldn't be overly hard to do for one very specific example where you basically want to replace the search term with some other field. I'll put that on my list of stuff to look at, but no promises :).

-rob.

I just pushed version 2.2, with a fix for a mistake I made in the update code—I accidentally quit the entire macro in some situations, instead of just quitting the update subroutine.

-rob.

1 Like

Hi Rob. Search the Web v2.1 is outstanding (and very polished)! It has immediately become one of my favorite macros. Thanks so much for sharing!

I have one minor issue and one suggestion:

  • When specifying the Brave Browser (e.g., by entering a Shortcut and Search: y b keyboard maestro) Safari is opened, not the Brave Browser.

  • If one starts typing in the Shortcut and Search field and then decides to press ⌘H to display the Help, the main dialog reappears with the Shortcut and Search field is empty. It would be nice if the previously typed text was automatically re-entered. (I haven't dug into the macro, but you could save this text in a global variable and selectively clear it when needed (e.g., when the Ok button is selected).

Thanks again, Rob!

Whoops, my bad! There's the first 2.3 fix—I forgot to edit the regex that checks the browsers when I added Brave Browser. To fix:

• Open zs-Process Input, then open the step named Split site, (browser), and search terms into separate variables. There you'll see this bit of regex:

(.+?)\ (?:([cefosv]) )?(.*)

Change that to this:

(.+?)\ (?:([bcefosv]) )?(.*)

And it should work now—thanks for finding this!

As for the lost text while typing, I didn't think KM saved the input box contents unless you clicked OK, but that's not true at all—it appears it's saved as soon as it's typed. So yes, that'll be fixed in the next update too (via a temp perm variable, as you suggested). I just tested, and it works quite nicely.

Thanks for the feedback!
-rob.

1 Like

@griffman, you're welcome. Thanks for the quick response.

I made a slight adjustment to this macro. I often copy an address and throw it into a Google Maps search, but if I paste into the search box an address that's split over more than one line, this macro captures only its first line as a search term. If I want to search for, say,

100 Main St.
Boston MA

A search on a generic "100 Main St." isn't likely to turn up what I'm looking for, and I can imagine other situations in which I'd be pasting something similarly split.

So I added an action to "zs-Process Input", immediately after "Get user input," to search the "instanceShortcut and Search" variable for the regex \s+ (that is, any repeated white space) and replace it with a single space.

That conveniently also changes newlines to spaces (I guess linebreak + carriage return = repeated whitespace, or something?), and my search term is correctly handed off to Google Maps as:

100 Main St. Boston MA

2 Likes

That sounds like a good change—I'll add it to the next update (which will be coming soon, because I updated the updater to work in a much better manner). Can you screenshot what you added; then I can make it identical (and skip redoing the work myself :)).

-rob.

Doh, should have thought to do that in the first place! Note that the apparently empty Replace box actually contains a single space, otherwise boy howdy would that be a mess.