-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.ps1
More file actions
73 lines (63 loc) · 3.1 KB
/
Copy pathswitch.ps1
File metadata and controls
73 lines (63 loc) · 3.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
# Toggle primary monitor and audio device
# Useful for when you need to switch between a monitor at a desk and a TV from the couch
# Load configuration
$configPath = Join-Path $PSScriptRoot "config.json"
if (!(Test-Path $configPath)) {
Write-Error "Configuration file not found: $configPath"
exit 1
}
try {
$config = Get-Content $configPath | ConvertFrom-Json
} catch {
Write-Error "Failed to parse configuration file: $($_.Exception.Message)"
exit 1
}
# Expand environment variables in paths
$logPath = [System.Environment]::ExpandEnvironmentVariables($config.paths.monitorLog)
# $audioLogPath = [System.Environment]::ExpandEnvironmentVariables($config.paths.audioLog)
# Maximize all windows to ensure there are no minimize windows or they won't move due to how Windows handles the restore/normal state
Start-Process "nircmd.exe" -ArgumentList "win max alltopnodesktop" -Wait
# Monitor Toggle using NirCmd
if (!(Test-Path $logPath)) { Set-Content $logPath $config.monitors.primary }
$lastMonitor = Get-Content $logPath
$nextMonitor = if ($lastMonitor -eq $config.monitors.primary.ToString()) { $config.monitors.secondary } else { $config.monitors.primary }
Set-Content $logPath $nextMonitor
Start-Process "MultiMonitorTool.exe" -ArgumentList "/SetPrimary $nextMonitor" -Wait
if ($config.notifications.enabled) {
Write-Host ($config.notifications.monitorMessage -f $nextMonitor)
}
# Move windows to primary monitor using MultiMonitorTool
Start-Process "MultiMonitorTool.exe" -ArgumentList "/MoveWindow Primary All" -Wait
# Maximize all windows again to ensure they fill the screen in case of different resolutions or scaling
Start-Process "nircmd.exe" -ArgumentList "win max alltopnodesktop" -Wait
# Per user window customizations
# Allows you to position specific windows at custom locations/sizes
# When switching to the TV, move all windows
# When switch to the desk, move some windows to other monitors
if ($config.windowCustomizations.enabled -and $nextMonitor -eq $config.monitors.primary) {
foreach ($customization in $config.windowCustomizations.rules) {
# Move specified apps to specified monitors
MultiMonitorTool.exe /MoveWindow $customization.monitor $customization.findMethod $customization.findValue -Wait
# Script block to run additional customization command defined in config, e.g. resizing windows
if ($customization.command) {
$sb = [ScriptBlock]::Create($customization.command)
& $sb
}
}
}
# Redraw taskbar if enabled in config
if ($config.redrawTaskbar.enabled) {
& ./redraw_taskbar.ps1
}
# Toggle between two audio devices
if ($nextMonitor -eq $config.monitors.primary) {
Start-Process "nircmd.exe" -ArgumentList "setdefaultsounddevice `"$($config.audio.device1)`" 1"
if ($config.notifications.enabled) {
Write-Host ($config.notifications.audioMessage -f $config.audio.device1)
}
} else {
Start-Process "nircmd.exe" -ArgumentList "setdefaultsounddevice `"$($config.audio.device2)`" 1"
if ($config.notifications.enabled) {
Write-Host ($config.notifications.audioMessage -f $config.audio.device2)
}
}