Windows has virtual desktops which is great. I use separate desktops for different things I am working on.But if I close a program (or reboot), then when I open the program again the program appear on the desktop I am currently on. So I manually have to move the program back.
I guess for chrome it is even more complicated as it is the same program with different windows. It would be nice to have the windows stay on the desktop they were before closing.
Similar to these issues with the difference of my question asking to remember windows position as opposed to setting it explicitly.
- How to open program on a specified virtual desktop on Windows 10?
- Windows 10: Assigning Application to Specific Desktop
- Starting Programs in a Specific Virtual Desktop
- How do I open a program directly to a specific virtual desktop with powershell
- Chrome instances move to same virtual desktop after sleep or hibernate in Windows 10
- How to open a program on particular desktop?
Solution:
This can be done with the below AutoHotKey script,which accepts two shortcut keys:
- F11 : Will write the titles of all current windows and theirvirtual desktop numbers to a text file specified in the parameter
FILENAME
that is found at the beginning of the script. - F12 : Reads the text file and moves all the windows it can findby their title to the specified virtual desktop.
Copy the following text into an .ahk
file, possibly changing “FILENAME”,”F11″ and “F12”.Double-click the file to start it executing. It will create a green “H”icon in the traybar that you can right-click and select Exit
to stop.If you always want this script to execute, copy it to the user Startup folder atC:Users<user name>AppDataRoamingMicrosoftWindowsStart MenuProgramsStartup
.
This script requires to download from the Github projectwindows-desktop-switcherthe DLLVirtualDesktopAccessor.dlland storing it in the same folder as the AutoHotKey script.It might not work for Windows 10 versions below 1809.
The script itself is below and is programmed for only one monitor.It does not create the virtual desktops, so they should be createdbefore running it.It worked for me, but should be tested.As it’s using undocumented features, it mightstop working some time in the future.
DetectHiddenWindows OffSetTitleMatchMode, 2FILENAME = C:Temppossaves.txthVirtualDesktopAccessor := DllCall("LoadLibrary", Str, "VirtualDesktopAccessor.dll", "Ptr") MoveWindowToDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "MoveWindowToDesktopNumber", "Ptr")IsWindowOnDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "IsWindowOnDesktopNumber", "Ptr")global numdesktops := GetDesktopsNumber()F11:: ; Write list of "desktop@title"EnumAddress := RegisterCallback("EnumWindowsProc", "Fast")global numwins := 0global file := FileOpen(FILENAME, "w")DllCall("EnumWindows", "Ptr", EnumAddress, "Ptr", 0)file.Close()returnF12:: ; Read list and executeglobal resultLoop, Read, %FILENAME%{ word_array := StrSplit(A_LoopReadLine, "@",, 2) ; Omits periods. hwnd := WinExist(word_array[2]) if (hwnd) DllCall(MoveWindowToDesktopNumberProc, UInt, hwnd, UInt, word_array[1] - 1)}returnGetDesktopsNumber(){ RegRead, cur, HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerSessionInfo1VirtualDesktops, CurrentVirtualDesktop RegRead, alldesktops, HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionExplorerVirtualDesktops, VirtualDesktopIDs return floor(strlen(alldesktops) / strlen(cur))}EnumWindowsProc(hwnd, lParam){ WinGetTitle, title, ahk_id %hwnd% if title { desktopnum := GetHWNDDesktopNumber(hwnd) if (desktopnum >= 0) { numwins := numwins + 1 line = % desktopnum "@" title "`r`n" file.Write(line) } } return true}GetHWNDDesktopNumber(hwnd){ global numdesktops, IsWindowOnDesktopNumberProc Loop, %numdesktops% { ix := A_Index - 1 windowIsOnDesktop := DllCall(IsWindowOnDesktopNumberProc, UInt, hwnd, UInt, ix) if (windowIsOnDesktop == 1) return A_Index } return -1}