如何修改win11下文件的默认打开应用?

传统情况下,打开一个文件(如.pdf),系统会调用默认的pdf软件打开此文件。如果想要更改,需要右键→打开方式→选择其他应用,在下方可以找到“设为默认打开应用”。
现在我的需求是:在不选定文件的情况下更改.pdf的默认打开应用。并且操作要足够便捷,因为不同的应用场景,我需要切换不同的阅读器打开。

之所以有这个需求是因为我的一些pdf文件是以绝对路径的形式嵌在笔记中,点击这个链接,系统自动调用之前默认的应用,不给我选择的机会,也就没办法更改打开方式。因此咨询有没有解决方案。

谢谢大家。

感觉可以换个思路。考虑找个程序,能够以命令行参数方式传入你需要打开的 PDF 文件路径,然后会自动跳出一个界面让你选择对当前文件使用的 PDF 阅读器,最后把这个程序作为你的 PDF 关联应用就好了。Android 下似乎有类似的 App,不过不太确定 Windows 下是否有… 但是写起来应该相对简单,AHK 应该就够了。

Linux 下的一个类似应用:

什么应用啊

你应该想办法让你的笔记软件支持选择打开方式

1 个赞

搓了一个,效果如图
screenshots

代码(需要将 {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}")

使用:

  1. 将系统的 Powershell 脚本权限设定为 unrestrictedHow to enable execution of PowerShell scripts? - Super User
  2. 将第一段代码存储为 ps-open-with.ps1,第二段代码存储为 register.ps1
  3. 用文本编辑器打开 ps-open-with.ps1,修改头部的打开方式,key 为显示名,value 为程序路径
  4. 用文本编辑器打开 register.ps1,修改底部的 {PSFILEPATH}ps-open-with.ps1 的完整路径
  5. 打开一个管理员权限的 Powershell 窗口,运行 register.ps1。本步骤完成后,右键 PDF 文件,打开方式中应该有 Windows Powershell
  6. Windows Powershell 设定为 PDF 的默认打开方式
4 个赞

obsidian,obsidian本身可以通过插件打开pdf,但是我不想让我的pdf文件作为obsidian的附件,因为这个pdf是另一个软件的附件,我在另一个软件进行pdf修改或标注后,在obsidian上用绝对路径打开,就可以浏览这些新标记,但如果把它添加为obsidian附件就不能同步更新了。

感谢大佬回复,我昨天按步骤执行,右键PDF文件没有找到 Windows Powershell。下面是我的设置截图,麻烦大佬抽空看下哪里出的问题。




第四步运行脚本

右键选择打开方式
Snipaste_2022-05-27_09-56-15
Snipaste_2022-05-27_09-56-36
Snipaste_2022-05-27_09-57-23
Snipaste_2022-05-27_09-57-55
Snipaste_2022-05-27_09-58-14

还望大佬帮我看看是哪里没有设置好,非常感谢。

这个软件搭配ahk可以非常轻松地实现~
五分钟搞定~

打开pdf

这层楼是教程,先占为敬,得写一下小会

=====

  1. 先下载textify,设个快捷键,比如ctrl+右键,这样只要在文件链接处按下ctrl+右键,就会把链接提取出来了。
    image

  2. 接下来点击更多设置。
    config这里,把自动copy选中

[config]
check_for_updates=0
auto_copy_selection=1
hide_tray_icon=0
  1. 添加按钮
    以这样的形式,在最下面添加几个按钮
    比如用adobe dc打开的按钮,第一行是按钮序号,第二行是按钮名,第三行选个图标,第四行是按钮对应的ahk代码。
[web_button_2]
name=dc
icon=icons\dc.ico
command=C:\Users\Administrator\AppData\Local\Programs\Textify\dcopen.ahk
  1. ahk代码
    拷贝这段代码为ahk文件。命名为dcopen.ahk。
#SingleInstance force
GoSub, getParams
Return

getParams:
  If 0 > 0
  {
     Loop, %0% ; for each parameter
     {
        param := clipboard
	http:= StrReplace(param,"%5C", "/")
	http:= StrReplace(http,"%20", " ")
	http:= StrReplace(http,"file:///", "")
	Run,C:\miniapp\app\SumatraPDF.exe "%http%"
     }
  }

重复第三第四步骤,有几个pdf打开器,就添加几个按钮,新建几个ahk就好了。

只需要改个图标,ahk名称和exe地址,半分钟搞定一个,之后都可以一直用了。

而且也不止pdf,任何文件类型,网址拉之类,都可以通过按钮指定对应的软件或浏览器来打开。

另外,这个软件每次开机都会跳出来设置界面。
在快捷方式后面加个 -hidewnd 就ok了

这个方法看上去很nice,但是操作没有成功,似乎ahk脚本调用不成功,截图如下

更改textify 更多设置
Snipaste_2022-05-27_13-30-51

脚本存放位置

Snipaste_2022-05-27_13-32-12

PDF软件位置
Snipaste_2022-05-27_13-29-23

obsidian地址栏点击2 后没有反应
Snipaste_2022-05-27_13-36-24

补充脚本截图

奥奥,忘了有的文件名会有空格的情况
换成这段代码

#SingleInstance force
GoSub, getParams
Return

getParams:
  If 0 > 0
  {
     Loop, %0% ; for each parameter
     {
        param := clipboard
	http:= StrReplace(param,"%5C", "/")
	http:= StrReplace(http,"%20", " ")
	http:= StrReplace(http,"file:///", "")
	Run,C:\miniapp\app\SumatraPDF.exe "%http%"
     }
  }

再有什么问题的话,代码里多加几行类似
http:= StrReplace(http,“%xx”, “x”)
的代码。
把提取出来的被转化成 %某某 的变回它的原来字符就好了。

不过除了空格和/ 应该也没其他这种奇怪问题了。

试了下,这么奇怪的名称也没有问题了
GIF 2022-5-27 14-05-39

PowerShell 脚本的运行方式可能有问题。先 cd 到存储目录下,再直接 ./register.ps1 执行。需要管理员权限。

我替换了一些ahk参数,可是点击设置的textify第二个选项,没有反应,理论上不应该启动SumatraPDF这个软件吗

谢谢回复,按照此方法启动脚本,但是右键打开方式里还没没找到powershell


脚本文件我也没找到问题

你换成这段代码

另外,这是textify版本

image

这是所有的设置

; Mouse and keyboard key numbers can be found here:
; https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

[mouse]
key=2
ctrl=1
alt=0
shift=1

[keyboard]
key=84
ctrl=1
alt=1
shift=1

[config]
check_for_updates=0
auto_copy_selection=1
hide_tray_icon=0

; The following option causes empty Unicode symbol to be removed
; and various space symbols to be replaced with a regular space.
unicode_spaces_to_ascii=0

[exclude]
;1=photoshop.exe
;2=notepad.exe

; Web buttons can be used with the keyboard shortcuts: Alt+1, Alt+2, etc.
; More web buttons can be added, and existing buttons can be removed.
; If desired, all of the web buttons can be removed.
;
; A command can be a URL or a program. It can contain the following:
; %s - will be replaced by the selected text, URL encoded.
; %cs - will be replaced by the selected text, with quote characters stripped.
; %rs - will be replaced by the selected text as is (use carefully).
;
; A command can start with popup-web!, followed by a URL.
; In this case, the page will be opened in a browser popup (browser auto-detected).
; You can be more specific by starting a command with one of the following:
; popup-chrome!, popup-edge!, popup-firefox!, popup-ie-control!.

;[web_button_1]
;name=Google Translate
;icon=icons\google_translate.ico
;command=popup-web!https://translate.google.com/m/translate#auto/en/%s
;width=400
;height=700
[web_button_1]
name=opera
icon=icons\duckduckgo.ico
command=D:\autohotkey\自写项目\Textify\open.ahk %s
width=1200
height=600

[web_button_2]
name=dc
icon=icons\dc.ico
command=C:\Users\Administrator\AppData\Local\Programs\Textify\dcopen.ahk %s

另外,我安装的autototkey这个版本

非常感谢,失败原因是我不小心删了这个代码 。
:bouquet: