Note: This post is no longer updated. Please see the version 2.0 post for current details and download links. This post remains because it has a fair bit of information about the macro in general.
Nearly five years ago, I wrote about my simple Keyboard Maestro web search macro that let me search many web sites from anywhere within macOS. While it worked well, it felt a bit cumbersome—I had to scan a pop-up palette of sites, and then press the proper key to select the site I wanted to search (so I had to keep the list relatively short). Then I'd wait for a new prompt to appear, type the search terms, and press Return.
Because of my macro's convoluted interface, I found myself using our own Butler most of the time, as it offers an interface similar to SafariKeywordSearch (which works in Safari as an extension; I was reminded of it due to a recent post by @danmoren on Six Colors). Both it and Butler do keyword searches, so you can type d best home vacuums to search DuckDuckGo for best home vacuums, or gm Tokyo to search Google Maps for Tokyo.
I decided to rewrite my macro to work like those apps do, but with an added feature: I use a number of browsers, and there are times I want my search to run in something other than the default browser. Using my rewritten macro, I can now optionally specify a browser in which to run the search. So when I activate the macro, I can use one of two different formats for the search command:
Both of those searches will search the Apple Movie Store for movies with the keyword disaster; the one on the left will use my default browser (currently Safari); the one on the right will use Edge.
The macro includes a number of predefined searches, inclcuding many for Apple's various stores. The list can be customized with relative ease, and there's an easily-accessible help window that lists all your shortcuts (though you have to manually keep that up to date when you add new ones).
Step one, download the macro and import it to Keyboard Maestro. The macro will be disabled by default; activate it, and you may want to change the shortcut from what I've chosen, which is ⌃⌘W.
If you wish, jump to the release notes section to see what's new in v1.2—it's pretty big news that will make keeping up with future releases much easier!
It's ready to use out of the box; I suggest typing help the first time you use it to see the list of predefined shortcuts. If you'd prefer, click the small image at right to see the current list, as shown in the macro's help.
To use, press the assigned shortcut key, then type the desired site abbreviation, the optional browser, and the search terms, each separated by a space from the others.
Rather than explain here how to modify and update the macro, you'll find that information in the first comment in the macro itself. It will be easier to keep that information current than to try to make changes here any time something changes going forward.
Removing existing entries is trivially easy. Adding new shortcuts and searches is quite simple. Adding or removing browsers is just a bit more complicated, but still pretty easy. All of those things are (hopefully well) explained in the comment within the macro.
I'm not going to go through this step-by-step, but here are the basics on how this macro works.
- A defined set of shortcuts and sites is stored in a variable. Users can add their own shortcuts and sites in a separate step; they will be added to the stored variable. The format uses a bullet (•) as a separator between the shortcut and the search site, as well as to identify the section(s) of the URL that contain the search term (i.e. •SEARCH•). I chose a bullet because that character isn't valid in URLs, so there shouldn't ever be a match in a search string.
- If the user types help, the help text is displayed, along with a dialog explaining how to restart the search process; assuming the user goes forward, the macro is restarted and the currently-running macro is canceled.
- If the user didn't type help, the macro makes sure they typed at least two "words," where the first word is the shortcut, and the second word is at least one word for the search term. This step (and the help check) are in a while loop, so the input dialog will show up again and again, until the user either types help or uses the proper search format.
- A regular expression search splits the user's input into two separate variables, one for the shortcut and one for the search term. The search term is then filtered for use in a URL (i.e. spaces become %20, etc.).
- The stored list of shortcuts is checked for a match; if one is found, the URL is retrieved, the •SEARCH• placeholder is replaced with the URL-safe search terms, and a flag is set to indicate a match was found. (I don't do this when checking to make sure the user has entered two words because it requires parsing the full list of search sites, and that would have to be done each time through the loop.)
- If the "match found" flag is set, the search is sent to the default browser. If the "match found" flag isn't set, the macro displays an error notification and ends.
The only really tricky bit (for me) was figuring out the best way to relaunch the macro after opening the help window. I'm still not thrilled with how it works now—the window will stay open forever unless the user closes it—but it's passable enough for regular use.
If you have any questions, or would like to see a favorite search added to the defaults, please feel free to ask, either here as comments or via email.
Dec 22 2021 — v1.2 Big news in today's update: The macro now includes an update checker and download tool! No longer do you have to return to the blog, or hope you catch an announcement on Twitter to find out I've updated the macro. I've spent my free time over the last couple of days designing a simple, highly-private (no data sent to my server), and easy-to-use update check subroutine that I can use in all of my macros.
I'll be posting about it in detail soon—including a sample project that can be easily modified for use with your own macros—but for now, here's a sneak-peek screenshot of how it looks in action on the sample project (click to see the full image):
It works by checking the version number of the installed macro against the latest version number, as downloaded from my server. If there's a newer version, you can choose to download it, do nothing at the time, or opt out of future checks. (You can also simply disable the subroutine that runs the update check; it won't affect the macro's operation at all.)
It does require one permanent variable in Keyboard Maestro to track when you last checked for an update. The variable is named zzzRGwebsearch_lastchecked to make it easy to find in the list—if you delete it, you can force the macro to check for an update right away. (If you disable update checks entirely, of course, you can just delete it and it should stay gone.)
This has been on my "someday I should..." list for a long time, and I'm quite happy with the way it came out. I'll be interested in hearing how well it works for everyone—though note, you won't be able to test it until version 1.3 comes out, because, well, you don't have an update checker in your version of the macro yet! I have some ideas for 1.3 already, so it'll probably release over the weekend, and you'll be able to test the updater then.
As for other stuff in 1.2, there's a new search shortcut (MIT) for Macintouch, Macworld's search (M) now runs on their site instead of through DuckDuckGo, and I added a "how many shortcuts" counter to the search dialog box, just for fun. Download via the link above, update, and you'll hopefully then be automatically notified about v1.3 when it releases.
Dec 21 2021 — v1.1 Key changes in this version:
- Added sixcolors and youtube to the list of shortcuts.
- Added a new optional subroutine to check for duplicate shortcuts (see below for more)
- Other changes detailed in the release notes within the macro
The biggest change in this version is the new duplicate check subroutine. This routine makes sure you haven't assigned two sites to the same shortcut. Nothing bad happens if you do, other than the macro will only ever use the last of the duplicate shortcuts.
The subroutine will notify you of a duplicate shortcut and end the macro; you can then find and remove the dupe. The subroutine is fast—it takes about a tenth of a second—but that's still notably slower than just putting the search dialog onscreen (about a hundredth of a second), so I've left it off by default. You can enable the subroutine by enabling the second step in the macro (highlighted in green); I'd recommend leaving it off, though, unless you try a shortcut and find it's opening a site you didn't expect.
Because there are now two files in the macro, they're stored together in a new macro group. This doesn't really change the updating instructions, though, other than you'll have to enable the macro group and not a single macro.
Dec 20 2021 — v1.0 Initial release.