I'm trying to build a macro that will click the button that means "don't save" when closing an unsaved document in any app. The problem is, different apps have different names for the button, including:
- Don't Save
- No
- Delete
- Revert Changes
and so on.
At first, I just augmented a simple script with if/then/else hard-coded with the various button names. However, I keep running into new variants.
Is there a way I could define a list of button names, have KM check for a button name on the list, and click the named button KM finds? That way when I find another name in the future, I can just add another name to the list, instead of building another branch in the if/then/else tree?
Thanks for any suggestions.
Yes. You can use a RegEx pattern for the Button Name, which will actually be your list of names.
Here's an example macro that works with TextEdit:
MACRO: Click on Any Button with Name in List [Example]
**Requires: KM 8.2.4+ macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))
#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/f/8/f80e3a76e831daa7009f98717ff0172cf55eb1d6.kmmacros">Click on Any Button with Name in List [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---
<img src="/uploads/default/original/3X/9/f/9f91356542cd7798b02790052f4aa38eb16cc424.png" width="580" height="696">
Does this work for you?
1 Like
YES. Wonderful. Thanks for that.
Solves the problem and opens my mind to even more possibilities.
1 Like
This works very nicely, and I just used it to build a similar macro for a changing menu name.
Forgive my ignorance, how does it work exactly? I get that the regex is replacing the with a |, but I don't understand how the Press A Button/Choose A Menu responds to
Don't Save|No
differently than
Don't Save
No
The secret is in the RegEx alternate metadata. So what the Press Buttom Action sees is this:
^Don't Save|No|Delete|Revert Changes
which tells it to match a Button Name with ANY of the names shown between the alternation symbol |
.
Make sense?
Yes! And thanks for the link.