我需要通过命令行批量更改与某些文件关联的应用程序。我不会更改某个文件扩展名的文件关联,实际上,我希望使用不同于该文件类型的默认关联的应用程序来打开特定文件。我不想更改此文件扩展名的默认应用程序,只想更改我所针对的特定文件。
现在,我在 Finder 中选择了所有想要更改的文件,然后按 option+command+i 打开获取多个文件的信息,然后更改这些文件的应用程序。这有点烦人。
答案1
此信息存储在文件的资源分叉(维基百科)这些资源分支公开为扩展属性(维基百科):
$ ls -l@ somefile.txt
-rw-r--r-- 1 danielbeck staff 0 18 Mär 19:00 somefile.txt
# setting non-default application using Finder
$ ls -l@ somefile.txt
-rw-r--r--@ 1 danielbeck staff 0 18 Mär 19:01 somefile.txt
com.apple.ResourceFork 1338
从头开始编辑可能相当痛苦——大量的二进制数据:
$ xattr -p com.apple.ResourceFork somefile.txt
00 00 01 00 00 00 05 08 00 00 04 08 00 00 00 32
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 04 04 00 00 00 32 2F 55 73 65 72 73 2F 64
61 6E 69 65 6C 62 65 63 6B 2F 41 70 70 6C 69 63
61 74 69 6F 6E 73 2F 53 75 62 6C 69 6D 65 20 54
65 78 74 20 32 2E 61 70 70 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 01 00 00 00 05 08
00 00 04 08 00 00 00 32 0C 00 00 00 C0 05 00 00
00 1C 00 32 00 00 75 73 72 6F 00 00 00 0A 00 00
FF FF 00 00 00 00 19 00 00 00
但你可以将这些资源分支视为文件。例如:
$ open somefile.txt # opens in Sublime 2
$ cp somefile.txt/..namedfork/rsrc openInSublime2rsrc
$ open otherfile.txt # opens in TextEdit
$ cp openInSublime2rsrc otherfile.txt/..namedfork/rsrc
$ open otherfile.txt # opens in Sublime 2
/..namedfork/rsrc
是资源分叉如何向 POSIX 应用程序公开(即您在终端中执行的所有操作)。
因此,您只需从现有资源分支(在此示例中openInSublime2rsrc
)创建一个“模板”文件,然后将其复制到其他文件中。
答案2
从上面 Daniel Beck 的回答中可以看出,这些信息确实存储在文件的资源分支中。Apple 在开发者工具中提供了两个实用程序,分别称为“Rez”和“DeRez”,它们允许您操作资源分支。特别是,您可以使用 Rez 将资源分支推送到文件中。
要从命令行更改单个文件的关联,首先创建一个正确类型的文件,然后手动将其关联更改为您希望在其中打开它的应用程序。这将在文件中创建资源分支 - 如果不这样做,则没有信息可以复制出来。然后,使用 DeRez 提取资源分支,如下所示(假设文件 foo.txt,并且您要使用的应用程序是 Firefox.app)。
DeRez foo.txt > foo.r
这将创建一个名为 foo.r 的文件,它是反编译的资源分支的文本文件。它看起来会像这样:
data 'usro' (0) {
$"0000 001A 2F41 7070 6C69 6361 7469 6F6E" /* ..../Application */
$"732F 4669 7265 666F 782E 6170 7000 0000" /* s/Firefox.app... */
$"0000 0000 0000 0000 0000 0000 0000 0000" /* ................ */
如果您想从头开始创建,可以编辑此内容,但必须获取格式确切地正确,否则它将无法工作。它只是十六进制编码的字符,用于描述所需应用程序包的路径,以零结尾。
您可能会看到一条消息,指出资源分支为空且未初始化。如果是这样,则说明您尚未更改源文件上的每个文件关联,因此您需要执行该操作,然后重新运行 DeRez 命令。
一旦您有了这个,您可以将其推送到另一个文件中,如下所示(假设 bar.txt 存在):
Rez foo.r -a -o bar.txt
这将使用来自 foo.r 的资源分支来更新 bar.txt。
为了检查它是否正常工作,因为 Rez 和 DeRez 都没有打印错误消息,只需执行以下操作:
DeRez bar.txt
您应该看到与之前相同的信息。完成所有这些后,只需像平常一样打开文件 bar.txt,它应该会在正确的应用程序中打开,而不是默认应用程序。
要进行批量更改,一旦有了.r 文件,您就可以使用标准通配符扩展,如下所示:
Rez foo.r -a -o *.txt
这将执行所有与通配符匹配的文件。
我已经在 10.7 上完成了这个操作,但据我了解,这也适用于早期的机器。
答案3
我用过属性(它在 APFS 卷上完美运行)
xattr -wx com.apple.LaunchServices.OpenWith \
"`xattr -px com.apple.LaunchServices.OpenWith FILEWITHGOODTYPE`" FILETOCHANGE
它的工作原理是将 OpenWith 扩展属性从已经与正确应用程序关联的文件复制到未关联的文件。