"Search the Web" Action With the Last Typed Search Text

Is it possible to use the "Search the Web" action with the last typed search text? I tried the following but it doesn't work. It seems like I need to have two dialogs pop up one to easily get the search string to then put into the search string.

image

I know I could do something like I do with the YouTube search I have but this is overly complicated.

Youtube Search Macro

QK - Youtube Search.kmmacros (41 KB)

I don't really understand what the token %Search% is doing behind the scenes.

If you read this page:

https://wiki.keyboardmaestro.com/Tokens

You will see that there is no token named Search.

In that case, your macro is referring to a variable called "Search".

There is no token for "last typed search text" but I'm sure that some people will have ideas how to do that.

Thank you, I actually did check there first and posted a question about that elsewhere to not muddy the waters here.

What is odd is that "Search the Web" seems to reference %Search% but yet it doesn't actually use what is in that token. The full script I was working on actually was trying to reference %Variable%Search%

Search Keyboard Maestro Forum and Google

Keyboard Maestro - Search Forum.kmmacros (29 KB)

What do you mean "Search the Web". Are you talking about a web page, someone else's macro, or the macro you wrote?

Anything that references "%Search%" is referencing a variable, not a token.

It's not a token. It would be a variable.

I will add that I think it may be possible to solve your needs using a combination of methods. I'm trying to solve it right now. If I succeed, I will post my solution in this thread. Part of the problem here is that people don't always type the URL string in sequential order. E.g., sometimes they use the mouse or the arrow keys to edit the URL. My method may not catch some of those cases, so it may miss some URLs.

Search the Web action.

I don't really understand how it works completely. I have read everything explaining on the wiki multiple times.

When I do something like the following it will work when I have text selected first.

However if I have that same text in the clipboard and run the macro with nothing selected it doesn't pop up empty or with what was the last text in the clipboar and just doesn't work.

Thank you

