forked from cuete/WindowMover
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinlayout_record.ps1
More file actions
287 lines (234 loc) · 10.1 KB
/
winlayout_record.ps1
File metadata and controls
287 lines (234 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<#
.SYNOPSIS
Records current window positions and saves them to a JSON configuration file.
.DESCRIPTION
Captures the position, size, and monitor of visible top-level windows.
Supports filtering by process name, deduplication, and multiple output formats.
Use this script to create layout configs that can be restored later with winlayout_apply.ps1.
.PARAMETER Path
Destination JSON file path. Default: "$env:USERPROFILE\windowlayout.config"
.PARAMETER CsvPath
Optional CSV output path written alongside JSON.
.PARAMETER ProcessName
Only include specific process names (e.g., "chrome","notepad","Code").
.PARAMETER ExcludeProcessName
Exclude specific process names.
.PARAMETER Append
Append captured entries to existing JSON file (merges arrays).
.PARAMETER IncludeMinimized
Include minimized windows in the capture.
.PARAMETER Deduplicate
Keep only the largest (by area) window per group.
.PARAMETER DedupBy
Deduplication grouping method:
- process: largest per process
- process+title: largest per process+title
- monitor: largest per monitor
- process+monitor: largest per process on each monitor
- process+title+monitor: largest per process+title on each monitor
Default: process
.PARAMETER DedupMonitorBy
For per-monitor dedup, group by 'device' (default) or 'index'.
.EXAMPLE
.\winlayout_record.ps1
Records all visible windows to default location.
.EXAMPLE
.\winlayout_record.ps1 -ProcessName "chrome","code" -Path "dev-layout.json"
Records only Chrome and VS Code windows.
.EXAMPLE
.\winlayout_record.ps1 -Deduplicate -DedupBy "process+monitor"
Records one window per process per monitor (useful for multi-monitor setups).
.NOTES
Version: 2.1
Requires: Windows 8+ with PowerShell 5.1+
Exit Codes:
0 - Success
1 - General error
#>
[CmdletBinding()]
param(
[string]$Path = (Join-Path $env:USERPROFILE 'windowlayout.config'),
[string]$CsvPath,
[string[]]$ProcessName,
[string[]]$ExcludeProcessName,
[switch]$Append,
[switch]$IncludeMinimized,
[switch]$Deduplicate,
[ValidateSet('process','process+title','monitor','process+monitor','process+title+monitor')]
[string]$DedupBy = 'process',
[ValidateSet('device','index')]
[string]$DedupMonitorBy = 'device'
)
#region Win32 Interop
$null = @'
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections.Generic;
public class Win32Window {
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
public static List<IntPtr> EnumAllWindows() {
var list = new List<IntPtr>();
EnumWindows((hWnd, lParam) => { list.Add(hWnd); return true; }, IntPtr.Zero);
return list;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left; public int Top; public int Right; public int Bottom;
}
'@ | Add-Type -PassThru -ErrorAction SilentlyContinue
Add-Type -AssemblyName System.Windows.Forms | Out-Null
#endregion
#region Helper Functions
function Get-MonitorInfo {
param([IntPtr]$Handle)
$screen = [System.Windows.Forms.Screen]::FromHandle($Handle)
$index = [array]::IndexOf([System.Windows.Forms.Screen]::AllScreens, $screen) + 1
return @{
Device = $screen.DeviceName
Index = $index
Bounds = $screen.Bounds
WorkingArea = $screen.WorkingArea
}
}
function Get-WindowList {
param([string[]]$Include, [string[]]$Exclude, [switch]$Minimized)
$includeSet = if ($Include) { $Include | ForEach-Object { $_.ToLower() } } else { @() }
$excludeSet = if ($Exclude) { $Exclude | ForEach-Object { $_.ToLower() } } else { @() }
$results = [System.Collections.Generic.List[object]]::new()
$handles = [Win32Window]::EnumAllWindows()
foreach ($hWnd in $handles) {
if (-not [Win32Window]::IsWindowVisible($hWnd)) { continue }
$len = [Win32Window]::GetWindowTextLength($hWnd)
if ($len -le 0) { continue }
$titleBuilder = [System.Text.StringBuilder]::new($len + 1)
[void][Win32Window]::GetWindowText($hWnd, $titleBuilder, $titleBuilder.Capacity)
$title = $titleBuilder.ToString()
if ([string]::IsNullOrWhiteSpace($title)) { continue }
$processId = 0
[void][Win32Window]::GetWindowThreadProcessId($hWnd, [ref]$processId)
if ($processId -eq 0) { continue }
try { $proc = Get-Process -Id $processId -ErrorAction Stop } catch { continue }
$procName = $proc.ProcessName
if ($includeSet.Count -gt 0 -and $includeSet -notcontains $procName.ToLower()) { continue }
if ($excludeSet.Count -gt 0 -and $excludeSet -contains $procName.ToLower()) { continue }
$rect = New-Object RECT
if (-not [Win32Window]::GetWindowRect($hWnd, [ref]$rect)) { continue }
$width = $rect.Right - $rect.Left
$height = $rect.Bottom - $rect.Top
if ($width -le 0 -or $height -le 0) { continue }
if (-not $Minimized -and [Win32Window]::IsIconic($hWnd)) { continue }
$mon = Get-MonitorInfo -Handle $hWnd
$results.Add([PSCustomObject]@{
ProcessName = $procName
Title = $title
X = [int]$rect.Left
Y = [int]$rect.Top
Width = [int]$width
Height = [int]$height
Area = [int]($width * $height)
MonitorIndex = [int]$mon.Index
MonitorDevice = [string]$mon.Device
}) | Out-Null
}
return $results | Sort-Object MonitorIndex, ProcessName, Title
}
function Remove-Duplicates {
param([System.Collections.IEnumerable]$Entries, [string]$By, [string]$MonitorKey)
if (-not $Entries) { return @() }
$monitorSelector = if ($MonitorKey -eq 'index') {
{ $_.MonitorIndex }
} else {
{ $_.MonitorDevice }
}
switch ($By) {
'process' {
$groups = $Entries | Group-Object ProcessName
}
'process+title' {
$groups = $Entries | Group-Object { "{0}||{1}" -f $_.ProcessName, $_.Title }
}
'monitor' {
$groups = $Entries | Group-Object (& $monitorSelector.InvokeReturnAsIs($_))
}
'process+monitor' {
$groups = $Entries | Group-Object {
"{0}||{1}" -f $_.ProcessName, (& $monitorSelector.InvokeReturnAsIs($_))
}
}
'process+title+monitor' {
$groups = $Entries | Group-Object {
"{0}||{1}||{2}" -f $_.ProcessName, $_.Title, (& $monitorSelector.InvokeReturnAsIs($_))
}
}
}
return $groups | ForEach-Object {
$_.Group | Sort-Object Area -Descending | Select-Object -First 1
}
}
function Export-CsvData {
param([System.Collections.IEnumerable]$Entries, [string]$CsvPath)
if (-not $CsvPath) { return }
try {
$Entries | Select-Object ProcessName, Title, X, Y, Width, Height, MonitorIndex, MonitorDevice |
Export-Csv -Path $CsvPath -NoTypeInformation -Encoding UTF8
Write-Host "CSV exported: $CsvPath" -ForegroundColor Green
} catch {
Write-Warning "CSV export failed: $($_.Exception.Message)"
}
}
#endregion
#region Main
$ErrorActionPreference = 'Stop'
try {
Write-Host "Recording window layout..." -ForegroundColor Cyan
$entries = Get-WindowList -Include $ProcessName -Exclude $ExcludeProcessName -Minimized:$IncludeMinimized
if (-not $entries -or $entries.Count -eq 0) {
Write-Warning "No matching windows found."
exit 0
}
if ($Deduplicate) {
$beforeCount = $entries.Count
$entries = Remove-Duplicates -Entries $entries -By $DedupBy -MonitorKey $DedupMonitorBy
Write-Host "Deduplicated: $beforeCount -> $($entries.Count) entries ($DedupBy, monitor=$DedupMonitorBy)" -ForegroundColor Cyan
}
# Remove helper property before output
$output = $entries | Select-Object ProcessName, Title, X, Y, Width, Height, MonitorIndex, MonitorDevice
Export-CsvData -Entries $output -CsvPath $CsvPath
if ($Append -and (Test-Path -LiteralPath $Path)) {
try {
$existing = Get-Content -LiteralPath $Path -Raw | ConvertFrom-Json
$merged = if ($existing -is [System.Collections.IEnumerable]) {
@($existing) + @($output)
} else {
@($existing) + @($output)
}
$merged | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath $Path -Encoding UTF8
Write-Host "Appended $($output.Count) entries to $Path" -ForegroundColor Green
} catch {
Write-Warning "Append failed, creating new file: $($_.Exception.Message)"
$output | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath $Path -Encoding UTF8
Write-Host "Created new file: $Path" -ForegroundColor Green
}
} else {
if (Test-Path -LiteralPath $Path) {
$backup = "$Path.bak-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
Copy-Item -LiteralPath $Path -Destination $backup -ErrorAction SilentlyContinue
Write-Host "Backup created: $backup" -ForegroundColor DarkGray
}
$output | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath $Path -Encoding UTF8
Write-Host "Recorded $($output.Count) window(s) to $Path" -ForegroundColor Green
}
} catch {
Write-Error $_
exit 1
}
#endregion