Using ChatGPT to generate Regular Expressions

Celebrating a Regular Expression win using ChatGPT! Expecially given I'd been doing this with UI scripting previously.

Below is a link to ChatGPT exchange resulting in a Regular Expression that worked in a macro.

And here's the resulting macro that takes the name of the selected Finder item (folder in my case), copies the name to the clipboard, edits the name, creates a new folder at the same location, and finally pastes in the new folder name:

10)Copy+ƒ+24 copy.kmmacros (24 KB)

YMMV with more complex expressions. I'm sharing just to suggest a possibility and not making claims about the robustness of this route.

You don't want to just ask Keyboard Maestro for the clipboard with two trailling characters dropped ?

5 Likes

Nice, Yes, yours is the more eloquent solution. Thank you! I'll explore the options in that action and save the fancy stuff for if I run into limitations.

You can count on Rob to come up with a non-Regex solution. :joy:
You constantly make my day!

KC

1 Like

I wonder whether, as casual macOS desktop scripters, we are particularly prone to getting tripped up by the Jamie Zawinski gotcha ?

Some people, when confronted with a problem, think
“I know, I'll use regular expressions.” Now they have two problems.

One hypothesis might be that the scripting language which we were historically offered, and tended to settle into (AppleScript) is unusually unhelpful with string processing.

The elements that make legible (and easily adjusted) string-munging solutions much easier to find and read in scripting languages than in regular expressions are:

  • splitting
  • value names (constants, variables)
  • built-in string function libraries
  • recursion

AppleScript happens (by chance, presumably) to be a bit unhelpful with all of these.

(Splitting has to be set up with string delimiter hacks, recursion stack soon runs out, and built-in string functions are not very generously provided)

I wonder if this underlies the prevalence on macOS desktops of "I know, I'll use regular expressions" ?


(Luckily, Keyboard Maestro's filter and substring actions, and variable arrays with custom delimiters, fill a lot of these gaps for us)

2 Likes

I'm thrilled when you provide Keyboard Maestro native solutions and the dialogue behind it. You are committed to your mission and I'm all for it. I think it's hilarious because when Regex comes up, I'm like Rob is coming! (like Omar on HBO's - The Wire). I appreciate that you provide significant context and meaning behind your opinion. I know you aren't against regex, when it's needed, but you make the case that it is not needed over 85% of the time. Thank you for that!

KC

2 Likes

Yes, taking the time to share your thinking makes available a vertical dimension that only show up in the dialogues here in the forum. I’ve got a new inroad for dealing with strings and benefit from other shares even when I’m only able to glean bits from each share. Thanks again!

2 Likes