Global hotkey (⌥⌘ + 1 … 9) to minimize/unminimize browser windows respectively assigned to the order of their creation

Hey everyone,

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 :+1:... 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.

Thanks a lot in advance.

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.

Example Output

List of All Windows for the Front Browser


The Window Title is shown in the list, but the Window ID is attached to each item. For example:
1573__Apple

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Toggle Window Minimize of Browser List [Example]

-~~~ VER: 1.0    2020-10-02 ~~~
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:

Toggle Window Minimize of Browser List [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Macro Author: @JMichaelTX

PURPOSE:

  • Toggle Window Minimize of Browser List
    • 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

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Activate any window in the Browser
  3. 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

  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. 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.

1 Like

Thanks a lot. :+1: 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.

Works like a charme :ok_hand:
Thanks again!

1 Like