Saving current state of all open windows to reopen later

Hi I've been looking around the forum and can't find anything that'll help my specific case.

I have a shortcut to close all open applications. I'd like to save the state of each open window (size and which monitor they are on) to a variable so when I'm ready to return to that state I can just have a macro to return back to where I was via that variable.

The apps that I use (mostly macOS apps) already behave that way. Which apps are you referring to?

Preview doesn't work like that, for one ... and I know there are others, but I can't think of them right now. But Preview always opens files at the top left of the main display for me, regardless of what size and location I left it at.

-rob.

1 Like

And also, macOS has a problem where, after sleep on a multi-display setup, all the windows restore to the main display.

I use our own app to solve that problem, but it should be doable via KM and a fair bit of window manipulation code.

-rob.

I guess outside of that fact I'd like to store which applications were open so I can go back to how everything was. The idea is say I'm working on a project with a few apps open with windows in various monitors and then I take a lunch break and close everything to just watch a youtube video in the main monitor. After the break I'd like to use a macro to return to the saved work state.

A knackered plist, perhaps? Because Preview behaves for me just as @Airy describes.

Have you tried hiding the apps you don't want to use then minimising all your browser windows bar the Youtube one? They should then pop back as they were when you activate/un-minimise them after lunch.

Yeah hiding works well enough if need be but I'd prefer what I described above if it's possible. I have trouble focusing if I don't have a clean desk basically, lol.

It does this on all my Macs. Open a document, move and resize it, close it, then open it again, and it's back at the top left corner.

Perhaps it's tied to the "reopen documents" setting (which I hate and disable) or the auto-save documents (which I also hate and disable)?

-rob.

That's the important bit.

If you close a document, either explicitly or implicitly via "Close windows when quitting...", the windows behave as you describe. IMO that's a bug in Preview, but since other Apple apps (eg TextEdit) also fail to honour last window position when re-opening a document perhaps it's actually apps that do remember it that are bringing something extra to the party.

Whereas I love it! But then I've either closed all windows then closed the app (so nothing re-opens) or the app died when I didn't want it to and "pretty please can I have my windows back?" on relaunch.

Different strokes...

What's the other alternative to closing them, force quitting? If I quit Preview, they close as part of the quit process. But in many other apps, open/close status is irrelevant. Word remembers where its window was, regardless of content—as do many other apps. Preview (and TextEdit) should do the same.

Then again, windows shouldn't pile up on one display on sleep and wake, yet they do. This bug popped into existence some number of years ago (it used to "just work"), and has never been fixed. Sigh.

-rob.

Yes -- if you "Quit" Preview the windows first get closed, because you've turned on "Close windows when quitting..." in System Settings. They re-open top-left of your screen. If I close a window it also re-opens top-left. It's the closing that causes the previous position to be forgotten/ignored.

Totally agree, which is why I regard it to be a bug. Numbers works as we both want it to, so why don't Preview and TextEdit?

Bad Apple...

1 Like

As a satisfied customer, and no more, I can add that the application referred to here is Moom. :wink:

Saving window state is certainly possible. All you have to do is collect the "target", "name" and "frame" of each window in each app and save them to a Dictionary, probably with a structure like:

{
   {application:"Safari", windows:{{target:"Doc1", name:"foo", windowFrame:"x1,y1,w1,h1"},{target:"Doc2", name:"bar", windowFrame:"x2,y2,w2,h2"}}}
   {application:"Preview", windows:{{target:"Doc1", name:"foo", windowFrame:"x1,y1,w1,h1"},{target:"Doc2", name:"bar", windowFrame:"x2,y2,w2,h2"}}}
...
}

"name" and "frame" is easy. The problem will be "target", needed so you can open the correct document/folder/URL, so you'll probably have to do this one window at a time for each application, collecting the target and closing them as you go. For some apps you can use the (new in KM v11) %FrontDocumentPath% token to get the window's target. For browsers you'll have to get the window's current URL, for the Finder it'll be %FinderInsertionLocation%, and so on.

Probably best to divide your apps into two groups:

  1. Those that re-open windows "properly" on re-launch. You just quit the app without closing any windows, re-launch it and the windows come back as they were
  2. Those that don't, and for which you'll have to save state for later

That'll minimise the amount of work you need to do. And if you can name some "problem" apps people might be able to help with routines for them.

Phew I think I might be biting off more than I can chew here. Maybe I'll just save some workspaces I tend to use and have them ready to go instead. I appreciate the explanation though, I'll definitely revisit this when I get a bit more experience under my belt.

It really does depend on the apps you want to use this with. The majority will re-open their open documents and put windows back where they were as long as you:

  1. Don't turn on System Setting's "Close windows when quitting an application"
  2. Don't have your shortcut close windows -- just quit the app, saving docs if asked

Then you only have to

  1. Have your closer shortcut save a list of what apps it closed, so your opener macro has a list to re-open
  2. Find a way to handle the apps that don't play nice

You're right and honestly I can deal with 90% perfect so what if I were to reframe my idea to just saving the apps that were open and then reopening them on a macro later and forgetting about the window placement? I'm quite new to all of this so I'm not sure where my starting point for that would be.

I recommend that you start by indicating which apps you want this macro to work with. Every app potentially has a different way to save files, so there's no way for us to write a macro that handles all possible apps' methods for saving files. Furthermore, what do you want the solution to do if the file you are editing is "Unnamed", do you want the macro to stop and wait for the user to pick a name, or do you want the macro to make up a name, or do you want the macro to discard the file?

Having a list of apps means we don't necessarily have to use AppleScript to solve your problem. I prefer to write solutions in Keyboard Maestro rather than AppleScript.

Take a look at the %ApplicationPath% token, using the Foreground parameter. That will get you a list of the paths to your open applications. Save the list as a global variable so it is available to your "opener" macro.

For the "opener" macro, the "For Each" action is good for working through a list, one item at a time. Since the "%ApplicationPath%" token returned a list with one item on each line, use the "Lines in" option.

The "Open a File, Folder, or Application" action will open whatever is at the path you give it. Pop that action in your "For Each" so that it opens each application in turn.

Bonus points available: If you follow the logic above you'll realise that you are getting your app paths in index order, front to back. If you open them using that list order you'll open them in reverse -- your original frontmost app will now be at the back. So if you want them to be in the same order as when you quit them you'll need to reverse the list.

Have a crack at it, using the above as guidelines, and see how you get on. And as with most things in KM there is no "one true way" to do things, so go off-piste if the mood takes you :wink: