Restore app windows to their original Spaces

When re-opening an app, all of its windows end up on the first Space, regardless of their original placement:

Original layout: one app has two windows open, each in a separate Space

+-----[ Space 1 ]-----+  +-----[ Space 2 ]-----+
|                     |  |                     |
|  [Chrome window 1]  |  |  [Chrome window 2]  |
|                     |  |                     |
|                     |  |                     |
+---------------------+  +---------------------+

Incorrect layout after app restart: both windows are in the first Space

+-----[ Space 1 ]-----+  +-----[ Space 2 ]-----+
|                     |  |                     |
|  [Chrome window 1]  |  |                     |
|  [Chrome window 2]  |  |                     |
|                     |  |                     |
+---------------------+  +---------------------+

Is there a setting or an app that would make it possible to have apps restore their windows in the Spaces they were originally located before the app was quit?


Solution 1:

edit 2: This still doesn't COMPLETELY work for me as well: some windows are restored and some are not. The new bug id in Chromium is 1012034, and it still needs your votes!

edit: It was implemented in the latest development version (Chrome 79)!

Though, by default, in compliance with Apple's UI requirements, this feature still won't restore the workspaces if you exited Chrome with Cmd-Q. Quoting sdy, the feature author:

For folks who want Chrome to always restore windows to their original spaces, you can set that by running this command in a terminal window (for Canary, use com.google.Chrome.canary):

defaults write com.google.Chrome NSWindowRestoresWorkspaceAtLaunch -bool YES

To go back to the default behavior, run:

defaults delete com.google.Chrome NSWindowRestoresWorkspaceAtLaunch

Original answer: There is a bug #74812 in Chromium. Vote for it!

Same question on Superuser.

Solution 2:

I don't have a complete answer, but today, I started a decent stab at this issue using applescript. The following Applescript does roughly half the job. It reports all the windows of every application open on every desktop. The next step is to record all this in a file and then implement a script to redistribute the windows after reboot:

--This applescript reports a list of application windows present on each desktop
--This is only a sample script intended to eventually be used to restore all application windows to their pre-reboot desktops

tell application "System Events"

    set windows_string to ""
    set numDesktops to (first paragraph of (do shell script "strings ~/Library/Preferences/com.apple.spaces.plist | grep -c ^\\\\$")) + 1
    --the following tcsh command can determine the number of desktops:
    -- @ x = ( `strings ~/Library/Preferences/com.apple.spaces.plist | grep -c '^\$'` + 1 ); echo $x

    -- switch to the first desktop:
    repeat with aDesktop from 1 to numDesktops
        key code 123 using {control down}
    end repeat

    repeat with aDesktop from 1 to numDesktops

        set windows_string to windows_string & return & return & "Desktop " & (aDesktop as string) & return
        delay 1
        get (the name of every application process whose class of windows contains window)

        repeat with P in the result

            set windows_string to windows_string & return & return & P & return

            get (every window of process (contents of P) whose value of attribute "AXMinimized" is false)

            repeat with W in the result

                set window_name to ((name of W) as string)

                --Stickies window names can be multi-line, so this trims from the first hard return onward
                set better_window_name to (my replacePattern:"[\\n].*" inString:window_name usingThis:"")
                if window_name is not equal to "" then
                    try
                        set windows_string to windows_string & better_window_name & return
                    on error
                        set windows_string to windows_string & "couldn't get window name" & return
                    end try
                end if

            end repeat

        end repeat

        --switch to the next desktop
        key code 124 using {control down}

    end repeat

    display dialog "List of windows on this desktop: " & return & return & windows_string

end tell

--Call like this: set res to my replacePattern:"\\s+" inString:"1 subtratcing-these: -2 3 4" usingThis:"-"
use framework "Foundation"
use scripting additions

on replacePattern:thePattern inString:theString usingThis:theTemplate
    set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
    set theResult to theRegEx's stringByReplacingMatchesInString:theString options:0 range:{location:0, |length|:length of theString} withTemplate:theTemplate
    return theResult as text
end replacePattern:inString:usingThis:

Here's an example of what's the the resulting dialog I get:

List of windows on this desktop: 



Desktop 1


iTunes
MiniPlayer


FluidApp
Found 626 tickets


Stickies
useful commands
System Config Notes
Special characters
Profile where a python scri…
XEMacs Tricks
Terminal Tips


Messages
Messages (3 unread)


FluidApp
Found 626 tickets


Slack
Slack - Princeton NPLC


FluidApp
Found 626 tickets


Desktop 2


iTunes
MiniPlayer


FluidApp
Found 626 tickets


Stickies
DAI


Messages
Messages (3 unread)


FluidApp
Found 626 tickets


Slack
Slack - Princeton NPLC


FluidApp
Found 626 tickets


Desktop 3


iTunes
MiniPlayer


Finder
Searching “Scripts”
Searching “Scripts”
Searching “Scripts”


FluidApp
Found 626 tickets


Stickies
RPST
Issues to resolve with RPST…


Messages
Messages (3 unread)


FluidApp
Found 626 tickets


Terminal
Terminal — -csh


TextEdit
Untitled 35.txt
Untitled 34.txt
Untitled 27.txt
Untitled 15.txt


Slack
Slack - Princeton NPLC


Safari
AppleScript: Essential Sub-Routines
Using Applescript to Execute a Complicated Keystroke - Stack Overflow
How to restore windows to their original desktops after reboot? - Ask Different


Script Editor
Untitled.scpt
Untitled 4.scpt
Untitled 3.scpt
paste_file_contents.scpt
Untitled 2.scpt


FluidApp
Found 626 tickets


Automator
Subtract.workflow (Quick Action)


Desktop 4


iTunes
MiniPlayer


FluidApp
Found 626 tickets


Stickies
GALAXY


Messages
Messages (3 unread)


FluidApp
Found 626 tickets


Terminal
Terminal — -bash
???c7??? 8yc`?h=??'?]b?c??k?k????Ԫ??m??d+ — -bash


Slack
Slack - Princeton NPLC


FluidApp
Found 626 tickets


Desktop 5


iTunes
MiniPlayer


FluidApp
Found 626 tickets


Stickies
TREEVIEW
When you start to work on a…
TreeView stats, legend, & d…
DATE OF SUBMISSION: 


Messages
Messages (3 unread)


FluidApp
Found 626 tickets


Slack
Slack - Princeton NPLC


FluidApp
Found 626 tickets


Desktop 6


iTunes
MiniPlayer


FluidApp
Found 626 tickets


Stickies
ALIZZI


Messages
Messages (3 unread)


FluidApp
Found 626 tickets


Slack
Slack - Princeton NPLC


FluidApp
Found 626 tickets


Desktop 7


iTunes
MiniPlayer


FluidApp
Found 626 tickets


Stickies
GARCIA


Messages
Messages (3 unread)


FluidApp
Found 626 tickets


Slack
Slack - Princeton NPLC


FluidApp
Found 626 tickets


Desktop 8


iTunes
MiniPlayer


FluidApp
Found 626 tickets


Stickies
EMILIA


Messages
Messages (3 unread)


FluidApp
Found 626 tickets


Slack
Slack - Princeton NPLC


FluidApp
Found 626 tickets