Restore last X windows

I have a macro that starts at an arbitrary time, minimizes the 2 frontmost windows, and then does some stuff.

When the macro is done I'd like to restore / un-minimize those windows. Keep in mind that since the macro runs at an arbitrary time and I won't be watching it - I won't know what applications those windows are.

That's why I would like to just restore the last X minimized windows.

I can see there is this option via the Manipulate Window action, but it doesn't do exactly what I'd like:

Keyboard Maestro Export

There is no good way to do this in general.

If you know the application, you could perhaps remember the title of the two windows and use that to unminimize them.

Keep in mind also when you say β€œI won't be watching it” - if the Mac is screen sleeping, saving or locked, you probably cannot perform any UI actions anyway.

1 Like

This should get you started:

Minimise then Return.kmmacros (6.0 KB)


I've left a slight pause in at the end because some apps take a moment to re-activate. You could change that to a "Pause Until..." and test "The text: %ApplicationPath%0% is %Variable%Local_frontApp%" if you want to minimise the wait.

1 Like
set numWindowsToRestore to 2

tell application "System Events"
   set minimizedWindows to {}
   repeat with proc in (processes whose visible is true)
      repeat with w in (windows of proc whose miniaturized is true)
         set end of minimizedWindows to w
      end repeat
   end repeat
   repeat with i from (count minimizedWindows) to 1 by -1
      if i > (count minimizedWindows) - numWindowsToRestore then
         set miniaturized of item i of minimizedWindows to false
      end if
   end repeat
end tell
1 Like