quick scenario description:
I use the browser Brave and have one open window. KM allows me to easily write a set of actions that checks the window state for the browser, brings it to the front if it isn't focussed or unminimizes it if necessary (using the window index "1"). Let's say I've mapped this to ⌥⌘1.
That works flawlessly ... if there is only one window.
I tried to create a second global hotkey action mapped to ⌥⌘2, which does the same with the window index "2". When two windows are open and the first one is focussed/in front hitting ⌥⌘2 will indeed minimize the second one. And hitting subsequently ⌥⌘1 will minimize window 1, which was still focussed and in front.
However, if I then press ⌥⌘2 again window 1 will show up instead of 2.
Furthermore, I can repeatedly hit ⌥⌘2 (or ⌥⌘1) and previously minimized windows will appear/disappear in alternating reverse order.
Is there any way to have Keyboard Maestro keep track of when the window was opened and map them accordingly to the ⌥⌘ + 1 ... 9 for up to 9 windows and maintain this when minimized?
This surely exceeds my knowledge of KM and has left me puzzled for a couple of evenings.
The window index of the windows change as you activate different windows. So that cannot be used to identify a specific window.
Here is a macro that takes a different approach which uses the unique Window ID of each window.
Instead of having a hot key for each window, this macro presents you with searchable list (using the KM Prompt With List action) that makes it very easy to select any window to toggle minimize on/off.
This is just an example written in response to the below KM Forum Topic. You will need to use as an example and/or change to meet your workflow automation needs.
Browsers Supported:
Google Chrome
Brave Browser
Other Browsers can easily be supported by minor changes to the two AppleScripts.
HOW TO USE
First, make sure you have followed instructions in the Macro Setup below.
Activate any window in the Browser
Trigger this macro.
MACRO SETUP
Carefully review the Release Notes and the Macro Actions
Make sure you understand what the Macro will do.
You are responsible for running the Macro, not me.
Make These Changes to this Macro
Assign a Trigger to this macro.
Move this macro to a Macro Group that is only Active when you need this Macro.
ENABLE this Macro, and the Macro Group it is in.
==USE AT YOUR OWN RISK==
While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
If you have any doubts or questions:
Ask first
Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.
Thanks a lot. I didn't know that you could get the window id of a specific app via AppleScript.
I modified your Toggle script to the following "hacked together" solution:
tell application "Keyboard Maestro Engine"
set frontBrowserName to process tokens "%FrontBrowser%"
end tell
if ((frontBrowserName contains "Chrome") or (frontBrowserName contains "Brave")) then
using terms from application "Google Chrome"
tell application frontBrowserName -- "Google Chrome"
--set winList to every window
set {winIDList} to {id} of every window
end tell
end using terms from
end if
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in winIDList) times
set the low_item to ""
repeat with i from 1 to (number of items in winIDList)
if i is not in the index_list then
set this_item to item i of winIDList as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
set the reverse_list to the reverse of sorted_list
set winID to item 1 of reverse_list
if ((frontBrowserName contains "Chrome") or (frontBrowserName contains "Brave")) then
using terms from application "Google Chrome"
tell application frontBrowserName -- "Google Chrome"
--set winList to every window
set winMin to minimized of window id winID
set winMin to not winMin
set minimized of window id winID to winMin
end tell
end using terms from
end if
return "OK"
It makes use of this sorting subroutine. By that the order of the windows is not impacted by minimizing them.
I duplicated this script for the numbers 1-9, mapped the hotkeys accordingly and changed the winID to the corresponding number.