Wow, I never noticed that before. (To be fair, I don't use that action.) That action seems to have been written sloppily. I.e., it neglected to use the characters "%Variable%" when it references a variable. Unless I'm missing something, that would seem to be a typo. (It's legal, but it's not using the best practice.)

I'm still working on a possible solution for you. I like tricky problems like this, and if my solution isn't very good, you can ignore it.

Doesn't look like it -- it seems to be a token or variable internal to that Action.

But you can roll your own version of the Action with a global variable, a Prompt Action that both uses and sets that global, a URL encode Filter, and an "Open a URL" Action -- plus some token concatenation, of course!

I have a different way to "roll my own." I think I just got it working, and I will upload it shortly. It doesn't require "Prompt with List" or any user interaction. My macro intercepts what the user types. It's not easy to explain, so you can read it for yourself, shortly...

Ok... You may not like my solution, but here it is. It was tricky for me to write, (it took hours to write, due to many unexpected technical issues) and it's the first time in years I had to use the %LineFeed% token in a macro.

This macro should be placed in a macro group which is active ONLY when your browser window is open (the only browser I tested was Safari. It may need modifications to work in other browsers.) And this macro uses an image detection action which means it won't work in Dark Mode, unless you modify it to work in Dark Mode.

In short, what this macro does is watch what you type in the URL field, and place that text into a global variable called LastSearch. It will delete previous lines so that only that "last line" is recorded. It certainly will not work with 100% accuracy, because many people use backspaces and mouse clicks when entering URLs.

There are two disabled actions at the end of the macro. They were used for debugging, and I left them in this macro (disabled) in case anyone wants to try to improve this macro. They could prove useful for people who want to improve this macro.

I wrote this macro because it was hard to do. Just like climbing a mountain. It may look short and simple, but there are many little tricky problems I had to solve to make this work, which I have not documented here.

Last Search Detection Macro (v11.0.3)

Last Search Detection.kmmacros (22 KB)

Very nice!

How does it differentiate between a search and a normally-typed URL?

It detects normal typing. It needed special handling for the Return/Enter key.

I'm not sure what you mean by "a search". It doesn't detect mouse clicks or URL changes through other methods besides typing, although perhaps it could be upgraded to detect some additional kinds of URL changes like these.

A search: Me typing "Keyboard Maestro Forum" into the address bar and hitting Return.
A normally-typed URL: Me typing "https://forum.keyboardmaestro.com" into the address bar and hitting Return.

But I'm guessing @skillet's looking for something that replicates the "Search the Web" action but defaults the prompt input to whatever the last search was -- something like:

Search Web with Last as Default.kmmacros (3.1 KB)

Image

Ahh. No, at this point my macro treats anything being typed as an address to be saved. It doesn't even validate the address, so a search would count as an address.

I don't really know what skillet's real intent was. Perhaps your interpretation is correct. I would never want to argue with you, as your interpretation skills are superb.

I just saw a nice, tricky problem here that I thought I could solve and I did. It was fun, even if this wasn't what skillet wanted.

Oh my goodness, thank you and I apologize for taking up so much of your time working on this!

Wow that is pretty wild and nice work on this. I would never even thought of something like this, it's a bit of a keystroke logger.

Yes that is what I was looking for without having to modify it for each way different websites do their search since the "Search the Web" action does various means of prebaked searching strings like underscores, + signs etc. depending on the website you are searching.

https://wiki.keyboardmaestro.com/action/Search_the_Web

Yes, you got it. This is pretty close to the stuff I was doing in the YouTube example above.

The best I could come up with is something like this but it requires you to get two dialog promptings. Just a double enter is all that is needed but still seems silly to have to do that.

Keyboard Maestro Wiki Help

Help [Referenced Macro].kmmacros (36 KB)

It was my pleasure, fun and privilege to do this.

1 Like

Idle classification of the %Seach% thingie based on behavior: :duck:

I think of it as a substitution placeholder in a query url, AKA format specifier because, well, it walks and quacks like one. Where you place the %s placeholder is where in the query url your search terms will land.

The unix printf %s thingie behaves the same way:

A command line using the printf command in AppleScript:

do shell script "printf "Hello, %s\n" "World""

In the terminal:

printf "Hello, %s\n" "World"

The value that is substituted doesn't stick around. It is good for one use only.

Web browsers do the same thing with a search term in the address bar.

From days gone by:

Google uses ,%s, as a placeholder for users to manage their search engine queries:

(Once on the above page look for:"'URL with %s in place of query' field")

Keyboard Maestro wiki %Search% doesn't explictly make this conection, but the list of urls with the placeholder,%Search% are look pretty similar to what you'll find if you follow the instructions on google page. More to the point, it acts like a format specifier, er, placeholder, er, thingie.

All it does for you, I think, is URL encode the search term -- you have to do the rest, although that's only copying the appropriate URL from the Action's web page.

For my macro above, all you have to do is add another field to the prompt for search engine selection then "Switch/Case" the engine choice to combine the engine-specific URL with the search term. And you could either default the engine choice or have "last used", as with the search term. You could even choose on the fly with an hot-key modifier...

After a bunch more testing I think you are right and I didn't realize that.

A wiki search does this for "Apples and oranges" search.
image

https://www.amazon.com/s?k=Apples%20and%20oranges

Turns into this I guess for end user to see and work with easier.
image

image

image

Perhaps the websites URLs are changing than is being displayed their own format and it is not just %20 for spaces.

Okay thanks, I will do that then without the fancy/unique little prompt box with what website you are searching.

That is cool I think I could piece that one together if I think hard eneough and search the forum.
Thanks for all the help on this.

Don't confuse the request you are making with what eventually appears in the browser's address bar after the site has processed the request.

Try it yourself -- type the Wiki-documented Amazon request, replacing %Search% with your URL-encoded search term:

...then hit Return. After a moment and a couple of refreshes the result appear and your browser address bar will show:

That's not to say you can't use the version returned in your browser's address bar -- this works perfectly fine, for example:

image

...but you'll have to check for each site.

Abosolutly, and thank you for your help on this. That is what I meant by the following but you may mean something else.

I guess on some servers they return something easier to read for the end user which is nice. I'll take + over %20 too bad that wasn't what became the standard for spaces in the first place.