Working on macro to find specific string by action type

One thing I would like KM to do natively is to find certain things based on types of actions.
For example, if I know that some Execute AppleScript actions have the word "dialog" and I want to find those, it's almost impossible to find them, because the search field is not very accurate and sometimes it's super slow. It will also show other things that I don't want to see.

So today I was finally testing ChatGPT for the first time and I decided to ask about this, making for it to provide some solution that relies on XML. I got an AppleScript with lots of stuff, but I'm not that good at it (yet), but it could be a starting point.

If someone wants to go from here and help with it, that would be awesome!

Here's the macro with that script and a prompt I added. My idea is that we have a complete list of all the actions in KM (eventually that will be in a variable instead of the prompt field) and then we can type whatever we want and then those variables will be used in the AS:

Find string inside specific action type Macro (v11.0.2)

Find string inside specific action type.kmmacros (23 KB)

Keyboard Maestro Export

So after the script is working, my idea was to show some kind of window that displays the various results that we can navigate to then open the macro and action, similar to this:

chatGPT forms no understanding – just a system for generating text that looks plausible to you.

What Is ChatGPT Doing … and Why Does It Work?—Stephen Wolfram

I'm already struggling over the "why" question, but letting that go for now...

A better starting point for "a list of AS actions that contain the word dialog" might be

tell application "Keyboard Maestro"
	(get id of every macro whose xml contains "<string>ExecuteAppleScript</string>" and xml contains "dialog")
end tell

That will contain false positives, eg where a macro's first action is "Execute an AppleScript" and later one has been renamed "This displays a dialog", but it would be a good start. You could then iterate through the list of IDs, checking each of a macro's actions in turn for the same thing. That still may not be enough to eliminate all false positives (a "Group" action might contain two actions, each with one of the things you're searching for) so you'd then have to decide how far to go.

But you might find that being more precise with your KM searches is good enough in most cases. For example, don't search for "dialog", use word:"display dialog" instead.

I understand that ChatGPT lacks a lot of capabilities, context, etc.
From my experience today, I noticed that depending on what you ask it to do, it can come up with some interesting answers, but in the end it's always good to check them and come up with your own conclusion. But that also applies to humans anyway, right?

For this particular case, I just got the script and since I'm not an AS expert, most of it looks foreign to me, but maybe others could find useful pieces?
I asked for a simpler script that I needed and it was pretty accurate. It made the script work as expected and since it was very simple, I was able to understand what it added and how it was processing things.
Now if those are "proper" ways of doing things, I don't know. But even with humans sometimes each person comes up with a different approach, so as long as it works...

It's not the first time (or even the tenth time) that I need this feature. Sometimes I recall having a certain word or group of words that I need to find. A lot of times it's because I want to reuse a certain script, or just any other reason, really.

I will see if this sparks some ideas. Thank you.
Even if that solution is not perfect, maybe I can come up with something that works better than the Search field.

I used the word "dialog", which could be more specific by using "display dialog", but that's not always the case. For example maybe the word I'm looking for is a generic word that can show up as the action's name or something else, but I just want to see all "Execute AppleScript" that contain that word in the script, automatically excluding the name of the action, for example. So if the name of a certain action contains the word "parallel" (just an example) and another action has the word "parallel" in the script itself, I just want the second one to be shown to me, not the first one.
When I perform (or wish I could perform) this type of search, it's really never to find an action by i's name. It's always by something I typed (script, variable's name, regex, etc).

That's where it gets tricky -- you're basically going to have to build an XML tree containing all your macros and all their actions (and all actions contained within those), then search the tree for any node whose "MacroActionType" is "ExecuteAppleScript" and "Text" contains "words_to_look_for". I'm not saying it can't be done, but it might be quicker to have a nicely refined Search in KM then click through the results!

About ChatGPT:

Then perhaps there was a copy'n'paste error? Because the script, as presented above, is missing two fundamental lines at the start without which it won't work:

use framework "Foundation"
use scripting additions

It was not this script. It was something else. I had the script, but some things were missing that I wanted it to perform. When I ask it to add those, it did, I tested it and it was working without any issues. And since it was a simpler script, I was able to look at the changes and it made perfect sense.

