MacOSX - 文件扩展名与应用程序关联 - 以编程方式

Nic*_*ick 15 macos file file-association

我正在尝试让我的程序自动关联某些文件扩展名,但是我不确定如何在MacOSX中执行此操作.我不是问如何将程序与GUI中的文件扩展名相关联,我希望能够将它编程到我的程序中.

Nic*_*ick 11

要使用应用程序注册新的文件扩展名,请使用以下defaults命令.
将PUT_FILE_EXTENSION_HERE_WITHOUT_PERIOD替换为文件扩展名即txt.
将org.category.program替换为程序的com/org名称,即com.apple.itunes.

$ defaults write com.apple.LaunchServices LSHandlers -array-add \
"<dict><key>LSHandlerContentTag</key>
<string>PUT_FILE_EXTENSION_HERE_WITHOUT_PERIOD</string><key>LSHandlerContentTagClass</key>
<string>public.filename-extension</string><key>LSHandlerRoleAll</key>
<string>org.category.program</string></dict>"
Run Code Online (Sandbox Code Playgroud)


将文件扩展名添加到启动服务后,必须重新启动启动服务deamon,以便重新读取配置文件.

您可以运行以下命令重新启动启动服务,也可以只重新启动计算机.登录/注销也可能会这样做,但我还没有尝试过.

$ /System/Library/Frameworks/CoreServices.framework/Versions/A/Framework/LaunchServices.framework/Versions/A/Support/lsregister -kill -domain local -domain system -domain user
Run Code Online (Sandbox Code Playgroud)


Ken*_*agh 7

在这里查看CFBundleDocumentTypes Info.plist键的描述:

http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-101685

-K

  • 链接错误。想必这就是现在该去的地方:https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-101685 (2认同)