Files
OBS-Replay-Buffer-for-IT-Su…/scripts/Show-ReplayTray.ps1

95 lines
3.7 KiB
PowerShell

#Requires -Version 5.1
<#
.SYNOPSIS
System tray icon that lets users trigger an OBS replay buffer save.
Right-click the tray icon to access Save Replay or Exit.
.NOTES
This script blocks (runs a WinForms message loop) — launch it via Start-Process.
Start-OBSReplayBuffer.ps1 handles launching this automatically at logon.
#>
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# --- Start transcript logging ---
try {
$config = Import-PowerShellDataFile -Path (Join-Path $PSScriptRoot '..\config.psd1')
$logDir = [System.Environment]::ExpandEnvironmentVariables($config.LogPath)
New-Item -ItemType Directory -Path $logDir -Force -ErrorAction SilentlyContinue | Out-Null
Start-Transcript -Path (Join-Path $logDir 'OBSReplayBuffer-Tray.log') -Append -ErrorAction Stop
} catch {
# Log path unavailable — continue without transcript
}
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [INFO] Tray icon started."
function Show-Notification {
param([string]$Title, [string]$Body)
try {
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$xml = [Windows.Data.Xml.Dom.XmlDocument]::new()
$escaped = [System.Security.SecurityElement]::Escape($Body)
$xml.LoadXml("<toast><visual><binding template='ToastGeneric'><text>$Title</text><text>$escaped</text></binding></visual></toast>")
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('IT Screen Recorder').Show($toast)
}
catch {
[System.Windows.Forms.MessageBox]::Show($Body, $Title, [System.Windows.Forms.MessageBoxButtons]::OK) | Out-Null
}
}
$saveScript = Join-Path $PSScriptRoot 'Invoke-ReplaySave.ps1'
# --- Tray icon ---
$tray = New-Object System.Windows.Forms.NotifyIcon
$tray.Icon = [System.Drawing.SystemIcons]::Shield
$tray.Text = 'IT Screen Recorder'
$tray.Visible = $true
# --- Context menu ---
$menu = New-Object System.Windows.Forms.ContextMenuStrip
$saveItem = New-Object System.Windows.Forms.ToolStripMenuItem('Save Replay')
$sep = New-Object System.Windows.Forms.ToolStripSeparator
$exitItem = New-Object System.Windows.Forms.ToolStripMenuItem('Exit')
$saveItem.Add_Click({
$saveItem.Enabled = $false
$saveItem.Text = 'Saving...'
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [INFO] Save replay requested."
$result = & powershell.exe -ExecutionPolicy Bypass -NonInteractive -File $saveScript
if ($result -eq $true) {
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [INFO] Replay saved successfully."
Show-Notification -Title 'IT Screen Recorder' -Body 'Replay saved.'
}
else {
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [ERROR] Replay save failed."
Show-Notification -Title 'IT Screen Recorder' -Body 'Could not save replay. Please contact the helpdesk.'
}
$saveItem.Text = 'Save Replay'
$saveItem.Enabled = $true
})
$exitItem.Add_Click({
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [INFO] Tray icon exited by user."
$tray.Visible = $false
[System.Windows.Forms.Application]::Exit()
})
$menu.Items.Add($saveItem) | Out-Null
$menu.Items.Add($sep) | Out-Null
$menu.Items.Add($exitItem) | Out-Null
$tray.ContextMenuStrip = $menu
# --- Message loop (blocks until Exit is chosen) ---
[System.Windows.Forms.Application]::Run()
$tray.Dispose()
Stop-Transcript -ErrorAction SilentlyContinue