Help With Regex in Conjunction With UI Browser / Script Debugger

Hello community, I've started to explore the possibilities of Regex in connection with the Search & Replace action.

I wanted to remove the green marked lines from an AppleScript of the UI Browser, in the Script Debugger:

-- Use this script as a wrapper for GUI Scripting statements when you are confident that GUI Scripting is available and enabled or that the user knows how to enable it if necessary

activate application process "Drafts"
tell application "System Events"
tell process "Drafts"
-- insert GUI Scripting statements here:

end tell
end tell

I managed to do that with the DevUtils app

2022-11-02_22-08-18

As you can see in the following video, this also works:

05)Click <625E 221102T203804>.kmmacros (31,3 KB)

Click to show image

However, when I test the regex I created in regex101, I get an error?

Can anyone help me or improve my regex?
As I said above, I'm still a beginner.

I think ^-- should be enough:

AppleScript lines except comments.kmmacros (5.0 KB)

I create my own tell blocks with KM, using %Application%1% to grab the process name. Does the job.

2 Likes

@ComplexPoint thank you very much for your macro :+1:

If I remove the

2022-11-02_22-08-18

asterisk in the regex and the

run it like this

2022-11-02_22-07-33

the text line "activate application process "Drafts"" is recognized in the DevUtils app, but not in regex101 :thinking:

You have an \r line delimiter in there ?

The unix and macos standard is \n and that difference (or a difference in regular expression dialects) might be relevant.

Generally, however, regular expressions are seldom the right place to put any complexity.

A good rule of thumb is that once a regular expression is no longer readable at a glance, or has grown beyond 3-5 semantic units, it is probably not the right tool for the job.

If you are going to invest time in learning a formal language, you might get better value from something that is less restricted, and is readable as well as writable, like Python or JavaScript.

They will let you stick to the useful kind of regular expression, which is always a very simple one.

In JS for example:

Application("Keyboard Maestro Engine")
.getvariable("aSnippet")
.split(/[\n\r]+/ug)
.filter(x => !x.startsWith("--"))
.join("\n");

AppleScript lines except comments Ver 2.kmmacros (2.3 KB)

1 Like

@ComplexPoint thanks for the further macro :+1:

Thanks for the hint.

I will do that.

I'm home now, so here's what I use:

AppleScript - Generate Tell Block.kmmacros (20 KB)

Macro screenshot

1 Like

@noisneil it can be so easy :wink:

Thank you for sharing the macro. I'll rework my macro again.