-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwinlayout_record.ps1
More file actions
82 lines (72 loc) · 2.6 KB
/
winlayout_record.ps1
File metadata and controls
82 lines (72 loc) · 2.6 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
#Saves configuration object data to a json file
Function SaveConfig($configpath)
{
$global:config | ConvertTo-Json | Out-File $configpath
}
#Queries the Windows API for a given window's size and position
#Saves it to a configuration object
Function GetWindowData($processName)
{
$handle = (Get-Process -Name $processName -ErrorAction SilentlyContinue).MainWindowHandle
#Gets the process instance with a handle number
foreach ($h in $handle)
{
if ($h -ne 0)
{
$takeHandle = $h
}
}
#Skip if the process is not currently running
if($takeHandle) {
Write-Host "Recording data for "$processName"-"$handle"..."
# This is to get the current process state
$Rectangle = New-Object RECT
$Return = [Window]::GetWindowRect($takeHandle,[ref]$Rectangle)
$windowObject = New-Object -TypeName psobject
$windowObject | Add-Member -MemberType NoteProperty -Name processname -Value $processName
$windowObject | Add-Member -MemberType NoteProperty -Name x -Value $Rectangle.Left
$windowObject | Add-Member -MemberType NoteProperty -Name y -Value $Rectangle.Top
$windowObject | Add-Member -MemberType NoteProperty -Name width -Value ($Rectangle.Right - $Rectangle.Left)
$windowObject | Add-Member -MemberType NoteProperty -Name height -Value ($Rectangle.Bottom - $Rectangle.Top)
$json = $windowObject | ConvertTo-Json
$global:config.Add($json)
}
else {
Write-Host "Process "$processName "not running"
}
}
# =======
# MAIN
# =======
#Window object type definition to interact with the Windows API
Try{
[void][Window]
}
Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Window {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
}
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
"@
}
$global:config = New-Object System.Collections.ArrayList
$configpath = $env:USERPROFILE + "\windowlayout.config"
#Creating an array with the process windows to record size and position
#To read a list of currently running processes use `Get-Process`
$processes = "Teams","Outlook","WindowsTerminal"
#Read and record window sizes and positions
$processes | ForEach-Object { GetWindowData($_) }
SaveConfig($configpath)