MacOSX - 与应用程序关联的文件扩展名 - 以编程方式 [英] MacOSX - File extension associate with application - Programmatically

查看:26
本文介绍了MacOSX - 与应用程序关联的文件扩展名 - 以编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get my program to automatically associate certain file extensions to be opened by it but I'm not sure how to do that in MacOSX. I'm not asking how to associate a program with a file extension in the GUI, I want to be able to program it into my program.

推荐答案

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

To register a new file extension with an application use the following defaults command.
Replace PUT_FILE_EXTENSION_HERE_WITHOUT_PERIOD with the file extension i.e. txt.
Replace org.category.program with the com/org name of your program i.e. 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>"


将文件扩展名添加到启动服务后,您必须重新启动启动服务守护进程,以便它重新读取配置文件.

Once you have added the file extension to the launch services, you must restart the launch services deamon so it will re-read the configuration file.

您可以运行以下命令重新启动启动服务,也可以简单地重新启动计算机.登录/退出也可以,但我没试过.

You can either run the command below to restart launch services, or simply restart your computer. Loging in/loging out also might do it but I haven't tried.

$ /System/Library/Frameworks/CoreServices.framework/Versions/A/Framework/LaunchServices.framework/Versions/A/Support/lsregister -kill -domain local -domain system -domain user

这篇关于MacOSX - 与应用程序关联的文件扩展名 - 以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