Looking for a simple regex formula I could use with the 'show or select menu item' action to cover menu items with multiple toggles

hello,

Many apps have menu items that change depending in the environment the user is working in.
For exemple, in Scrivener, the same menu item may toggle between a simple show/hide to more complex toggles consisting of sometimes 4 different variants depending on what I am doing (working in the main editor, playing around with virtual index cards, etc).
In other words, I would like to understand how to use regex to create one single 'show or select menu item' action which would cover all toggles instead of writing a different action for each eventuality.

For example:

  • I want to create a 'show or select menu item' action
  • I would like to use a regex so that the action works with all 3 possible menu item toggles in the Documents (menu title), copy (submenu) , namely
  • Copy to Main Document
  • Delete and send to Clipboard
  • Move to next Index Card

This is just an example. I am asking a general question about managing toggles with regex.

thanks very much for your time and help

image

You are on the right track. You just need to use the RegEx metacharacter for Alternation "|" instead of the " / " you are using, and to start the string with a caret "^" to let KM know this is a RegEx expression:

^Copy to Main Document|Delete and send to Clipboard|Move to next Index Card

However, I advise that you proceed with caution. Those three actions look like dramatically different actions to me, so it is possible that an unintended action gets executed. Were it me, I would want different triggers and/or different criteria (IF/THEN or SWITCH actions) to determine which to use.

1 Like

thank you very much !
I will also give your suggestion some thought, especially when menu items are very different
Have a nice weekend

1 Like

You don't need to use a regex at all for selecting one of several alternatives, that is independently supported in the Select a Menu Item action.

The Select Menu action allow you to specify multiple options separated by a vertical bar (eg Show|Hide) to allow for toggling or varying menus. They will also ignore the difference between three dots (...) and an ellipsis (…) so you do not have to worry which one the menu uses.

So just use Copy to Main Document|Delete and send to Clipboard|Move to next Index Card

2 Likes

great ! thank you very much Peter !

Peter, as you know using the RegEx "^" in Actions like this used to be required. :wink:
And, of course, your current syntax is exactly the same as the RegEx syntax.

So, it is easier for me to remember to always use the "^" rather than remember where it is needed and where it is not.

Actually, no. RegEx came after support for this. The documentation for version 4 says this:

The Select Menu and Press Button actions allow you to specify multiple options separated by a vertical bar (eg Show|Hide) to allow for toggling menus. They will also ignore the difference between three dots (...) and an ellipsis so you do not have to worry which one the menu uses.

Technically, not quite. With the bar syntax, the name of the menu item must be match the name of a component in the action. With the regex, unless you add the $ at the end, it need only match the start of the menu item (because that is what the ^ means). And when doing a regex like this:

^abc|def$

I am never quite sure which binds more, the $ or the |, so I end up writing it like this:

^(abc|def)$

But yes, either will generally be interchangeable for simple cases.

Perhaps so for this specific Action. Are there any other Actions that need the "^" in order to use RegEx expression in it? Now, or in the past.

I'm not sure. I don't think so. It is a bit of a hack, basically because there is no setting for the string to say how it matches, it has to have some sort of indication to say it is a regex, and “^” seemed a good compromise since menus and buttons don't generally start with ^. But I try to be consistent, so if I hit another case like this with another action, I would likely use the same technique.

I created a macro that selects a menu item if it contains either .txt or .html. I started with the regex ^\.txt|\.html because I want the dot to appear literally (in the search) rather than standing for any character (i.e., xxxtxt should NOT match, but xxx.txt should). While that works in a regex engine, it didn't work in KM. When I changed it to .txt|.html it worked as I wanted (meaning that KM interprets the unadorned dot literally), though it leaves me wondering how I would code it with a dot to mean any character (as in standard regex).

That didn't come through properly. My original regex had a backslash before each dot.

I edited your post to put the regex patterns between backquotes (`).

In order to invoke RegEx in the menu matching, you must start the string with a caret: ^
So, try this:
^\.txt|\.html

Without the caret, KM just uses plain text matching, but uses the vertical bar | as a logical OR.

Sorry, I forgot to mention that I started with a caret in all cases, so what you wrote is exactly what I started with. But KM still interprets the dot (and apparently the backslash) literally.

If that is the case, then @peternlewis will have to address whether this is a bug or not.

^ means treat the expression as regex. But the entire expression is still treated as a regex, and so the ^ is still relevant, and means “match at the start”. So you need to add .*, and perhaps $ to the end:

^.*(\.txt|\.html)$
1 Like