From 0fe6e9a31eff304932b1bc8a84390f18dccd95aa Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 5 Aug 2023 17:36:27 +0800 Subject: [PATCH] add fast install script --- README.md | 14 +++++++++++--- install.ps1 | 17 +++++++++++++++++ start.ps1 | 4 ++++ systray.ps1 | 4 ++-- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 install.ps1 create mode 100644 start.ps1 diff --git a/README.md b/README.md index 3314182..23dc249 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,19 @@ # `numlock-indicator` -**\* Important: You'll need to manually use taskbar settings to pin the taskbar icon so it's always visible!** - This powershell script adds a small indicator to the system tray to indicate when the numlock is enabled. +## Install + +1. Run `install.ps1` in an **administrator** powershell terminal. This script will add a task scheduler task to start up the indicator on login. Admin privileges are required to add task scheduler tasks. + +2. **\* ⚠Important: You'll need to manually use taskbar settings to pin the taskbar icon so it's always visible!** + +## Old installation method + +**\* ⚠Important: You'll need to manually use taskbar settings to pin the taskbar icon so it's always visible!** + Copy files to some directory and run `powershell systray.ps1`. You can also run `start.sh` to ensure that one copy of the script is always active. I wrote this because for some reason, my laptop keyboard has no LED indicator for the numlock key (yet they have one for capslock and speakers???) -Feel free to replace `icon.png` with any bitmap icon if you don't like my terrible pixel art. \ No newline at end of file +Feel free to replace `icon.png` with any bitmap icon if you don't like my terrible pixel art. diff --git a/install.ps1 b/install.ps1 new file mode 100644 index 0000000..6bea68d --- /dev/null +++ b/install.ps1 @@ -0,0 +1,17 @@ +$TaskName = "numlock-indicator" +$ScriptPath = Join-Path $PSScriptRoot "start.ps1" +$Action = New-ScheduledTaskAction -Execute 'C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe' -Argument "-WindowStyle Hidden -File `"$ScriptPath`"" +$Trigger = New-ScheduledTaskTrigger -AtLogon +$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries + +if (!$(Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue)) { + Register-ScheduledTask -TaskName $TaskName -Action $Action -Trigger $Trigger -Settings $Settings -User $env:USERNAME + if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) { + Start-ScheduledTask -TaskName $TaskName + Write-Output "Task '$TaskName' created successfully." + } else { + Write-Output "Failed task creation. Run this script in admin mode." + } +} else { + Write-Output "Task '$TaskName' already exists." +} diff --git a/start.ps1 b/start.ps1 new file mode 100644 index 0000000..e9b10ae --- /dev/null +++ b/start.ps1 @@ -0,0 +1,4 @@ +# Ensures one copy of the program is always active. Not necessary for operation. +while (1) { + powershell $(Join-Path $PSScriptRoot 'systray.ps1') +} \ No newline at end of file diff --git a/systray.ps1 b/systray.ps1 index f569eb5..697428a 100644 --- a/systray.ps1 +++ b/systray.ps1 @@ -1,6 +1,6 @@ [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null -$ICON_ON = [System.Drawing.Icon]::FromHandle(([System.Drawing.Bitmap]::FromFile($(Join-Path $pwd '\icon_on.png'))).GetHicon()) # Convert my png (bitmap) to an icon -$ICON_OFF = [System.Drawing.Icon]::FromHandle(([System.Drawing.Bitmap]::FromFile($(Join-Path $pwd '\icon_off.png'))).GetHicon()) +$ICON_ON = [System.Drawing.Icon]::FromHandle(([System.Drawing.Bitmap]::FromFile($(Join-Path $PSScriptRoot '\icon_on.png'))).GetHicon()) # Convert my png (bitmap) to an icon +$ICON_OFF = [System.Drawing.Icon]::FromHandle(([System.Drawing.Bitmap]::FromFile($(Join-Path $PSScriptRoot '\icon_off.png'))).GetHicon()) # $ICON_ON = [System.Drawing.Icon]::ExtractAssociatedIcon() # Feel free to use a system icon # $ICON_OFF = [System.Drawing.Icon]::ExtractAssociatedIcon()