-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall-windows.ps1
More file actions
112 lines (89 loc) · 3.65 KB
/
Copy pathinstall-windows.ps1
File metadata and controls
112 lines (89 loc) · 3.65 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
<#
.SYNOPSIS
Install screen-ruler and register a global keyboard shortcut on Windows.
.DESCRIPTION
Copies the built screen-ruler.exe binary to an install directory (default:
current directory), creates a Start Menu shortcut with a Ctrl+Shift+R
hotkey, and adds the binary to the user PATH.
.PARAMETER InstallDir
Directory to install the binary into (default: current directory).
.EXAMPLE
.\install-windows.ps1
.\install-windows.ps1 -InstallDir "$env:LOCALAPPDATA\screen-ruler"
#>
param(
[string]$InstallDir = (Get-Location).Path
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$AppName = 'Screen Ruler'
$AppId = 'screen-ruler'
$Binary = Join-Path $PSScriptRoot 'dist' 'screen-ruler.exe'
# --- Locate the built binary ------------------------------------------------
if (-not (Test-Path $Binary)) {
Write-Error "Binary not found at $Binary.`nBuild it first: pyinstaller screen_ruler.spec" -ErrorAction Continue
exit 1
}
# --- Copy binary to install dir ---------------------------------------------
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$InstallDir = (Resolve-Path $InstallDir).Path
$ExecPath = Join-Path $InstallDir "$AppId.exe"
$BinaryFull = (Resolve-Path $Binary).Path
if ($ExecPath -ne $BinaryFull) {
Copy-Item -Path $BinaryFull -Destination $ExecPath -Force
Write-Host "Installed binary -> $ExecPath"
} else {
Write-Host "Binary already at $ExecPath"
}
# --- Add to user PATH (if not already present) ------------------------------
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
# Normalize install directory for comparison
$normalizedInstallDir = [IO.Path]::GetFullPath($InstallDir).TrimEnd('\').ToLowerInvariant()
# Split existing PATH into entries, normalize, and check for presence
$pathEntries = @()
if (-not [string]::IsNullOrEmpty($userPath)) {
$pathEntries = $userPath -split ';' | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
}
$hasInstallDir = $false
foreach ($entry in $pathEntries) {
$expandedEntry = [Environment]::ExpandEnvironmentVariables($entry)
try {
$normalizedEntry = [IO.Path]::GetFullPath($expandedEntry).TrimEnd('\').ToLowerInvariant()
} catch {
continue
}
if ($normalizedEntry -eq $normalizedInstallDir) {
$hasInstallDir = $true
break
}
}
if (-not $hasInstallDir) {
if ([string]::IsNullOrEmpty($userPath)) {
$newPath = $InstallDir
} else {
$newPath = $userPath.TrimEnd(';') + ';' + $InstallDir
}
[Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
Write-Host "Added $InstallDir to user PATH (restart your terminal to pick it up)."
}
# --- Create Start Menu shortcut with hotkey ---------------------------------
$startMenu = [Environment]::GetFolderPath('StartMenu')
$programsDir = Join-Path $startMenu 'Programs'
New-Item -ItemType Directory -Path $programsDir -Force | Out-Null
$lnkPath = Join-Path $programsDir "$AppName.lnk"
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($lnkPath)
$shortcut.TargetPath = $ExecPath
$shortcut.Description = 'Measure on-screen UI elements using edge detection'
$shortcut.WorkingDirectory = $InstallDir
# Hotkey format: "Modifier+Key" — Ctrl+Shift+R
$shortcut.Hotkey = 'Ctrl+Shift+R'
$shortcut.Save()
Write-Host "Created Start Menu shortcut -> $lnkPath"
Write-Host " Hotkey: Ctrl+Shift+R"
# --- Done -------------------------------------------------------------------
Write-Host ''
Write-Host "Done. Press Ctrl+Shift+R to launch $AppName."
Write-Host 'Note: the shortcut hotkey requires the .lnk to stay in the Start Menu folder.'