Select Text, Open Background Tab, Search Website

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

The advantage this provides is that the action takes the user to the macro even if the Keyboard Maestro app is not open. Even if the app is open, the user doesn't have to manually find the macro.

Yes, I like your idea and thank you for it. I haven't decided if it's the best idea for me yet.

1 Like

One thing to consider: How often will the user need to modify the URL list?

The reason I use a text files in Markdown Link Tool is that I need modify the files frequently. There are three or more text files (three initially) in a folder and the following options are available to modify the contents:

I'm sure this would be overkill for your application, but the Edit option could be used. That's done with the Open a File Folder or Application action which opens the file with the default application (in my case BBEdit).