Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

macos - MacOSX - File extension associate with application - Programmatically

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...