搓了一个,效果如图
代码(需要将 {USERNAME}
替换为自己的用户名)
$pdfApps = @{
SumatraPDF = "C:\Users\{USERNAME}\AppData\Local\SumatraPDF\SumatraPDF.exe"
AdobeReader = "C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
}
$filePath=$args[0]
Add-Type -assembly System.Windows.Forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='Open With Picker'
$main_form.Width = 600
$main_form.Height = 100
$main_form.AutoSize = $true
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Open With"
$Label.Location = New-Object System.Drawing.Point(0,10)
$Label.AutoSize = $true
$main_form.Controls.Add($Label)
$ComboBox = New-Object System.Windows.Forms.ComboBox
$ComboBox.Width = 300
Foreach ($key in $pdfApps.keys)
{
$ComboBox.Items.Add($key);
}
$ComboBox.Location = New-Object System.Drawing.Point(100,10)
$ComboBox.SelectedIndex = 0
$main_form.Controls.Add($ComboBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(400,10)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Open"
$main_form.Controls.Add($Button)
$Button.Add_Click(
{
Start-Process $pdfApps[$ComboBox.SelectedItem] $filePath
$main_form.Close()
}
)
$main_form.ShowDialog()
注册文件关联(需要将 {PSFILEPATH}
替换上端代码存储的 .ps1
文件地址,参考: windows 10 - How can I associate a file type with a powershell script? - Stack Overflow)
Function Set-FileAssociationToPowerShellScript($extension, $pathToScript) {
# first create a filetype
$filetype = cmd /c "assoc $extension 2>NUL"
if ($filetype) {
# Association already exists: override it
$filetype = $filetype.Split('=')[1]
Write-Output "Using filetype $filetype"
}
else {
# Name doesn't exist: create it
# ".log.1" becomes "log1file"
$filetype = "$($extension.Replace('.', ''))file"
Write-Output "Creating filetype $filetype ($extension)"
cmd /c "assoc $extension=$filetype"
}
Write-Output "Associating filetype $filetype ($extension) with $pathToScript.."
cmd /c "ftype $filetype=powershell.exe -File `"$pathToScript`" `"%1`""
}
Set-FileAssociationToPowerShellScript(".pdf", "{PSFILEPATH}")
使用:
- 将系统的 Powershell 脚本权限设定为
unrestricted
: How to enable execution of PowerShell scripts? - Super User - 将第一段代码存储为
ps-open-with.ps1
,第二段代码存储为register.ps1
- 用文本编辑器打开
ps-open-with.ps1
,修改头部的打开方式,key 为显示名,value 为程序路径 - 用文本编辑器打开
register.ps1
,修改底部的{PSFILEPATH}
为ps-open-with.ps1
的完整路径 - 打开一个管理员权限的 Powershell 窗口,运行
register.ps1
。本步骤完成后,右键 PDF 文件,打开方式中应该有Windows Powershell
- 将
Windows Powershell
设定为 PDF 的默认打开方式