Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X file association works, but file icon not changed

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?

like image 627
gfrigon Avatar asked Jan 29 '15 19:01

gfrigon


People also ask

How do I reset file associations on a Mac?

You can easily change the default app associated with a particular file to something else. To do so, click Get Info in the contextual menu and select another app from the Open With: dropdown list in the file's Info pane. To use that app to open all files sharing the same extension, click the Change All...

How do I change the default icon for a file type Mac?

On your Mac, select the file or folder. Choose File > Get Info in the menu bar. At the top of the Info window, select the small custom icon. Make sure you click the small icon at the top of the Info window (not the large icon under Preview).

How do I associate a file with an application on a Mac?

On your Mac, do one of the following: Drag the file onto the app icon in the Finder or the Dock. Select the file in the Finder, choose File > Open With, then choose an app. Control-click the file, choose Open With, then choose an app.


1 Answers

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.

like image 134
James Turner Avatar answered Oct 29 '22 11:10

James Turner