Is it Possible to Select a Menu Item Using a Name Containing Variables Set by a User?

Hello!

I'm creating a series of macros that select menu items based on user input which is stored as a variable. That said, I don't want to have to remember the exact name of every menu item, I just want to get "close enough".

27%20AM
How do I set it up so that it selects a menu item containing the variable rather that requiring that the menu item exactly match the variable?

Additionally, I'd like to be able to store user input in different variables, one for each word typed. Is this possible?

Thanks for the help!

Use the regex option of the Select a Menu Item action (TIP: You can get to that link by selecting Help from the action (gear) :gear: menu).

Regex has to start with ^ so basically:

^.*variable

or if you mean the value of a Keyboard Maestro variable:

^.*%Variable%Your Var Name%

Assuming that the variable does not contain any regex characters like []().*?.

2 Likes

Thank you! I will try this tomorrow!

I can't figure out how to get it to store each user-typed word as a separate variable in the first place in order to use regex to selectively match them. Any ideas?

Thanks for your help!

Hey @Footbaggin,

See: Assign all Chinese Character Snippets into Variables - #2 by ccstone

You need to tell us how the user is inputing these words, so we can give you informed advice.

-Chris

1 Like

Sure. I will post me macro tomorrow when I'm at work. Thanks!

Here is my workaround macro.
51%20PM
It doesn't work very well because it assumes that you remember at least a string of the words in the window titles consecutively. The trouble is, with my usage, there will be many windows open with crossover keywords and I often remember the first and last words in the title but not the middle.

If the actual window I want to find is titled "200 Act 4 Notes Work 0903 Night" but I only remember that I want the window called something like "Act 4... Night" I want to be able to type "Act 4 Night" and store it kind of like %Act% %4% %Night% then title match all three variables.

I want to prompt for user input:
"What are some words in the name of the bin you want to select?"

Store each individual word typed as a separate variable: Bin Name1, Bin Name2, Bin Name3, etc...

Then Bring to front:
Window With Name Containing %Bin Name1%,%Bin Name2%, %Bin Name3%

Thanks a lot for your help!

Found some answers here:

I'm actually still stuck.

55%20PM
I was able to split the text into variables successfully but I get an error when I try to find the window containing them. Many will often be empty. Any thoughts?

Thanks!

What happens if you try "windows with title matching" and separate the variables with a pipe character (|)?

image

1 Like

Will give this a shot, thanks! What is the pipe supposed to tell it?

"matching" is KM's way of saying "use regular expressions here", and the pipe character is what tells the regex to look for matches on both sides of the pipe. In this case, if you have two variables with "Act" and "4" as their values, using matching and separating the variables with pipes should tell KM to bring forward all windows that contain either "Act" or "4" in their title, regardless of where in the title they fall (i.e. it should match "Act 4 Night" and "Night 4 Act" equally).

1 Like

Hey @Footbaggin,

I think you're taking a more difficult than necessary approach with this...

The Prompt With List action will take any combination of words to find a result, and that saves you from having to do the heavy-lifting yourself.

Try the appended macro.

-Chris


Download: Bring Window to Front Using Prompt with List v1.00.kmmacros
      [ 5.5 KB, Creator: KM v9.0.1 ]

3 Likes

Hey Peter (@peternlewis ),

Thanks for implementing %WindowName%All% !!!!   :smile:

-Chris

3 Likes

This works well, thanks ccstone!

One more question. This macro seems to only be able to search the list of windows in the frontmost application. Most of the time this will be fine but, sometimes, I need to search all open windows from all open applications. Is that possible using this approach?

Hey @Footbaggin,

You'd have to iterate through all the apps and get all of their windows – then you'd have to be able to identify what window name belonged to what app.

This can be done with Keyboard Maestro, but I find it easier to do with AppleScript and System Events.

This script will produce a flat list of window names prefaced by their app-name – run it in the Script Editor.app to see how it works.

Use with an Execute an AppleScript action.

From there you can use the Prompt with List – massage the returned item a bit – and use a Bring a Window to the Front action to activate your chosen window.

-Chris

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2019/09/04 16:36
# dMod: 2019/09/04 17:07
# Appl: System Events
# Task: List the Name and Process of Every Window of Normal Apps.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @List, @Name, @Process, @Windows, @Normal, @Apps
------------------------------------------------------------

set windowList to {}
set flatWindowList to {}

tell application "System Events"
   set processList to application processes whose background only is false
   set processNameList to name of application processes whose background only is false
   
   repeat with theProcess in processList
      set end of windowList to name of windows of theProcess
   end repeat
   
end tell

set processNameIndex to 0

repeat with ndx1 in windowList
   set processNameIndex to processNameIndex + 1
   repeat with ndx2 in (contents of ndx1)
      set end of flatWindowList to ((item processNameIndex of processNameList) & " » " & (contents of ndx2))
   end repeat
end repeat

set AppleScript's text item delimiters to linefeed

return flatWindowList as text

------------------------------------------------------------
1 Like

So, I'm using the Prompt With List method and it works really well but with a key flaw. It turns out that in my workflow I am commonly combining windows into a single tabbed window. The issue there is that KM can only see the name of the currently selected tab. That said, all of the window names are viewable in an application submenu. So, is it possible to use Prompt With List to %MenuName%All% or some similar token? If so, how would I specify the submenu? Thank you so much for all the help!!!

I tried using apple script to get the names of all the menu items. It works but it stores them as a string in the KM variable. How can I tell apple script to parse the items out into individual items?

49%20AM

Add these commands to the end of your AppleScript:

set AppleScript's text item delimiters to linefeed
return windowList as text
1 Like