Set current front window as variable, minimize, then restore after macro

Howdy all. I have certain macros that run every hour and would like to tweak them a little bit.

One in particular resizes and repositions all of my work related windows as sometimes they get bumped around while doing other things.

However, since this happens automatically at different times of the day, many times I am in the process of doing something else in another window or another application entirely. What I would like is to set the current front window as a variable, minimize it, then have that window reappear on top after the macro has run.

Included is what I have tried (minus the work related macro). It works great as long as the current front window is of the same application as the last item in the macro; in this case Google Chrome since it's the last work window to be resized and repositioned. So if I'm in a separate GC window then it works fine. But if I'm in a DIFFERENT application (like Keyboard Maestro) then it doesn't work because it will only allow me to bring to front the window matching the variable in the front application which is always GC as it is the front application after macro has finished running.

What I would like is to be able to bring to front the window matching the variable in any application, or to be able to set that varuable matching window as the front application so I can then restore it.

Does anybody know if there's a way to do this or not?

Thanks all!

Keyboard Maestro Actions.kmactions (5.0 KB)

Yep. Just keep track of the current app BEFORE the other macro changes it, and then reactivate last app if need be:


Here my complete example macro, with your actions and two changes.
Changes in teal you need to make.
Changes in magenta you don't add -- they just simulate your other macro.

MACRO:   Restore Window in Last App [Example]

~~~ VER: 1.0    2018-07-17 ~~~


Restore Window in Last App [Example].kmmacros (9.4 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

BTW, in the future please post a Macro rather than Actions. Most users are unused to importing Actions and don't realize that the Actions are added to the current macro without warning.


Thanks for the tip. That's a neat little command to set that variable (I'm still learning variables obviously).

Unfortunately in my case it doesn't work since my macro resizes/repositions windows from multiple applications (Evernote and Google Chrome in this case). So the "Activate Last Application" command actually goes back to Evernote as it's resized/repositioned first, then GC.

Any other ideas?

Sorry about the commands snafu...I'm still new at uploading!!

OK, since your other macro may activate several other apps, then you'll need to use this script to activate the app by name:


Put This Script in a Execute AppleScript Action

### Requires Keyboard Maestro 8.0.3+ ###
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
  set appName to getvariable "Local__LastApp" instance kmInst
end tell

tell application appName to activate

Thanks! I still seem to be having problems with it though. Here's a new file with the updated macro if you could take a look and see what I'm doing wrong. :frowning:

Return previous front window back to front after macro runs.kmmacros (7.2 KB)

EDIT: Never mind, I just realized I forgot to change the variable name in the Apple Script. Seems to be working now. I'll double check tomorrow at work and if I have any other issues I'll post. Otherwise thanks so much!