Select Text, Open Background Tab, Search Website

I like your idea/request. I'd like to implement an idea like that, too. I have some ideas.

I've never used any extensions on any browser (mostly because of privacy concerns), and I refuse to use Chrome because it's not cleanly uninstallable on macOS, and it damages macOS. So I can't test your extension. But I think I get the general idea.

The best way to implement this might be KM's palettes, which is also something I've never used (yet.) I'm going to contemplate how I would like to see the user interface for this idea. This sort of thing might be a good idea to justify a Stream Deck. But I presume you don't have one (I don't either.)

One thing I like to do is come up with creative new interfaces for ideas like this. You probably want menus and submenus. I'm not sure yet if I want that (and I don't think KM directly supports that without using complex things like Custom HTML prompts.) I'm leaning towards installing these as macOS services, but that's not definite yet.

You didn't say which apps or browsers you want this to work with. Maybe all apps? It might be simpler (because of AppleScript) if you can limit this requirement to certain apps, like maybe Safari.

This isn't what you wanted, but it's a bare-bones solution, which simply is triggered by a hotkey and then opens up each of the websites for the selected text in a separate Safari tab. In this case, it uses wikipedia and youtube as the search engines.

Just FYI, there are two websites I would add to your list of search engines, Wolfram Alpha and Onelook.

This macro isn't meant to be a solution to your request, so I'm not uploading a macro, just the screen capture. It's just meant to allow me to ponder what I want as a UI for this problem. Personally, I like the idea of opening multiple tabs at the same time. That's easier than restarting this macro multiple times for each website I want to look up the term in.

However maybe this pop-up would allow me to choose between the various choices, and then click OK to see all the tabs open.

Here is my next version of this idea. It's a little better. It lets you choose which sites to open from a pop-up window. It uses the ProcessTokens filter a couple of times, so it's not the easiest macro to understand. But it's still just a prototype that helps me decide what I want. It still doesn't match your preferred UI, although I'm not sure your menu-based UI is possible without some Javascript.

Search Engine Prompt Macro (v10.0.2)

Search Engine Prompt.kmmacros (9.2 KB)

I've been toying with creating a pop-up menu with icons for the various websites. It could be made to work, but let me tell you this: HTML is horrible. I just spent an hour reading a dozen web pages trying to explain to me why you can't create a "Cancel" button in HTML. Some of them even resorted to Javascript to implement a cancel button. I refuse to learn a second language just to create a button that does nothing. I'm sorry, I'm just in a bad mood when I come across something (ie, HTML) that was designed decades ago and still unfixed and unclear.

I may come back to this another day. But I do like my solution above, anyway. It's good enough for now.

Sleepy, thanks for the replies and sharing the macro. I can't seem to work it right, though... It keeps copying the text in Keyboard Maestro and searching the browser (switched from Safari to Chrome*) :face_with_diagonal_mouth:

Initially, all I wanted to do is have key commands that are used to select the text, open background tab, search website. No need for a menu...

However, I like your system because it can be more efficient to search multiple sites at the same time rather than keyying each one.

Here are the other websites that I have added to the extension, in case you were interested:

*I also have local libraries and their libby counterpart.

*I don't like the idea of using Chrome myself, but it has the best extension support... Does this not erase all remnants of an app..? It's what I use, when I need to delete an applicaton: AppCleaner

I am not sure if AppCleaner works on Google Chrome. All I know for sure is that Chrome goes out of its way to make it very difficult to uninstall cleanly. And I don't load apps onto my computer unless I am very sure that they are trustworthy, so I won't even install AppCleaner.

Maybe the reason it doesn't work for you is that I didn't explain clearly how it's supposed to work. You're supposed to press the Trigger key only when you are in another app and have some text selected. It cannot copy text from the KM Editor unless your Editor is open, which should normally not be the case when you trigger this macro.

I'm glad you liked my approach. I designed it so that it would be somewhat easy for users to add their own sites to mine. Did you try adding some of your own sites to my macro?

@Dom and @Sleepy. I suggest you check out MACRO: Abbreviation-Based Web Searches by @griffman.

I just spent a minute looking at it. It does much the same thing, albeit in a different way. I think my approach requires fewer keys.

1 Like

It would be relatively trivial to modify my macro to copy the selected text when it's invoked. Then you'd just type "G cmd-V" in the input box to search Google.

This isn't a change I'm going to make, because it's not something I have any need for, and I don't want to add a menu/preference to let it be enabled or not. But in theory, just add a Cmd-C keyboard command somewhere before the input dialog appears, and you're done.

-rob.

Your approach is particularly nice if you want to search multiple sites. I do have a few suggestions:

  • It would be nice if the dialog included the selected text (or at least a substring).

  • You could have two triggers, the second being the first with an added modifier. If the second trigger was used, then the dialog could open with none of the sites checked (or maybe one, your favorite).

  • The technique you use to get the text is simple, but it does write over the current System Clipboard. You can restore the System Clipboard using:

I have a few macros that use the selected text, at least one of which uses the System Clipboard if no text is selected.

Originally I tried using the method you are using, but I found cases where it didn't work as expected. (No, I can't remember the specific circumstances. :thinking:) But I do remember working through this and developing a method that was more robust. If this is of any concern to you, check out the technique I use in:

The second one has the feature to use the System Clipboard if no text is selected.

@Dom and @Sleepy, obviously I'm following this post with interest and will likely steal whatever you end up creating. :grinning:

One thing I've found is that, depending on the website, sometimes it's more effective to use google.com rather than the site-specific search. For example, when searching for information pertaining to the keyword dictionary, here are the two obvious methods that could be used:

Using the KM forum search engine:

https://forum.keyboardmaestro.com/search?q=dictionary

Using google.com searching for keyboard maestro forum dictionary:

https://www.google.com/search?q=keyboard+maestro+forum+dictionary

For the subset of sites where google.com works better, one could easily create a few actions that could be generalized to complete the search.

I agree; I didn't think of that.

That's a good idea. I didn't think of that. Although I tend to prefer approaches that give the user the ability to configure any such features. I'll still ponder your good idea.

I think I probably should do that.

I didn't think of that either... determining if there is selected text. I think I know how to do that. EDIT: I think my macro already does this. If there is no selected text, the macro does nothing.

I didn't know you've done this before. Even if other people have already written macros that do similar things, I still enjoy writing the code myself. Everyone has their own approach and their own ideas, and usually each person comes up with a unique twist. Right now I'm inclined to use dictionaries in all my solutions - they are just so cool.

1 Like

I think there's a slightly better way. In the google search string you can specify that the search must return results from a specific domain. I think the syntax is "site:keyboardmaestro.com" and it's easy enough to see by inspection how to create a URL to include that search string.

I'm aware that each website has its own way of accepting search string data. For now I was just assuming that the data would be at the end of the string. I know that's not the right assumption, but it was a reasonable assumption for purposes of developing a temporary solution.

Now that I have a working macro, I'll probably rewrite it to account for many of these extra little things that people have mentioned today. And it doesn't matter if people use my macro, because I'm really just doing it for myself.

1 Like

The obvious advantage of using two triggers vs a configuration parameter is that the user has both options readily available.

I agree, but my concern was that you said "the dialog could open with...your favorite... site". That's because the term "favorite" probably requires some special configuration that isn't changeable without editing the macro.

I'm still pondering the best solution. I'm trying to write v2 now.

1 Like

Favorite was just an idea, probably a bad one. I think the real options should be: none selected or all selected. I would think that both modes would be used often. If your macro defaulted to all checked, then it would take several clicks to use it in the other mode. And vice versa.

That's very sensible.

1 Like

Yes, I agree. I've started using dictionaries rather than global variables for values that I want to retain between macro invocations. I shared this idea here: Dialog with Dynamic Popup List

Okay I have a new version that is much cleaner in design, making it easier for people to modify for their own favorite websites. Here's how it currently works:

  1. ^X will let you pick a website from a list to search for the currently selected text.
  2. ⌃⌘X will re-use the previously selected website to search for the currently selected text.
  3. ⌃⇧X will open many new tab in Safari using the currently selected text as the search term, one tab for each search engine in your list.
  4. ⇧⌘X will (-when I get it working-) let you edit the list of websites.

It's pretty cool. There are two reasons I don't want to upload it yet. 1) I want to test it for a few hours. 2) I haven't got the fourth item (above) working yet. I can't remember how to write a KM action that lets you edit a multi-line variable. This makes me feel like an idiot. What's the simplest method, can anyone advise me?

1 Like

I'm assuming you have a multi-line text variable near the top of you macro, right?

If so, you can use the following to open your macro in the editor:

Keyboard Maestro Export

As you probably know, you can get your macro UUID by right-clicking, select Copy as... > Copy as UUID.

That's the simplest method I know. If you want to do something fancier, you can put the URL's in a text file. I do this in Markdown Link Tool.

Well, that is certainly "simple". But it's not really any different from letting the user edit the initialization action for the variable.

I might take your advice to have the user edit the value in an external editor.

KM really could use some sort of sophisticated action for editing variables. There's the fancy app from Dan, but that's overkill for what I need. I'm a wiz with shell scripts, maybe I can send my variable to a shell application for editing.

1 Like