From 4721e386a0c209f9c056d86161eac33da59e66e9 Mon Sep 17 00:00:00 2001 From: Lago Date: Fri, 27 Mar 2026 16:32:06 +0100 Subject: [PATCH] Enhance autostart setup with a one-command installer and improve script logic for KVM switch launch --- README.md | 30 ++++++++++++++++-------------- install_autostart.ps1 | 28 ++++++++++++++++++++++++++++ start_kvm_switch.cmd | 22 +++++++++++++++++++++- start_kvm_switch_background.vbs | 8 ++++++-- 4 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 install_autostart.ps1 diff --git a/README.md b/README.md index cdeb988..2712b42 100644 --- a/README.md +++ b/README.md @@ -56,22 +56,24 @@ uv run monitorcontrol_main.py ## Autostart -### Easiest option: Startup folder +### Recommended: one-command installer + +From the project folder, run: + +```powershell +powershell -ExecutionPolicy Bypass -File .\install_autostart.ps1 +``` + +This creates or updates a Startup shortcut for the current Windows user and points it to this folder's `start_kvm_switch_background.vbs`. +No hardcoded `C:\Users\...` path is required, so it works across tower/laptop deployments. + +### Manual option: Startup folder 1. Press `Win + R` -2. Run: +2. Run `shell:startup` +3. Create a shortcut to this repo's `start_kvm_switch_background.vbs` -```text -shell:startup -``` - -3. Create a shortcut in that folder pointing to: - -```text -C:\Users\LagoWorkStation\OneDrive\Documentos\BE-terna\Internal - KVM Switch\start_kvm_switch_background.vbs -``` - -This starts the app at logon in the background (no persistent console window). +The launcher resolves its own folder dynamically, so the project can live in different user directories. ### Cleaner option: Task Scheduler @@ -86,7 +88,7 @@ wscript.exe Arguments: ```text -"C:\Users\LagoWorkStation\OneDrive\Documentos\BE-terna\Internal - KVM Switch\start_kvm_switch_background.vbs" +"\\start_kvm_switch_background.vbs" ``` Set the trigger to `At log on`. Optional: enable `Hidden` in task settings. diff --git a/install_autostart.ps1 b/install_autostart.ps1 new file mode 100644 index 0000000..e0be226 --- /dev/null +++ b/install_autostart.ps1 @@ -0,0 +1,28 @@ +param( + [string]$ShortcutName = "Internal KVM Switch.lnk" +) + +$ErrorActionPreference = "Stop" + +$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path +$launcher = Join-Path $scriptDir "start_kvm_switch_background.vbs" + +if (-not (Test-Path -LiteralPath $launcher)) { + throw "Launcher not found: $launcher" +} + +$startupFolder = [Environment]::GetFolderPath("Startup") +$shortcutPath = Join-Path $startupFolder $ShortcutName + +$wscript = New-Object -ComObject WScript.Shell +$shortcut = $wscript.CreateShortcut($shortcutPath) +$shortcut.TargetPath = "$env:WINDIR\System32\wscript.exe" +$shortcut.Arguments = """$launcher""" +$shortcut.WorkingDirectory = $scriptDir +$shortcut.IconLocation = "$env:WINDIR\System32\shell32.dll,220" +$shortcut.Save() + +Write-Host "Autostart shortcut created/updated:" +Write-Host " $shortcutPath" +Write-Host "Target launcher:" +Write-Host " $launcher" diff --git a/start_kvm_switch.cmd b/start_kvm_switch.cmd index d9087f3..73cabd9 100644 --- a/start_kvm_switch.cmd +++ b/start_kvm_switch.cmd @@ -1,3 +1,23 @@ @echo off +setlocal cd /d "%~dp0" -uv run kvm-switch + +where uv >nul 2>&1 +if %errorlevel%==0 ( + uv run kvm-switch + exit /b %errorlevel% +) + +if exist ".venv\Scripts\kvm-switch.exe" ( + call ".venv\Scripts\kvm-switch.exe" + exit /b %errorlevel% +) + +if exist ".venv\Scripts\python.exe" ( + call ".venv\Scripts\python.exe" -m main + exit /b %errorlevel% +) + +echo [ERROR] Could not start KVM Switch. +echo [ERROR] Neither "uv" nor ".venv" runtime was found. +exit /b 1 diff --git a/start_kvm_switch_background.vbs b/start_kvm_switch_background.vbs index 3c95851..82802cb 100644 --- a/start_kvm_switch_background.vbs +++ b/start_kvm_switch_background.vbs @@ -1,12 +1,16 @@ Option Explicit Dim shell +Dim fso Dim projectPath +Dim launcherPath Dim command Set shell = CreateObject("WScript.Shell") -projectPath = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) -command = "cmd.exe /c cd /d """ & projectPath & """ && uv run kvm-switch" +Set fso = CreateObject("Scripting.FileSystemObject") +projectPath = fso.GetParentFolderName(WScript.ScriptFullName) +launcherPath = fso.BuildPath(projectPath, "start_kvm_switch.cmd") +command = "cmd.exe /c """ & launcherPath & """" ' Run hidden and do not wait. shell.Run command, 0, False