I have developed a Mac application using Qt 5.3.2. This application handles files with specific extension (lets say .xyz
).
I have created an icon file named XYZ.icns and added it to my app bundle Resource folder (MyApp.app/Contents/Resources/XYZ.icns
).
I have also modified the bundle's Info.plist file in order to set the file association. I have added this entry:
<key>CFBundleDocumentTypes</key>
<array>
<!-- Registered file accociation -->
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeName</key>
<string>XYZ</string>
<key>CFBundleTypeExtensions</key>
<array>
<string>xyz</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>XYZ</string>
</dict>
<array>
The result: the file association worked (double clicking on the file does open my application) however, the icon has not been replaced (still displaying the blank document icon).
Is there something that I missed? I looked at other applications for examples and there does not seem to be anything more than what I did.
EDIT: I did some more tests. I dumped the Launch service data with this command:
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister -dump
In the result, I can find claims on file the file type including the icon information:
...
--------------------------------------------------------------------------------
Container mount state: mounted
bundle id: 105396
...
path: /Applications/MyApp.app
name: MyApp
...
--------------------------------------------------------
claim id: 27628
name: XYZ
rank: Default
reqCaps:
roles: Editor
flags: relative-icon-path doc-type
icon: Contents/Resources/XYZ.icns
bindings: .xyz
--------------------------------------------------------------------------------
...
EDIT2: After some time, it finally got to work on its own. Overnight, there is an OS update that got installed and I also had to shutdown the computer (the Launch Service probably refreshed something on its own). I would update my question to : How to make sure Launch Service refreshes the associated file icons when an application is installed or modified?
My understanding is certain (built-in) actions trigger Launch Services to refresh the database. Dragging a new .app bundle to an Applications folders is one trigger, as is running a PackageMaker installer. However there may be a disconnect between what's in the Launch Services database and what the Finder shows (until some other refresh/restart).
Some quick Google searching suggests you can force a rebuild from the command line: /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
However, this step should not be necessary due to the built-in triggers, so I would be hesitant to use it unless you can clearly identify why the built-in triggers fail for your application. And if the problem is the Finder's icon cache, this may not help either.
来源:https://stackoverflow.com/questions/28222787/mac-os-x-file-association-works-but-file-icon-not-changed