-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwinlayout_apply.ps1
More file actions
71 lines (63 loc) · 2.06 KB
/
winlayout_apply.ps1
File metadata and controls
71 lines (63 loc) · 2.06 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
#Reads json file into a configuration object
Function ParseConfig($configpath)
{
$jsontmp = Get-Content $configpath | Out-String | ConvertFrom-Json
$global:config = {$jsontmp}.Invoke()
}
#Modifies a given process' window to a given size and position using the Windows API
Function MoveWindow($processjson)
{
$process = $processjson | ConvertFrom-Json
$handle = (Get-Process -Name $process.processname -ErrorAction SilentlyContinue).MainWindowHandle
#Select the process instance with a valid PID
foreach ($h in $handle)
{
if ($h -ne 0)
{
$takeHandle = $h
}
}
#Skip if the process is not currently running
if($takeHandle) {
Write-Host "Moving window"$process.processname"-"$handle"..."
$return = [Window]::MoveWindow($takeHandle, $process.x, $process.y, $process.width, $process.height, $True)
Write-Host "Success?" $return "`n"
}
else {
Write-Host "Process "$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
#Reading file with configured size and position values
#Use `winlayout_record.ps1` to record these values
$configpath = $env:USERPROFILE + "\windowlayout.config"
#Modify windows' size and position as spec'd in the configuration file
ParseConfig($configpath)
$global:config | ForEach-Object { MoveWindow($_) }