# Adds the "LX-D02" printer to Windows, starts print_server.py at every logon and adds # "Print on LX-D02" for pictures to the Explorer menu and to "Send to". # Needs the POS-58 driver (stock driver for 58 mm receipt printers), see README. # # Run in PowerShell as administrator: .\install.ps1 (printer found by name) # .\install.ps1 AA:BB:CC:DD:EE:FF # Remove: .\install.ps1 -Uninstall param([string]$Mac = "", [switch]$Uninstall) $ErrorActionPreference = "Stop" $Name = "LX-D02" $Port = "LX-D02-9100" $Task = "LX-D02 print server" $Root = Split-Path $PSScriptRoot $Menu = "HKCU:\Software\Classes\SystemFileAssociations\image\shell\LXD02" $SendTo = Join-Path ([Environment]::GetFolderPath("SendTo")) "LX-D02.lnk" # Clean up first, so running it again simply reinstalls Stop-ScheduledTask -TaskName $Task -ErrorAction SilentlyContinue Unregister-ScheduledTask -TaskName $Task -Confirm:$false -ErrorAction SilentlyContinue Get-CimInstance Win32_Process -Filter "Name like 'python%.exe'" | Where-Object { $_.CommandLine -like "*print_server.py*" } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force } Remove-Printer -Name $Name -ErrorAction SilentlyContinue Remove-PrinterPort -Name $Port -ErrorAction SilentlyContinue Remove-Item $Menu -Recurse -ErrorAction SilentlyContinue Remove-Item $SendTo -ErrorAction SilentlyContinue if ($Uninstall) { "Removed."; return } $driver = Get-PrinterDriver | Where-Object Name -like "POS-58*" | Select-Object -First 1 if (-not $driver) { throw "POS-58 driver is not installed. See README: 'Driver'." } $py = (Get-Command python.exe).Source $pyw = (Get-Command pythonw.exe).Source # The printer: the POS-58 driver sends ESC/POS to our server instead of a COM port Add-PrinterPort -Name $Port -PrinterHostAddress 127.0.0.1 -PortNumber 9100 Add-Printer -Name $Name -DriverName $driver.Name -PortName $Port # The server, started at logon $action = New-ScheduledTaskAction -Execute $pyw -Argument "`"$PSScriptRoot\print_server.py`" $Mac" -WorkingDirectory $PSScriptRoot $trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME $settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0 -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries Register-ScheduledTask -TaskName $Task -Action $action -Trigger $trigger -Settings $settings -Force | Out-Null Start-ScheduledTask -TaskName $Task # Pictures: right click -> "Print on LX-D02", or several at once via "Send to" $cmd = "`"$py`" `"$Root\lxd02.py`"" New-Item -Path "$Menu\command" -Force | Out-Null Set-ItemProperty -Path $Menu -Name "(default)" -Value "Print on LX-D02" Set-ItemProperty -Path $Menu -Name "Icon" -Value "$env:SystemRoot\System32\imageres.dll,46" Set-ItemProperty -Path $Menu -Name "MultiSelectModel" -Value "Single" # one Bluetooth connection at a time Set-ItemProperty -Path "$Menu\command" -Name "(default)" -Value "$cmd `"%1`"" $link = (New-Object -ComObject WScript.Shell).CreateShortcut($SendTo) $link.TargetPath = $py $link.Arguments = "`"$Root\lxd02.py`"" $link.WorkingDirectory = $Root $link.IconLocation = "$env:SystemRoot\System32\imageres.dll,46" $link.Save() "Done: printer '$Name' (driver '$($driver.Name)'), print server, Explorer menu." "Log: $PSScriptRoot\print_server.log"