Reusing the same filter for multiple variables

I have to use this same "search and replace" for multiple variables.
Is there a way to achieve this in a more elegant way? (rather than just duplicating the step in KM?)
I have an ever growing list of variables that need this step. It is manageable now, but it will become increasingly difficult.

See under: Subroutines [Keyboard Maestro Wiki]

1 Like

But more generally, the KM Filter action already includes an HTML entity decoder:

Decode HTML entities.kmmacros (3.0 KB)


would it look something like this:

if I was using this method.

(obviously the KM Filter method for HTML entities is better, but I was wondering if this was generally how the subroutines thing worked?)

oh! Very interesting! Thanks

EDIT: This lis amazing.

when using decode HTML entities, can I put more than one variable in a step?

(Even if I cannot, this will still save me time!)

IN other words, would I need to duplicate the below for each variable? (Or I should use the aforementioned subroutine method?)

An aside: in one very specific case I need to do a weird filter:

is this also a built in filter in KM by any chance?
My (wild) guess is that it's called encoding HTML entities? (But maybe not because it isn't simply the reverse of the step I did in the OP.)

EDIT: I tested, my guess was incorrect

EDIT: through trial and error, this seems to be a double decode

I think you may have been approaching this at the wrong level – that looks like a mixture of encoded and decoded that probably doesn't need to arise.

HTML entities can be encoded with names or numbers – here you can see the same string encoded in two different ways,

&  < > "

&  < > "

and yes, KM's filter actions does provide for encoding as well as decoding:

Encode HTML entities (using names or numbers).kmmacros (3.8 KB)

Screenshot 2024-02-22 at 7.41.48 pm

1 Like

Separately, if you ever do need to use subroutines, here's an example, of:

  1. a subroutine, and
  2. a macro which uses that subroutine.

Subroutine example Macros.kmmacros (4,9 Ko)

1 Like

Not sure that I have a picture of what you are trying to do there.

The Decode HTML Entities action will resolve all entities in a document.

How do the multiple variables arise ?

What's in them ?

I scrape various elements from a large HTML document
I then filter each variable (to decode HTML entities)
Are you suggesting that, rather, I should decode the big HTML document and THEN scrape?

An example of how I scrape

How i then make the text readable

Decoding HTML entities is actually better

Your filter suggestion is better (it saves me even more steps)

But now I have a situation where I want to feed it like 20 or more variables and

ooh thanks for this.
I have downloaded it.
However it gives me a headache trying to understand it. I wish there was a video tutorial.

What is return text ?

Is it like display text?

can do i like this:

Well stepping back a bit, its easier to extract text from a webpage using things like document.querySelector, which completely avoid all these issues with HTML entity decoding.

There are probably plenty of examples here, but you could start by looking at a contribution like:

Extract Data from Web Page and Paste into Mac App [Example] - Macro Library - Keyboard Maestro Discourse

1 Like

In another filter example I am changing some words for emojis like this:

It's just a whimsical thing - not important. But there are about 20 or 30 replacements.

That will filter the variable instance_ShortProductTitle

But next, I want to also do it for several other variables. This will take lots of copy and pasting. How would I do this using subroutines please?

oh really I can use that instead

I didn't know that it would make things better.

I will use Document Query Selector everywhere instead. I had no idea it made a difference! thanks!!

update on this -

the special filter is no longer required since using the Query Selector method to scrape the date - I can just do a standard filter

1 Like

Just looking at the Return from subroutine step for the first time.

What if I want it to always return the value to the source?

0 Subroutine example Macros.kmmacros (5.5 KB)


Screenshot 2024-02-23 at 16.19.58

You were able to put a space between local_Source and String? Does String need to be there? Is it creating a new variable?

From the Wiki, Manual, Variable Naming Rules:

  1. Variable names must start with a letter, and then can contain letters, numbers, spaces, or underscores.

local_Source String is a unique variable, different from local_Source

If you add a Return Result Action into a Subroutine it will always return the specified value as a result to the Execute Subroutine Action that called it. In the calling/returning end the value will be stored in the variable you define in the Execute Subroutine Action.

In your example subroutine macro you do not set any value to the local_Result variable, therefore it will return empty.

I am not sure what exactly you’d like to return, but if you where to specify your %Variable%instance_Title% in the Return Result Action, it would return that variables content, saving it in the calling end to the local_Result variable that you’ve specified in the Execute Subroutine Action.

EDIT: The checkbox in the Subroutines header field, the one labeled "Returning a value", needs to be checked for it to be true, that the Return Result always returns, of course

1 Like

woah variables can have spaces :exploding_head:
That breaks my brain for some reason
Thank you, I did not know that!