Search for Variable by Name?

Has anyone created a "Search for Variable by Name" prompt-type macro? I don't want to reinvent the wheel, if someone else has already done it. Or maybe there's something built-in for it?

What would such a macro be used for?

Global macros are listed in Settings (or is that Preferences) and I only use three (the legacy "Result Button" variable keeps returning there too for some reason).

Perhaps you want to clean up the local and instance variables that appear in the "variables" menu for text fields...! :thinking:

What's it supposed to find, all the macros that have actions using that variable? Is it that you want to list all variables and allow the user to pick one to search for?

To make a long story short, I have a Custom HTML Prompt that contains, among other things, this:

image

It allows you to enter a "Prefix Variable", which is an existing global variable. I'd like to be able to search for existing global variables, making it easier to enter commonly used variables. Mostly for if you've forgotten what variable you used elsewhere.

It's not difficult to do, especially since a Custom HTML Prompt has access to all variables, but like I said, I didn't want to reinvent something someone else had already created.

Ah, globals only is a different kettle of fish... Quickest is probably:

tell application "Keyboard Maestro Engine"
	name of every variable
end tell

Custom HTML Prompts get access to all variables, so getting them is no problem.

Honestly, while I appreciate the suggestions for how to do this, I only wanted to know if someone else has already done it in something I could consume. I have code from my Variable Inspector Prompt that I can use. I just wanted to make sure I wasn't missing something obvious.

Done before, but I didn't think it would be much use to you since it works directly in the Editor UI:

Search for Global Usage.kmmacros (4.1 KB)

Thanks - that's the type of thing I was looking for. I can call macros from my Custom HTML Prompt, so that's not a big deal.

The only issue is, I'd like it to display the variable's value as it gets highlighted with an arrow key. I don't think that can be done with the Prompt with List action, right?

You don't need to continue finding a solution here - although I totally appreciate your help. I'm just going to code it into my Prompt. ChatGPT has designed a dialog for me I can use.

But like I said, I really, really appreciate your help!

1 Like

Now I am curious about that!

Here's the link, but be aware that I'm still working on this, so I don't know what happens when I change something:

It's a little ugly right now, but I'll clean up the css before I'm done.

image

KM Variables are stored in document.kmvar, and I'm limiting it to global variables that aren't blank:

const variables = [];
Object.keys(document.kmvar).forEach(key => {
	if (!/^instance|^local/i.test(key)) {
		const value = document.kmvar[key];
		if (value)
			variables.push({ key: key, value: value });
	}
});
1 Like