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".
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?
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?
Here is my workaround macro.
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%
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?
"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).
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?
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.
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
------------------------------------------------------------
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?