Selecting a Menu Item Using a Variable

Is it possible to select a menu item with a variable name?

In this use case I want to assign a hotkey to a menubar function in OmniFocus but the issue is that the menu item changes.

The menu item is "View -> Focus on project_name" but project_name changes depending on what item is selected in the application.

I was wondering if there was a way to select menu item using a wildcard?

1 Like

I've never used OmniFocus, so unfortunately I can't test this, but I would imagine that, assuming OF supports AppleScript, this should be possible to do by using AS to extract the project name from the item currently selected, then using that name in a variable in the "Select or Show Menu Item" action.

If the project name is also displayed in the front OF window when selecting an item, then you could use a "Front Window Name" token to grab the project name and set it to a variable this way (including a "Search and Replace Variable" action if there's more text in the front window title besides the project name):


but otherwise, I don't think (though I could of course be wrong) that there's a "native" KM method to accomplish this.

Yes. @gglick showed you how to do that.

Yes. The Menu Item can be a RegEx expression. Just start the entry with a "^".

Here's an example that selects the menu if it start with EITHER "Show" or "HIde":

I use this to toggle the menu item.

9 Likes

@JMichaelTX @gglick thank you for your replies.

Using RegEx seems to be the easier solution

1 Like

Worked for me in Photos

^Show|Hide Face Names

1 Like

How would I select a menu item that has many more potential names but all begin with the same word?

For example, right now I'm trying to create a hotkey for ejecting the selected hard disk on my desktop. However, depending on the specific hard drive selected, the second part of the menu name will change to the name of the selected item.

Ahqq5 WmSpZ

Have you tried using the Select or Show a Menu Item action?
It should allow you to click through a series of menu items to select the one you want.

Hey @Llama_Lord,

Select or Show a Menu Item action.

It's one line in that whole wiki page, but you can use regular expressions in menu items by prefacing them with a caret character “^”.

So you can do something like this:

image

-Chris

3 Likes

ooops I thought I had tried that before, but just realized the reason it didnt work before was because I had set that hotkey to something else using another app (BetterTouchTool) :grimacing: Sorry. But it works now!! Thank you!!

1 Like

Hi there, this is all super helpful for me, but I was wondering how to have KM check a menu item that ENDS with a specific word? Using KM in Adobe Premiere to get it to toggle between timelines using the menu, but the timeline names are always different. Not very good with RegEx, want it to click the menu ending in "EDL-REF," so I tried:

42%20AM

Doesn't work though. Any help would be appreciated!

Tried:
.*EDL-REF,?

To use RegEx here, you need to start with a caret symbol: "^"
So, try this:
^EDL-REF$

That will start at the beginning of the menu name, and look for the menu ending in "EDL-REF"

1 Like

Thanks so much for the quick responses, unfortunately neither .*EDL-REF,? or ^EDL-REF$ is working.

Here's a screenshot of the menu if that's helpful.
06%20PM2

This could be due to use of dynamic menus by the app.
Try using the complete menu item name and let us know the results.

1 Like

Yeah I figured the menu type was causing problems. If I enter the full name of the timeline it does work just fine, it would be handy if the regex worked to give more options though :confused: Is there any way to tell it to select the menu item containing the name? As opposed to ending with it? I noticed if I use KM to check if such a menu item is checked, it is able to do this perfectly without the RegEx.

22%20PM

OK that's good. That means it is NOT using dynamic menus.

But I gave you a bad RegEx earlier. Please try this one:
^.*EDL-REF$

I tested this method using Excel windows, and it worked.

If you want CONTAINS, instead of ENDS WITH, then use this:
^.*EDL-REF

3 Likes

Both work great, thanks so much!!

1 Like

I'm having the same problem, but more generally, i.e., the variable in the menu can contain anything.

The app in question at the moment is Apple Mail. One of the menu commands is: "Move to '' again. (e.g., Move to "Acme Inc." again.) (It's found under the "Message" menu.)

Since the variable is wherever you just previously moved a message, it can be anything. So with traditional variables, the menu command might be shown as: "Move to * again". That would select anything that started with "Move to" and ends with "again". The "*" wildcard symbol would show that the data at that point could be anything as long as the text before and after it matches.

Can we do something like this?

I have my macro almost working except that I'm trying to select a menu item using a variable but I keep getting a KM error message:

Macro Cancelled
Select Menu Item failed to find target menu item Window> %Variable%LogicPro_RegionName%.

As you can see my variable name is "LogicPro_RegionName".

Here's my action:

What am I doing wrong?

TIA

I am answering my own question. I added the ^ at the beginning in the Menu Item field and now it works! To be clear, this is what worked: ^%Variable%LogicPro_RegionName%