Again, this is where someone who's more experienced than I am, can come and check it (as long as they feel that this macro can somehow be useful, of course). The point of this thread was to just share my needs, seeing if others have the same needs, and if so and wanted to help build the macro, then we could build it. I was assuming that the starting point shared by ChatGPT could be something others could find useful. Not a perfect script, especially because ChatGPT clearly said:
"Remember that working with XML and automation tools like Keyboard Maestro can be intricate, so don't get discouraged if it takes some time to get everything working smoothly. Here's a simple example to get you started:"

and at the end:

"This is a very basic example and would need to be integrated into Keyboard Maestro with appropriate XML parsing and handling of actual macro data."

So yeah, I understand that it will not give me a super complex script without any flaws, but maybe what it provided could be a starting point. Maybe not...

Unfortunately, this was not always the case. The Search field seems very "counterproductive" to me. As I mentioned, sometimes (most of the time) is very slow to respond. Then it's not very intuitive, because just using word:word to search versus word:"words to search" will be different so I always have to think which one I need. Then sometimes, for some odd reason, if I press a modifier, it adds it to the field, when I clearly just pressed it super quickly.

But I will check your suggestion as a starting point and look at the ChatGPT thing and see if I can understand what can be done, even if not "great", at least something that's less painful than the search field haha

Perhaps if the search box could just add a code: keyword, that would help a lot: Anything after that term would only be searched for in KM script actions or HTML custom prompts. The problem with any search we may write is that users can rename things—almost none of my actions that run AppleScripts are named Execute AppleScript. So any search using XML to find those keywords will fail.

But at the application level, Peter clearly knows which actions are scripts and which are not.

Personally, this would help me a lot, as I'm often looking for words in code blocks, and KM can't (at least mine doesn't) highlight the actual word in the code block—just the whole code block indicating there's a match somewhere.


Not really (and that's one of the reasons I thought this was a good starting point). I have this action with the name "Activate Reminders > Today List > Maximize window"
If you look at the XML, here's what it shows:

So basically that's all we need: type of action (MacroActionType) and then the word (or words) we need to find, which seems to always be included inside <string></string>

So I was thinking that the first step would be to check if the action being scanned matches the MacroActionType we picked in the prompt and if so, check the word. If not, move on to the next action, so maybe it doesn't have to check the whole XML. Not sure if that makes a huge difference, but I would assume that it would make some difference.

The same for me. Especially when I'm trying to remember how to do "this" to "that" when it comes to AppleScript. I know I used a similar script before and I remember a word or a group or words I used... Now... where is it? :man_shrugging:

That would cover that scenario, but I can see how other scenarios are not covered, such as a field where we type some Regex, the name of a Local variable, or even the content of a variable, etc.
With this macro I would like to build, when we pick the type of action, we are already filtering out a lot of options that we don't want to show. We can also have "All" to scan everything. Why not...?

And now I'm thinking: maybe this would also be good in those cases where the text is hidden, but KM still highlights the action? There was a thread about this same issue, where someone even mentioned the issues with sharing the macro publicly, privacy issues, etc... So I'm not sure if using the XML it shows that info or not?

Not the same. Humans produce an understanding. Large Language Models don't.

An LLM (see the Wolfram article) chews the input up into tokens, profiles them distributionally, and regurgitates them in a stochastic sequence shaped only to look distributionally plausible, but not informed by any understanding.

(When people regurgitate their dinner into the street, there may sometimes happen to be some protein-bearing chunks in it here and there, but it profits us not to search them out).

I would argue that "some humans" do hahaha it also depends on how many drinks you had that night...
I understand what you mean, but it's not that what ChatGPT shares is completely nonsense. I mean, how many times have we created macros that are nothing but a mess of actions, relying on Found Image actions, to then realize that someone can achieve the same thing with a simple AppleScript action? I know I have too many of those. In the end, if it works, it works, with or without context or understanding.

As I said, today it provided me with a few versions of a code I had, which was incomplete, and it's working and making perfect sense. Now the "theory" behind it, doesn't seem to be relevant (in my opinion). I believe it's up to the user to interpret what's being giving to them.

I think you are taking it to the extreme. I don't think that what ChatGPT "regurgitates" could be compared to someone's puke. I would need a BIG reason to touch someone's puke (if there's any...). I learned a few things today with the scripts ChatGPT provided me, because I have a "basic" understanding of how some things work and it made perfect sense to me. Now this XML script may be a big mess, I don't know. That's why I shared it here, so others could see if it's useful or not. Apparently, it's not. Let's move on then...