我想创建一个带有参数的 PDF 文档的快捷方式文件,这样当我在资源管理器上双击它时,它将在我的默认 PDF 查看器中的指定页面上打开 pdf 文件。
我可以在资源管理器中创建这样的快捷方式,方法是右键单击 PDF 文件,然后:
PDFXEdit /a page=87 "C:\temp\sample.pdf"
我想要一个 PowerShell 方法来执行上述操作,我尝试过:
set-shorcut -Path 'c:\temp\sample.pdf' -Arguments 'PDFXEdit /a page=87' | get-shortcut
WindowStyle : 1
Path : C:\temp\sample.lnk
IconPath : ,0
Hotkey :
Arguments : PDFXEdit /a page=87
Target : C:\temp\sample.pdf
快捷方式确实已创建,但是当我查看快捷方式文件属性对话框的目标字段时,其值为:
"C:\temp\sample.pdf" PDFXEdit /a page=87
并且执行它确实使用默认的 pdf 程序打开文件,但不是在第 87 页。
看起来预期值是
PDFXEdit /a page=87 "C:\temp\sample.pdf"
但我似乎无法在PowerShell中执行此操作。
对于 .lnk
文件来说,是否不可能尝试将文件路径和一些任意参数传递给当前的“打开方式...”pdf 程序??
任何帮助将不胜感激!
set-shortcut
的代码是:
function Set-Shortcut{
param(
[Parameter(ValueFromPipelineByPropertyName)]
[String]$Path = $pwd,
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[String]
$Target,
[Parameter(ValueFromPipelineByPropertyName)]
[String]
$Arguments,
[Parameter(ValueFromPipelineByPropertyName)]
[String]
$IconPath
)
process{
if (-not(get-item -path $path).PSIsContainer){Write-Error 'Only directories allowed as destination Path' -ErrorAction Stop}
$path = join-path -path $path -ChildPath ((get-item -path $target).BaseName + '.lnk')
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($Path)
$Shortcut.TargetPath = $Target
Switch ($PSBoundParameters.keys){
'Arguments' {$Shortcut.Arguments = $Arguments}
'IconPath' {$Shortcut.IconLocation = $IconPath}
}
$Shortcut.Save()
return (Get-Item $Path)
}
}
当您手动创建快捷方式时,您可以对其进行编辑以完全更改快捷方式所指向的内容。具体来说,您将其更改为指向 PDFXEdit 应用程序,参数包括文件路径和页码。因此,当您在 PowerShell 中创建快捷方式时,您仍然希望复制这些更改。
set-shorcut -Path PDFXEdit -Arguments '/a page=87 "c:\temp\sample.pdf"' | get-shortcut