|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Install screen-ruler and register a global keyboard shortcut on Windows. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + Copies the built screen-ruler.exe binary to an install directory (default: |
| 7 | + current directory), creates a Start Menu shortcut with a Ctrl+Shift+R |
| 8 | + hotkey, and adds the binary to the user PATH. |
| 9 | +
|
| 10 | +.PARAMETER InstallDir |
| 11 | + Directory to install the binary into (default: current directory). |
| 12 | +
|
| 13 | +.EXAMPLE |
| 14 | + .\install-windows.ps1 |
| 15 | + .\install-windows.ps1 -InstallDir "$env:LOCALAPPDATA\screen-ruler" |
| 16 | +#> |
| 17 | + |
| 18 | +param( |
| 19 | + [string]$InstallDir = (Get-Location).Path |
| 20 | +) |
| 21 | + |
| 22 | +Set-StrictMode -Version Latest |
| 23 | +$ErrorActionPreference = 'Stop' |
| 24 | + |
| 25 | +$AppName = 'Screen Ruler' |
| 26 | +$AppId = 'screen-ruler' |
| 27 | +$Binary = Join-Path $PSScriptRoot 'dist' 'screen-ruler.exe' |
| 28 | + |
| 29 | +# --- Locate the built binary ------------------------------------------------ |
| 30 | + |
| 31 | +if (-not (Test-Path $Binary)) { |
| 32 | + Write-Error "Binary not found at $Binary.`nBuild it first: pyinstaller screen_ruler.spec" -ErrorAction Continue |
| 33 | + exit 1 |
| 34 | +} |
| 35 | + |
| 36 | +# --- Copy binary to install dir --------------------------------------------- |
| 37 | + |
| 38 | +if (-not (Test-Path $InstallDir)) { |
| 39 | + New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null |
| 40 | +} |
| 41 | +$InstallDir = (Resolve-Path $InstallDir).Path |
| 42 | +$ExecPath = Join-Path $InstallDir "$AppId.exe" |
| 43 | + |
| 44 | +$BinaryFull = (Resolve-Path $Binary).Path |
| 45 | +if ($ExecPath -ne $BinaryFull) { |
| 46 | + Copy-Item -Path $BinaryFull -Destination $ExecPath -Force |
| 47 | + Write-Host "Installed binary -> $ExecPath" |
| 48 | +} else { |
| 49 | + Write-Host "Binary already at $ExecPath" |
| 50 | +} |
| 51 | + |
| 52 | +# --- Add to user PATH (if not already present) ------------------------------ |
| 53 | + |
| 54 | +$userPath = [Environment]::GetEnvironmentVariable('Path', 'User') |
| 55 | + |
| 56 | +# Normalize install directory for comparison |
| 57 | +$normalizedInstallDir = [IO.Path]::GetFullPath($InstallDir).TrimEnd('\').ToLowerInvariant() |
| 58 | + |
| 59 | +# Split existing PATH into entries, normalize, and check for presence |
| 60 | +$pathEntries = @() |
| 61 | +if (-not [string]::IsNullOrEmpty($userPath)) { |
| 62 | + $pathEntries = $userPath -split ';' | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
| 63 | +} |
| 64 | + |
| 65 | +$hasInstallDir = $false |
| 66 | +foreach ($entry in $pathEntries) { |
| 67 | + $normalizedEntry = [IO.Path]::GetFullPath($entry).TrimEnd('\').ToLowerInvariant() |
| 68 | + if ($normalizedEntry -eq $normalizedInstallDir) { |
| 69 | + $hasInstallDir = $true |
| 70 | + break |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +if (-not $hasInstallDir) { |
| 75 | + if ([string]::IsNullOrEmpty($userPath)) { |
| 76 | + $newPath = $InstallDir |
| 77 | + } else { |
| 78 | + $newPath = "$userPath;$InstallDir" |
| 79 | + } |
| 80 | + [Environment]::SetEnvironmentVariable('Path', $newPath, 'User') |
| 81 | + Write-Host "Added $InstallDir to user PATH (restart your terminal to pick it up)." |
| 82 | +} |
| 83 | + |
| 84 | +# --- Create Start Menu shortcut with hotkey --------------------------------- |
| 85 | + |
| 86 | +$startMenu = [Environment]::GetFolderPath('StartMenu') |
| 87 | +$programsDir = Join-Path $startMenu 'Programs' |
| 88 | +$lnkPath = Join-Path $programsDir "$AppName.lnk" |
| 89 | + |
| 90 | +$shell = New-Object -ComObject WScript.Shell |
| 91 | +$shortcut = $shell.CreateShortcut($lnkPath) |
| 92 | +$shortcut.TargetPath = $ExecPath |
| 93 | +$shortcut.Description = 'Measure on-screen UI elements using edge detection' |
| 94 | +$shortcut.WorkingDirectory = $InstallDir |
| 95 | +# Hotkey format: "Modifier+Key" — Ctrl+Shift+R |
| 96 | +$shortcut.Hotkey = 'Ctrl+Shift+R' |
| 97 | +$shortcut.Save() |
| 98 | + |
| 99 | +Write-Host "Created Start Menu shortcut -> $lnkPath" |
| 100 | +Write-Host " Hotkey: Ctrl+Shift+R" |
| 101 | + |
| 102 | +# --- Done ------------------------------------------------------------------- |
| 103 | + |
| 104 | +Write-Host '' |
| 105 | +Write-Host "Done. Press Ctrl+Shift+R to launch $AppName." |
| 106 | +Write-Host 'Note: the shortcut hotkey requires the .lnk to stay in the Start Menu folder.' |
0 commit comments