Automatically Restore Last Window Position For Each Process

Post your working scripts, libraries and tools for AHK v1.1 and older
gallaxhar
Posts: 143
Joined: 03 Sep 2014, 06:35

Automatically Restore Last Window Position For Each Process

16 May 2016, 20:54

This is a truly automatic window manager (saves window positions, even snapped ones) with no user input or setup, you don't have to specify what windows should be what size and go where like other with other window managers, it just works, based on the previous window position. Windows OS can't save snapped-window locations. This really bothered me as all day long I was having to re-snap windows.

Image

This script works by getting the current active window information (id and processname). It saves the latest window size/pos to an ini file, and reads from that ini file when new window ID's become active. It's smart enough to not auto-size existing windows from before the script is run, it will only autosize new ID's, and it will only do so one time for that ID.

The one thing you may need to set up is this script to start with windows automatically, if you want to use it for more than just a test run. I have RegistryAdd and RegistryRemove files separate from the script so you can run the script without adding it to the registry, if you like it and want it to start in the background automatically (so it's actually useful to you), download RegistryAdd in the same directory, and run the main script again, it will silently add it to the registry, then delete the RegistryAdd.ahk file, likewise you can download RegistryRemove in the same directory, and run the main script to remove the startup registry entry. Remember to remove RegistryAdd or RegistryRemove after you use either of them, so they continually run (no harm in that other than it may prompt you for UAC elevation to run as admin every time you boot up).

Code: Select all

; Automatically Restore Previous Window Size/Pos

; To make this script run when windows starts, make sure RegistryAdd.ahk is in the same directory as this script, run this script, and it will be added to the registry. Then delete RegistryAdd.ahk
#Include *i RegistryAdd.ahk

; To easily remove the previously added registry entry, make sure RegistryRemove.ahk is in the same directory as this script, run this script, and it will be removed from the registry. Then delete RegistryRemove.ahk
#Include *i RegistryRemove.ahk

#SingleInstance Force
#Persistent
#NoEnv
;#NoTrayIcon
SetWinDelay, 50
Process, Priority, , Normal

MatchList := ""

; Build the MatchList
WinGet, id, list,,, Program Manager
Loop, %id%
{
    this_id := id%A_Index%
	if (MatchList = "")
	MatchList := this_id
	else
	MatchList := MatchList . "," . this_id 
}

; ExclusionList
ExclusionList = ShellExperienceHost.exe,SearchUI.exe

; The main program loop, which manages window positions/sizes and saves their last known configuration to an ini file in the script directory.
Loop,
{
	Sleep, 350
	WinGet, active_id, ID, A
	if active_id not in %MatchList% ; Then this is a new window ID! So, check if it has a configuration saved.
	{
		MatchList := MatchList . "," . active_id ; This window ID is not new anymore!
		WinGet, active_ProcessName, ProcessName, A
		WinGetClass, active_Class, A
		IniRead, savedSizePos, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
		if (savedSizePos != "ERROR" AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd") ; Then a saved configuration exists, size/move the window!
		{
			StringSplit OutputArray, savedSizePos,`,
			if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
			{
				
			}
			else
			{
				WinMove, A,, OutputArray1, OutputArray2, OutputArray3, OutputArray4
			}
		}
		else ; No saved configuration exists, save the current window size/pos as a configuration instead!
		{
			WinGetPos X, Y, Width, Height, A
			WinGet, active_ProcessName, ProcessName, A
			WinGetClass, active_Class, A
			If (X != "" AND Y != "" AND Width != "" AND Height != "" AND Width > 0 AND Height > 0 AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd")
			{
				if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
				{
					
				}
				else if active_ProcessName not in %ExclusionList%
				{
					IniWrite %X%`,%Y%`,%Width%`,%Height%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
				}
			}
		}
	}
	else ; Save/overwrite the active window size and position to a file with a link to the processname, for later use.
	{
		WinGetPos X, Y, Width, Height, A
		WinGet, active_ProcessName, ProcessName, A
		WinGetClass, active_Class, A
		If (X != "" AND Y != "" AND Width != "" AND Height != "" AND Width > 0 AND Height > 0 AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd")
		{
			if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
			{
				
			}
			else if active_ProcessName not in %ExclusionList%
			{
				IniWrite %X%`,%Y%`,%Width%`,%Height%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
			}
		}
	}
}
Return

Code: Select all

if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}
RegWrite, REG_SZ, HKEY_LOCAL_MACHINE, SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run, "%A_ScriptName%", "C:\Program Files\AutoHotkey\AutoHotkey.exe" "%A_ScriptFullPath%"

Code: Select all

if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}
RegDelete, HKEY_LOCAL_MACHINE, SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run, "%A_ScriptName%"
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Automatically Restore Last Window Position For Each Process

02 Feb 2017, 08:16

Simple,elegant,automated and exactly what I was looking. Thanks a f$#!k ton dude,tons better than alternatives that require manual interaction...
Outsourcing Clicks & Presses Since 2004.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Automatically Restore Last Window Position For Each Process

05 Feb 2017, 13:55

I find that it's a bit vulgar in how it tries to resize dialog boxes to their parents windows and so forth so here's a tweaked version that instead of using an exclusion list uses a filter to decide which programs get resized,only name of exe need be defined just as in the prior exclusion list.

Code: Select all

; Automatically Restore Previous Window Size/Pos

; To make this script run when windows starts, make sure RegistryAdd.ahk is in the same directory as this script, run this script, and it will be added to the registry. Then delete RegistryAdd.ahk
; #Include *i RegistryAdd.ahk

; To easily remove the previously added registry entry, make sure RegistryRemove.ahk is in the same directory as this script, run this script, and it will be removed from the registry. Then delete RegistryRemove.ahk
; #Include *i RegistryRemove.ahk

#NoTrayIcon

#SingleInstance Force
#Persistent
#NoEnv
SetWinDelay, 50
Process, Priority, , Normal

MatchList := ""

; Build the MatchList
WinGet, id, list,,, Program Manager
Loop, %id%
{
    this_id := id%A_Index%
	if (MatchList = "")
	MatchList := this_id
	else
	MatchList := MatchList . "," . this_id 
}

;Filter - only programs listed here have their size and position automatically managed,otherwise without this filter wierd malfunctions occur with dialog boxes being resized to their parent program and such...
Filter = xulrunner.exe,firefox.exe,notepad.exe,chrome.exe

; ExclusionList		--- use this as alternative to filter above,i.e this will exclude applications below from automated resize/relocate
ExclusionList = ShellExperienceHost.exe,SearchUI.exe,notepad++.exe,cpuz.exe,gpu-z.0.3.3.exe


; The main program loop, which manages window positions/sizes and saves their last known configuration to an ini file in the script directory.
Loop,
{
	Sleep, 350
	WinGet, active_id, ID, A
	if active_id not in %MatchList% ; Then this is a new window ID! So, check if it has a configuration saved.
	{
		MatchList := MatchList . "," . active_id ; This window ID is not new anymore!
		WinGet, active_ProcessName, ProcessName, A
		WinGetClass, active_Class, A
		IniRead, savedSizePos, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
		if (savedSizePos != "ERROR" AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd") ; Then a saved configuration exists, size/move the window!
		{
			StringSplit OutputArray, savedSizePos,`,
			if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
			{
				
			}
			else
			{
				WinMove, A,, OutputArray1, OutputArray2, OutputArray3, OutputArray4
			}
		}
		else ; No saved configuration exists, save the current window size/pos as a configuration instead!
		{
			WinGetPos X, Y, Width, Height, A
			WinGet, active_ProcessName, ProcessName, A
			WinGetClass, active_Class, A
			If (X != "" AND Y != "" AND Width != "" AND Height != "" AND Width > 0 AND Height > 0 AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd")
			{
				if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
				{
					
				}
				else if active_ProcessName not in %ExclusionList%
				{
				if active_ProcessName in %Filter%																					;TWEAK to only add specific programs to windowConfiguration
					IniWrite %X%`,%Y%`,%Width%`,%Height%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
				}
			}
		}
	}
	else ; Save/overwrite the active window size and position to a file with a link to the processname, for later use.
	{
		WinGetPos X, Y, Width, Height, A
		WinGet, active_ProcessName, ProcessName, A
		WinGetClass, active_Class, A
		If (X != "" AND Y != "" AND Width != "" AND Height != "" AND Width > 0 AND Height > 0 AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd")
		{
			if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
			{
				
			}
			else if active_ProcessName not in %ExclusionList%
			{
			if active_ProcessName in %Filter%																					;TWEAK to only add specific programs to windowConfiguration
				IniWrite %X%`,%Y%`,%Width%`,%Height%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
			}
		}
	}
}
Return

Outsourcing Clicks & Presses Since 2004.
nathan323
Posts: 44
Joined: 31 Jan 2016, 02:53
Location: Australia

Re: Automatically Restore Last Window Position For Each Process

07 Feb 2017, 04:39

Great work gallaxhar :)

brutus, I agree that dialog boxes being snapped are definitely not ideal. But as I have seen when using your version - if I add a program to the filter to be snapped, that program will have its dialog boxes snapped too, so doesn't fix?
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Automatically Restore Last Window Position For Each Process

07 Feb 2017, 12:48

nathan323, yeah I'm lookin to fix that if you don't beat me to it,it's just a matter of filtering the dialog box Window class from it's parent exe's class,my tweak just limits the oddity of morphed dialog boxes ONLY to programs you have specified instead of every dialog box you ever open, I think for now at least that's a worthwhile tweak don't you...
Outsourcing Clicks & Presses Since 2004.
nathan323
Posts: 44
Joined: 31 Jan 2016, 02:53
Location: Australia

Re: Automatically Restore Last Window Position For Each Process

07 Feb 2017, 13:56

Yeah for sure. I had a prog for a while called Display Fusion, did a lot of windows management. Ended up turfing it because too buggy, but they had an option to limit snapping only to windows of a certain starting size (ie. pixels), that might be something we can to play with..
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Automatically Restore Last Window Position For Each Process

08 Feb 2017, 07:25

Here's a full functioning tweak,it only resizes and moves the specified exe and doesn't alter dialog boxes opened from the process,it basically takes the class name of the specified program the first time it detects it,after that the class name for exe's main window won't change and it uses the difference in classname between dialog boxes of a window and the main window it self,i've also filtered the standard windows Open/Save dialog box class #32770.

Code: Select all

; Automatically Restore Previous Window Size/Pos

; To make this script run when windows starts, make sure RegistryAdd.ahk is in the same directory as this script, run this script, and it will be added to the registry. Then delete RegistryAdd.ahk
; #Include *i RegistryAdd.ahk

; To easily remove the previously added registry entry, make sure RegistryRemove.ahk is in the same directory as this script, run this script, and it will be removed from the registry. Then delete RegistryRemove.ahk
; #Include *i RegistryRemove.ahk

#NoTrayIcon

#SingleInstance Force
#Persistent
#NoEnv
SetWinDelay, 50
Process, Priority, , Normal

MatchList := ""

; Build the MatchList
WinGet, id, list,,, Program Manager
Loop, %id%
{
    this_id := id%A_Index%
	if (MatchList = "")
	MatchList := this_id
	else
	MatchList := MatchList . "," . this_id 
}

;Filter - only programs listed here have their size and position automatically managed,otherwise without this filter wierd malfunctions occur with dialog boxes being resized to their parent program and such...
Filter = xulrunner.exe,firefox.exe,notepad.exe,chrome.exe,notepad++.exe

; ExclusionList		--- use this as alternative to filter above,i.e this will exclude applications below from automated resize/relocate
ExclusionList = ShellExperienceHost.exe,SearchUI.exe,notepad++.exe,cpuz.exe,gpu-z.0.3.3.exe


; The main program loop, which manages window positions/sizes and saves their last known configuration to an ini file in the script directory.
Loop,
{
	Sleep, 350
	WinGet, active_id, ID, A
	if active_id not in %MatchList% ; Then this is a new window ID! So, check if it has a configuration saved.
	{
		MatchList := MatchList . "," . active_id ; This window ID is not new anymore!
		WinGet, active_ProcessName, ProcessName, A
		WinGetClass, active_Class, A
		IniRead, savedSizePos, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
		; MsgBox, debug marker 1  %pClass%
		if (savedSizePos != "ERROR" AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd" ) ; Then a saved configuration exists, size/move the window!
		{
			StringSplit OutputArray, savedSizePos,`,
			if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
			{
				
			}
			else
			{
			;Last minute check to make sure Active windows matches specified classname
			WinGetClass, active_Class, A
			IniRead, pClass, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
			if ( pClass = active_Class )
				{
				if ( active_Class != #32770 )	;filter open/save dialog boxes
					{
					WinMove, A,, OutputArray1, OutputArray2, OutputArray3, OutputArray4
					; MsgBox, debug marker 0 %pClass% v %active_Class%
					}
				}
			}
		}
		else ; No saved configuration exists, save the current window size/pos as a configuration instead!
		{
			WinGetPos X, Y, Width, Height, A
			WinGet, active_ProcessName, ProcessName, A
			WinGetClass, active_Class, A
			If (X != "" AND Y != "" AND Width != "" AND Height != "" AND Width > 0 AND Height > 0 AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd")
			{
				if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
				{
					
				}
				else if active_ProcessName not in %ExclusionList%
				{
				if active_ProcessName in %Filter%																					;TWEAK to only add specific programs to windowConfiguration
					{
					WinGetClass, active_Class, A
					IniRead, pClass, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
					;To PREVENT SIZE OF DIALOG BOX BEING ASSIGNED AS SIZE AND LOCATION OF MAIN PROCESS WINDOW
					if ( pClass = active_Class OR pClass = "ERROR" )		;if process has not yet been assigned class value or if active window matches process class write!.
						{
						IniWrite %X%`,%Y%`,%Width%`,%Height%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
						}
					WinGetClass, exePrimaryClass, ahk_exe %active_ProcessName%
					if ( pClass = "ERROR" )		;to make sure the class tied to an exe is only written when the window is first detected,so it's position can be updated but not it's class name.
						{
						; MsgBox, debug marker 2  %ExEpClass%  - %exePrimaryClass%
						IniWrite %exePrimaryClass%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
						}
					}
				}
			}
		}
	}
	else ; Save/overwrite the active window size and position to a file with a link to the processname, for later use.
	{
		WinGetPos X, Y, Width, Height, A
		WinGet, active_ProcessName, ProcessName, A
		WinGetClass, active_Class, A
		If (X != "" AND Y != "" AND Width != "" AND Height != "" AND Width > 0 AND Height > 0 AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd")
		{
			if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
			{
				
			}
			else if active_ProcessName not in %ExclusionList%
			{
			if active_ProcessName in %Filter%																					;TWEAK to only add specific programs to windowConfiguration
				{
				WinGetClass, active_Class, A
				IniRead, pClass, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
				;To PREVENT SIZE OF DIALOG BOX BEING ASSIGNED AS SIZE AND LOCATION OF MAIN PROCESS WINDOW
				if ( pClass = active_Class OR pClass = "ERROR" )		;if process has not yet been assigned class value or if active window matches process class write!.
					{
					IniWrite %X%`,%Y%`,%Width%`,%Height%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
					}
				WinGetClass, exePrimaryClass, ahk_exe %active_ProcessName%
				if ( pClass = "ERROR" )		;to make sure the class tied to an exe is only written when the window is first detected,so it's position can be updated but not it's class name.
					{
					; MsgBox, debug marker 3  %ExEpClass%  - %exePrimaryClass%
					IniWrite %exePrimaryClass%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
					}
				}
			}
		}
	}
}
Return

Outsourcing Clicks & Presses Since 2004.
pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

Re: Automatically Restore Last Window Position For Each Process

29 Aug 2017, 12:23

@brutus_skywalker
Sometimes, even with the same exe and class, they are completely different window. Like the program written by Qt. This script has a very bad effect if used on anki desktop.

It maybe better if the script match the “exe+class+title” triad of the window.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Automatically Restore Last Window Position For Each Process

29 Aug 2017, 14:29

pk23 wrote:@brutus_skywalker
Sometimes, even with the same exe and class, they are completely different window. Like the program written by Qt. This script has a very bad effect if used on anki desktop.

It maybe better if the script match the “exe+class+title” triad of the window.

That would be a hell of a mess though,take a browser for instance where the title of a window changes every page. Though you could easily add title checking specifically for anki provided it have a static title,though instead of a title you might prefer using strings in the main window that only exists in the main window context such as 'file,open,help,...' and other such text that are only present in the main window.
Outsourcing Clicks & Presses Since 2004.
mankvl
Posts: 25
Joined: 23 Sep 2016, 03:58

Re: Automatically Restore Last Window Position For Each Process

02 Mar 2018, 10:17

Would be nice to have title ExclusionList. I wan't main outlook windows to be maximized but new message only half screen. Is this possible ?
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Automatically Restore Last Window Position For Each Process

09 Mar 2018, 09:53

mankvl wrote:Would be nice to have title ExclusionList. I wan't main outlook windows to be maximized but new message only half screen. Is this possible ?
This should allow you to filter out altering the position of a filtered application so long as it's title is also blacklisted against resizing or moving... just add the title you wish to filter in a new line as shown below, there should be a Title_Filter variable to which you can add titles to filter just under the application filter list. ;)

Code: Select all

Title_Filter =		;add every new title on a new line
(
Automatically Restore Last Window Position For Each Process - AutoHotkey Community - Mozilla Firefox
)

Code: Select all


; Automatically Restore Previous Window Size/Pos

; To make this script run when windows starts, make sure RegistryAdd.ahk is in the same directory as this script, run this script, and it will be added to the registry. Then delete RegistryAdd.ahk
; #Include *i RegistryAdd.ahk

; To easily remove the previously added registry entry, make sure RegistryRemove.ahk is in the same directory as this script, run this script, and it will be removed from the registry. Then delete RegistryRemove.ahk
; #Include *i RegistryRemove.ahk

#NoTrayIcon

#SingleInstance Force
#Persistent
#NoEnv
SetWinDelay, 50
Process, Priority, , Normal

MatchList := ""

; Build the MatchList
WinGet, id, list,,, Program Manager
Loop, %id%
{
    this_id := id%A_Index%
	if (MatchList = "")
	MatchList := this_id
	else
	MatchList := MatchList . "," . this_id 
}

;Filter - only programs listed here have their size and position automatically managed,otherwise without this filter wierd malfunctions occur with dialog boxes being resized to their parent program and such...
Filter = xulrunner.exe,firefox.exe,notepad.exe,chrome.exe,notepad++.exe
Title_Filter =		;add every new title on a new line
(
Automatically Restore Last Window Position For Each Process - AutoHotkey Community - Mozilla Firefox
)

; ExclusionList		--- use this as alternative to filter above,i.e this will exclude applications below from automated resize/relocate
ExclusionList = ShellExperienceHost.exe,SearchUI.exe,notepad++.exe,cpuz.exe,gpu-z.0.3.3.exe


; The main program loop, which manages window positions/sizes and saves their last known configuration to an ini file in the script directory.
Loop,
{
Title_Filter_reInit:		;a patch to allow terminating further checks when a filtered title is active within a filtered application

	Sleep, 350
	WinGet, active_id, ID, A
	if active_id not in %MatchList% ; Then this is a new window ID! So, check if it has a configuration saved.
	{
		MatchList := MatchList . "," . active_id ; This window ID is not new anymore!
		WinGet, active_ProcessName, ProcessName, A
		WinGetClass, active_Class, A
		IniRead, savedSizePos, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
		; MsgBox, debug marker 1  %pClass%
		if (savedSizePos != "ERROR" AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd" ) ; Then a saved configuration exists, size/move the window!
		{
			StringSplit OutputArray, savedSizePos,`,
			if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
			{
				
			}
			else
			{
			;Last minute check to make sure Active windows matches specified classname
			WinGetClass, active_Class, A
			IniRead, pClass, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
			if ( pClass = active_Class )
				{
				if ( active_Class != #32770 )	;filter open/save dialog boxes
					{
					WinMove, A,, OutputArray1, OutputArray2, OutputArray3, OutputArray4
					; MsgBox, debug marker 0 %pClass% v %active_Class%
					}
				}
			}
		}
		else ; No saved configuration exists, save the current window size/pos as a configuration instead!
		{
			WinGetPos X, Y, Width, Height, A
			WinGet, active_ProcessName, ProcessName, A
			WinGetClass, active_Class, A
			If (X != "" AND Y != "" AND Width != "" AND Height != "" AND Width > 0 AND Height > 0 AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd")
			{
				if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
				{
					
				}
				else if active_ProcessName not in %ExclusionList%
				{
				if active_ProcessName in %Filter%																					;TWEAK to only add specific programs to windowConfiguration
					{
					;Title_Filter patch
					WinGetActiveTitle, this_activeTitle
					IfInString, Title_Filter, this_activeTitle	;if active window is filtered by title, return to beginning of loop until active window is unfiltered
						Goto, Title_Filter_reInit
											
					
					WinGetClass, active_Class, A
					IniRead, pClass, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
					;To PREVENT SIZE OF DIALOG BOX BEING ASSIGNED AS SIZE AND LOCATION OF MAIN PROCESS WINDOW
					if ( pClass = active_Class OR pClass = "ERROR" )		;if process has not yet been assigned class value or if active window matches process class write!.
						{
						IniWrite %X%`,%Y%`,%Width%`,%Height%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
						}
					WinGetClass, exePrimaryClass, ahk_exe %active_ProcessName%
					if ( pClass = "ERROR" )		;to make sure the class tied to an exe is only written when the window is first detected,so it's position can be updated but not it's class name.
						{
						; MsgBox, debug marker 2  %ExEpClass%  - %exePrimaryClass%
						IniWrite %exePrimaryClass%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
						}
					}
				}
			}
		}
	}
	else ; Save/overwrite the active window size and position to a file with a link to the processname, for later use.
	{
		WinGetPos X, Y, Width, Height, A
		WinGet, active_ProcessName, ProcessName, A
		WinGetClass, active_Class, A
		If (X != "" AND Y != "" AND Width != "" AND Height != "" AND Width > 0 AND Height > 0 AND active_Class != "MultitaskingViewFrame" AND active_class != "Shell_TrayWnd")
		{
			if (active_ProcessName = "explorer.exe" AND active_Class != "CabinetWClass")
			{
				
			}
			else if active_ProcessName not in %ExclusionList%
			{
			if active_ProcessName in %Filter%																					;TWEAK to only add specific programs to windowConfiguration
				{
				;Title_Filter patch
				WinGetActiveTitle, this_activeTitle
				IfInString, Title_Filter, this_activeTitle	;if active window is filtered by title, return to beginning of loop until active window is unfiltered
					Goto, Title_Filter_reInit

				
				WinGetClass, active_Class, A
				IniRead, pClass, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
				;To PREVENT SIZE OF DIALOG BOX BEING ASSIGNED AS SIZE AND LOCATION OF MAIN PROCESS WINDOW
				if ( pClass = active_Class OR pClass = "ERROR" )		;if process has not yet been assigned class value or if active window matches process class write!.
					{
					IniWrite %X%`,%Y%`,%Width%`,%Height%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%
					}
				WinGetClass, exePrimaryClass, ahk_exe %active_ProcessName%
				if ( pClass = "ERROR" )		;to make sure the class tied to an exe is only written when the window is first detected,so it's position can be updated but not it's class name.
					{
					; MsgBox, debug marker 3  %ExEpClass%  - %exePrimaryClass%
					IniWrite %exePrimaryClass%, %A_ScriptDir%\WindowSizePosLog.ini, Process Names, %active_ProcessName%_pClass
					}
				}
			}
		}
	}
}
Return


Outsourcing Clicks & Presses Since 2004.
timblaktu
Posts: 2
Joined: 15 Mar 2018, 15:51
Contact:

Re: Automatically Restore Last Window Position For Each Process

15 Mar 2018, 16:12

I was trying to "uninstall" this ahk script, and have followed @gallaxhar's instructions (run main script with RegistryDelete.ahk colocated) multiple times and every time I reboot it (something) was still trying to run the AutomaticWindowManager.ahk script. I simply moved the script, and then found of course it doesn't run at boot but I do get a dialog saying it cannot find the script.

I searched my entire registry for the string "AutomaticWindowManager" in keys, values, and data, and got zero hits. Then I searched without "match whole string" and found an entry in:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run

that was trying to run AutomaticWindowManager.ahk.

I deleted that, and now my system doesn't try to run it every boot.

Any ideas why RegistryDelete.ahk didn't work?

Anyway, nice script, but I was annoyed at the effect it had on dialogs, so I am using this now:
https://github.com/rwese/DockWin
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Automatically Restore Last Window Position For Each Process

16 Mar 2018, 22:47

timblaktu wrote:I was trying to "uninstall" this ahk script, and have followed @gallaxhar's instructions (run main script with RegistryDelete.ahk colocated) multiple times and every time I reboot it (something) was still trying to run the AutomaticWindowManager.ahk script. I simply moved the script, and then found of course it doesn't run at boot but I do get a dialog saying it cannot find the script.

I searched my entire registry for the string "AutomaticWindowManager" in keys, values, and data, and got zero hits. Then I searched without "match whole string" and found an entry in:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run

that was trying to run AutomaticWindowManager.ahk.

I deleted that, and now my system doesn't try to run it every boot.

Any ideas why RegistryDelete.ahk didn't work?

Anyway, nice script, but I was annoyed at the effect it had on dialogs, so I am using this now:
https://github.com/rwese/DockWin

It needs to be run as admin, tested it & it works just fine, and thanks for linking an alternative, though DockWin is a MANUAL alternative hence why it has no 'dialog box' issues.
Outsourcing Clicks & Presses Since 2004.
robhayes
Posts: 19
Joined: 28 Dec 2015, 15:35

Re: Automatically Restore Last Window Position For Each Process

20 Mar 2018, 13:20

How do I uninstall this script? I don't like it's behaviour with popup menus and folders.

I've tried just deleting the files, but it appears to be called in my startup somewhere, and I can't find it.
gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Automatically Restore Last Window Position For Each Process

20 Mar 2018, 13:55

Well, it seems you have to place the RegDelete file with the right name (see last code box: RegistryDelete.ahk) in the same directory and run the main file again - description is in the first post.
robhayes
Posts: 19
Joined: 28 Dec 2015, 15:35

Re: Automatically Restore Last Window Position For Each Process

21 Mar 2018, 12:54

gregster wrote:Well, it seems you have to place the RegDelete file with the right name (see last code box: RegistryDelete.ahk) in the same directory and run the main file again - description is in the first post.
This did not work for me. Is there a certain reg file I can manually delete? This script completely broke my multi-screen Photoshop and Illustrator setups, making the programs unusable and messing with the DPI.

Thanks in advance for your assistance.
gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Automatically Restore Last Window Position For Each Process

21 Mar 2018, 14:16

It should be the key from the RegistryDelete.ahk - you can see in RegistryAdd.ahk that is was added there...

Code: Select all

HKEY_LOCAL_MACHINE, SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run, "%A_ScriptName%"
Obviously, the actual script name depends on how you named the main script, but the key should be this one.

Or, did you manually add a shortcut to your window's startup folder?
I've tried just deleting the files, but it appears to be called in my startup somewhere, and I can't find it.
How do you know? What is the error message? Did you reboot since then or is the script still running in memory?
robhayes
Posts: 19
Joined: 28 Dec 2015, 15:35

Re: Automatically Restore Last Window Position For Each Process

21 Mar 2018, 15:12

Thank you for pointing out where I could find the registry key that needed to be deleted. Problem solved.
ovnis
Posts: 12
Joined: 25 Mar 2018, 13:40

Re: Automatically Restore Last Window Position For Each Process

25 Mar 2018, 14:09

gallaxhar, Thx for your script.

There is issue with Taskmgr.exe. The window doesn't appear at the right place.
Windows OS can't save snapped-window locations.
I think It's not exactly true. All of non windows application i have tried work pretty well with the save of the snapped-window location. There is an issue with Firefox which doesn't appear exactly at the same place.
The big issue is with some microsoft applications like onenote, Skype, task manager, note, etc.
Last edited by ovnis on 25 Mar 2018, 22:29, edited 2 times in total.
ovnis
Posts: 12
Joined: 25 Mar 2018, 13:40

Re: Automatically Restore Last Window Position For Each Process

25 Mar 2018, 21:48

Hello Brutus,

Thx for your script, but there are problems.

There are issues with Taskmgr.exe and explorer.exe. Windows don't appear at the right place.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 77 guests