143 lines
5.1 KiB
PowerShell
143 lines
5.1 KiB
PowerShell
Add-Type -AssemblyName System.Windows.Forms
|
|
|
|
$SetFTAPath = "C:\ProgramData\SetUserFTA\SetUserFTA.exe"
|
|
|
|
# Prepare form early so we can use it as a message box owner
|
|
$form = New-Object System.Windows.Forms.Form
|
|
$form.Text = "Set Default PDF Application"
|
|
$form.Size = New-Object System.Drawing.Size(400, 250)
|
|
$form.StartPosition = "CenterScreen"
|
|
$form.TopMost = $true
|
|
|
|
# Check if SetUserFTA.exe exists
|
|
if (-not (Test-Path $SetFTAPath)) {
|
|
[System.Windows.Forms.MessageBox]::Show($form, "SetUserFTA.exe not found.`nExpected at:`n$SetFTAPath", "Missing Dependency", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
$form.Close()
|
|
return
|
|
}
|
|
|
|
# Known ProgIDs and Friendly Names
|
|
$friendlyMap = @{
|
|
"AcroExch.Document.DC" = "Adobe Acrobat"
|
|
"AcroExch.Document" = "Adobe Reader"
|
|
"NitroPDF.Pro.v13" = "Nitro PDF Pro 13"
|
|
"NitroPDF.Pro.v14" = "Nitro PDF Pro 14"
|
|
"Bluebeam.Revu.PDF" = "Bluebeam Revu"
|
|
"RedlinePDFfile" = "Bluebeam Revu PDF"
|
|
"MSEdgePDF" = "Microsoft Edge"
|
|
}
|
|
|
|
# Check if a ProgID exists in the registry
|
|
function Is-ProgIDPresent($progId) {
|
|
$paths = @(
|
|
"HKCR:\$progId",
|
|
"HKCU:\Software\Classes\$progId",
|
|
"HKLM:\Software\Classes\$progId"
|
|
)
|
|
foreach ($path in $paths) {
|
|
if (Test-Path $path) { return $true }
|
|
}
|
|
return $false
|
|
}
|
|
|
|
# Get only installed PDF handlers
|
|
function Get-InstalledPDFProgIDs {
|
|
$installed = @()
|
|
foreach ($progId in $friendlyMap.Keys) {
|
|
if (Is-ProgIDPresent $progId) {
|
|
$installed += $progId
|
|
}
|
|
}
|
|
return $installed
|
|
}
|
|
|
|
# Get current default handler
|
|
function Get-CurrentPDFHandler {
|
|
try {
|
|
$userSID = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value
|
|
$keyPath = "Registry::HKEY_USERS\$userSID\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.pdf\UserChoice"
|
|
if (Test-Path $keyPath) {
|
|
return (Get-ItemProperty -Path $keyPath -Name ProgId).ProgId
|
|
} else {
|
|
return "Unknown"
|
|
}
|
|
} catch {
|
|
return "Error"
|
|
}
|
|
}
|
|
|
|
# Set PDF default using SetUserFTA with a mapping file
|
|
function Set-PDFDefaultHandler($progId) {
|
|
try {
|
|
$mapFile = Join-Path $env:TEMP "SetFTA.txt"
|
|
"pdf $progId" | Out-File -FilePath $mapFile -Encoding ASCII -Force
|
|
Start-Process -FilePath $SetFTAPath -ArgumentList "`"$mapFile`"" -Wait -NoNewWindow
|
|
} catch {
|
|
[System.Windows.Forms.MessageBox]::Show($form, "Failed to apply default:`n$_", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
}
|
|
}
|
|
|
|
# Add controls
|
|
$labelCurrent = New-Object System.Windows.Forms.Label
|
|
$labelCurrent.Text = "Current Default Handler:"
|
|
$labelCurrent.Location = New-Object System.Drawing.Point(20, 20)
|
|
$labelCurrent.AutoSize = $true
|
|
$form.Controls.Add($labelCurrent)
|
|
|
|
$labelCurrentValue = New-Object System.Windows.Forms.Label
|
|
$labelCurrentValue.Text = Get-CurrentPDFHandler
|
|
$labelCurrentValue.Location = New-Object System.Drawing.Point(180, 20)
|
|
$labelCurrentValue.AutoSize = $true
|
|
$form.Controls.Add($labelCurrentValue)
|
|
|
|
$comboBox = New-Object System.Windows.Forms.ComboBox
|
|
$comboBox.Location = New-Object System.Drawing.Point(20, 60)
|
|
$comboBox.Size = New-Object System.Drawing.Size(340, 20)
|
|
$comboBox.DropDownStyle = "DropDownList"
|
|
$form.Controls.Add($comboBox)
|
|
|
|
# Build dropdown from installed handlers
|
|
$displayToProgID = @{}
|
|
$installedProgIDs = Get-InstalledPDFProgIDs
|
|
$currentHandler = Get-CurrentPDFHandler
|
|
|
|
foreach ($progId in $installedProgIDs) {
|
|
$friendlyName = $friendlyMap[$progId]
|
|
if (-not $friendlyName) { $friendlyName = $progId }
|
|
$displayToProgID[$friendlyName] = $progId
|
|
$comboBox.Items.Add($friendlyName)
|
|
|
|
if ($progId -eq $currentHandler) {
|
|
$comboBox.SelectedItem = $friendlyName
|
|
}
|
|
}
|
|
|
|
# Apply Button
|
|
$buttonApply = New-Object System.Windows.Forms.Button
|
|
$buttonApply.Text = "Set as Default"
|
|
$buttonApply.Location = New-Object System.Drawing.Point(20, 100)
|
|
$buttonApply.Size = New-Object System.Drawing.Size(120, 30)
|
|
$buttonApply.Add_Click({
|
|
if ($comboBox.SelectedItem -and $comboBox.Enabled) {
|
|
$selectedDisplayName = $comboBox.SelectedItem
|
|
$selectedProgId = $displayToProgID[$selectedDisplayName]
|
|
Set-PDFDefaultHandler $selectedProgId
|
|
[System.Windows.Forms.MessageBox]::Show($form, "Default PDF app set to: $selectedDisplayName", "Success", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
|
|
$labelCurrentValue.Text = Get-CurrentPDFHandler
|
|
} else {
|
|
[System.Windows.Forms.MessageBox]::Show($form, "Please select an application.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
}
|
|
})
|
|
$form.Controls.Add($buttonApply)
|
|
|
|
# Close Button
|
|
$buttonClose = New-Object System.Windows.Forms.Button
|
|
$buttonClose.Text = "Close"
|
|
$buttonClose.Location = New-Object System.Drawing.Point(240, 100)
|
|
$buttonClose.Size = New-Object System.Drawing.Size(120, 30)
|
|
$buttonClose.Add_Click({ $form.Close() })
|
|
$form.Controls.Add($buttonClose)
|
|
|
|
# Show GUI
|
|
[void]$form.ShowDialog()
|