Unminimize windows of the current application

Hello,

I often have the multiple windows of an application and minimize some of them. I have been using this script to unminimize them with a shortcut (using BTT).

try
    tell app "System Events" to tell process "Dock"
        click (last UI element of list 1 where role description is "minimized window dock item")
    end tell
end try

I’ve got this script from this thread:

The thing is that it always unminimize a window that is minimized recently regardless of the current active application.

How could I unminimize the window of the currently active application?

(I’m a software engineer but Applescript looks like a black magic to me so I don’t know where I should start.)

Thanks.

I haven’t tested this extensively, but it should un-minimise the mostly recently minimised window of the frontmost app.

try
	tell application "System Events"
		-- We could also just use "process" but "application process" seems like the more precise class
		set frontProcess to the first application process where frontmost is true
		-- Windows should have a `miniaturized` property, but the attribute seems to be more reliably present
		set appMinimisedWindowNames to the name of the frontProcess's windows whose value of attribute "AXMinimized" is true
		
		tell process "Dock"
			-- Find the minimised windows in the dock 
			set dockMinimisedWindows to the UI elements of list 1 whose subrole is "AXMinimizedWindowDockItem"
		end tell
		
		-- Find minimised windows in the dock with names matching those fetched from the app.
		-- Using `name` here will cause problems if multiple apps have windows with the same name, but I haven't found a better property on which to match.
		set dockWindowsMatchingApp to {}
		repeat with minimisedWindow in dockMinimisedWindows
			if minimisedWindow's name is in appMinimisedWindowNames then set end of dockWindowsMatchingApp to the minimisedWindow
		end repeat
		-- The "last" element in the dock will be the one most recently minimised
		click the last item of dockWindowsMatchingApp
	end tell
end try

Thanks a lot for the script, @dwrs. I didn’t expect the script to be so long like this.

Unfortunately, it doesn’t seem to work when I run it with BTT for a Chrome window.

I’d like to debug and fix the issue but I have no idea how I can do it. I tried to show variables in a dialog something like this display dialog "my variable: " & myVar but it doesn’t show anything. I learned that there’s a log function but it seems I can only see the log when I run it with Script Editor.

No problem. Yeah, it’s quite long because I had to correlate the two separate lists and I was aiming for clarity rather than conciseness.

Can you describe what is or isn’t happening? As a software engineer, I’m sure you know how hard it is to debug an issue without a description.

For the first stage of debugging, I’d run it in Script Editor and change the where frontmost is true to where name is "Chrome" so you can see the output/add log statements.

Nope. The miniaturised property is a property belonging to window class objects of a scriptable application. Although they share the same human-readable term, the window element of System Events is a subclass of UI element, and is a child of a process (or application process) object. Whilst process and application process are synonyms that form identical references, and are thus interchangeable, the application.window class and application('sevs').process.window class are distinct and so are not aware of each other’s scripting terms.

It’s not clear whether you specifically want to restore a single window in the case where multiple windows are minimised, or whether you wish to restore all windows but typically only have one minimised at a time.

I’ll cover both situations.

To restore a single minimised window (usually, this will be the window of the particular application that was minimised most recently):

tell application id ("com.apple.systemevents") to tell (process 1 ¬
	where it is frontmost) to tell (windows whose attribute named ¬
	"AXMinimized"'s value is true) to if (it exists) then set the ¬
	value of its attribute named "AXMinimized" of item 1 to false

Or, to restore all windows of an application that have been minimised:

tell application id ("com.apple.systemevents") to tell (process 1 ¬
	where it is frontmost) to tell (windows whose attribute named ¬
	"AXMinimized"'s value is true) to if (it exists) then set the ¬
	value of its attribute named "AXMinimized" to false

This is identical to the previous script except for the omission of "of item 1" in the final clause. However, since the result of restoring all minimised windows will necessarily be such that none of the application’s windows will be minimised, you can probably get away with the slightly briefer:

tell application id ("com.apple.systemevents") to tell (process 1 ¬
	where it is frontmost) to set the value of the attribute named ¬
	"AXMinimized" of every window to false

If my scripts above happen to suffer with the same issue, then it is likely that the Chrome window you wish to restore belongs to a different process than the Chrome application that has focus, for example, if you were using a progressive web app that runs in its own Chrome instance, or if you opened up a separate instance of Chrome yourself.

I looking for this feature too but here i have two solution: app swish y un atajo
1.- Swish for macOS
2.- app atajos - crear nuevo atajo - minimizar todas las apps - agregar al dock
*.- the general idea is emulate “option+clic” or “cmd+m” when the mouse is over the dock
*.- that scrip i dont know how use it so if you can explain please