将 .docx 与 swriter.exe LibreOffice 关联(使用 PowerShell)

将 .docx 与 swriter.exe LibreOffice 关联(使用 PowerShell)

我想将 .docx 与 OpenOffice 关联

$extension = ".docx"
$executable = "$MyPrograms\LibreOffice\program\swriter.exe"
$fileType = "docxfile"
if (Test-Path $executable) {
    $elevated = @"
        cmd /c "assoc $extension=$fileType"
        cmd /c 'ftype $fileType="$executable" "%1" "%*"'
        New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
        Set-ItemProperty -Path "HKCR:\$fileType" -Name "(Default)" -Value "$fileType file" -ErrorAction Stop
"@
    Start-Process powershell -WindowStyle Hidden -Verb runAs -ArgumentList "-Command", $elevated
    Write-Host "`'$extension`' has been associated with `'$executable`'."
} else {
    Write-Host "`'$executable`' does not exist, so no association for `'$extension`' has been created."

这存在一些问题:

  • 首先,它不能处理带有空格的文件,因此“This File.docx”将显示两个错误对话框“未找到'This'”,然后“未找到'File.docx'”,因此 ftype 语法需要正确处理swriter.exe完整的文件名,我该怎么做?
  • 其次,即使我使用不带空格的名称“MyFile.docx”,也只会打开一个控制台并显示帮助输出,swriter.exe而不会打开应用程序,因此仅传递文件swriter.exe不足以打开程序。我该如何解决这个问题?

答案1

如果自由办公室安装后,您应该已经拥有进程ID LibreOffice.Docx定义如下HKCR

如果出于某种原因你不这样做,

  • HKCR\LibreOffice.Docx\shell\open\command
    • "C:\Program Files\LibreOffice\program\swriter.exe" -o "%1"

纯的电源外壳相等的

从运行行政电源外壳控制台,这将创建相同的键/条目:

$extension  = '.docx'
$executable = 'D:\MyPrograms\LibreOffice\program\swriter.exe'
$fileType   = 'LibreOffice.Docx'
$Hive       = 'HKLM:\SOFTWARE\Classes'

New-Item -Path "$HIve\$extension" -Value $fileType
New-Item -Path "$Hive\$fileType" -Value "$fileType file"
New-Item -Path "$Hive\$fileType\Shell\Open\Command" -Type ExpandString -Force -Value @"
"$executable" -o "%1" 
"@

输出:

>>


    Hive: HKEY_LOCAL_MACHINE\SOFTWARE\Classes


Name                           Property
----                           --------
.docx                          (default) : LibreOffice.Docx
LibreOffice.Docx               (default) : LibreOffice.Docx file


    Hive: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\LibreOffice.Docx\Shell\Open


Name                           Property
----                           --------
Command                        (default) : "D:\MyPrograms\LibreOffice\pro
                               gram\swriter.exe" -o "%1"


PS C:\WINDOWS\system32>

答案2

通过上述@Keith Miller 的回答,我可以使我的原始代码正常运行,以便swriter.exe动态关联扩展,如下所示:

$extension = ".docx"
$executable = "D:\MyPrograms\LibreOffice\program\swriter.exe"
$fileType = "docxfile"
if (Test-Path $executable) {
    $elevated = @"
        cmd /c "assoc $extension=$fileType"
        cmd /c 'ftype $fileType="$executable" -o """%1""" "%*"'
        New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
        Set-ItemProperty -Path "HKCR:\$fileType" -Name "(Default)" -Value "$fileType file" -ErrorAction Stop -Force
"@
    Start-Process powershell -WindowStyle Hidden -Verb runAs -ArgumentList "-Command", $elevated
    Write-Host "`'$extension`' has been associated with `'$executable`'."
} else {
    Write-Host "`'$executable`' does not exist, so no association for `'$extension`' has been created."
}

相关内容