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.
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.
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.
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
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:
I am never quite sure which binds more, the $ or the |, so I end up writing it like this:
But yes, either will generally be interchangeable for simple cases.
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).
^ 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: