Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering an icon for my application's document type

I'm trying to register an icon for my app's document type. After reading Declaring New Uniform Type Identifiers and looking at /Developer/Examples/Sketch I came up with something like this in my Info.plist:

<key>CFBundleDocumentTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Viewer</string>
    <key>LSItemContentTypes</key>
    <array>
      <string>com.mycompany.myextension</string>
    </array>
    <key>NSDocumentClass</key>
    <string>NSString</string>
  </dict>
</array>

...

<key>UTExportedTypeDeclarations</key>
<array>
  <dict>
    <key>UTTypeDescription</key>
    <string>Blah blah blah</string>
    <key>UTTypeConformsTo</key>
    <array>
      <string>public.data</string>
    </array>
    <key>UTTypeIconFile</key>
    <string>My-file-icon.icns</string>
    <key>UTTypeIdentifier</key>
    <string>com.mycompany.myextension</string>
    <key>UTTypeTagSpecification</key>
    <dict>
      <key>public.filename-extension</key>
      <array>
        <string>myextension</string>
      </array>
    </dict>
  </dict>
</array>

Now, everything is fine and dandy, i.e. my program is opened when I click on a file with my extension, etc. However, the document icon is not registered with the OS, i.e. I see an ugly blank icon instead of my beautiful My-file-icon.icns. I suspect that I'm missing something in the plist above, any ideas?

like image 237
svintus Avatar asked Nov 20 '09 16:11

svintus


People also ask

How do I assign an icon to a file type?

Right click extension whose icon you want to change and then select “Edit Selected File Type.” In the “Edit File Type” window, click the “…” button to the right of the Default Icon text field. The “Change Icon” window shows some basic icons, but click the “Browse” button to find your own icon files.

How do I change the default file icon?

Right-click the shortcut you created, click Properties, and then click Change Icon. Click Browse, and type c:\windows\system\shell32. dll in the File name box, click Open, click to select the icon you want to use, click OK, and then click OK.

How do I change file icons in Windows 7?

File Types ManagerRight-Click the file type you'd like to change, and then Select Edit Selected File Type. In the Edit window that appears, Click the … button next to Default Icon. Browse for the icon that you would like to use, and then Click OK from both open windows to apply changes.


3 Answers

Try putting the icon name in the CFBundleTypeIconFile key in the CFBundleDocumentTypes array, not in the UTExportedTypeDeclarations array.

And of course make sure that "My-file-icon.icns" is in your target's Copy Bundle Resources build phase and is being copied into Contents/Resources in your app's bundle.

like image 193
cdespinosa Avatar answered Nov 16 '22 04:11

cdespinosa


Your UTI declaration in the Info.plist appears to be correct, however I noticed an other issue. If you application is a document-based application you need to replace the NSString in following entry with your NSDocument subclass:

<key>NSDocumentClass</key>
<string>NSString</string>

For example it's "SKTDocument" in Sketch:

<key>NSDocumentClass</key>
<string>SKTDocument</string>

Edit: Please also make sure to use your own reverse domain name for your exported UTIs. This ensures that UTIs are unique. For example its com.mindnode.MindNode.MindNodeDocument in my case.

like image 41
Markus Müller-Simhofer Avatar answered Nov 16 '22 02:11

Markus Müller-Simhofer


I've been trying to do this in Xcode 11 and it's been a challenge. There are lots of answers here on SO to questions like this but I couldn't find one that had everything I needed to get this working. I'll try to do that here.

Technical Q&A QA1587 Although this is a bit old I think it has everything required but it's a bit vague in some spots like when it says Provide an icon for the document.

I thought I could make it work with an icon in Assets.xcassets but it didn't work for me until I provided an icns file. I took the 1024x1024 png app icon and converted it to an icns here at cloudconvert. Then I added that icns to my project. I don't think it matters where you put it. I put it just below the top level. It's circled in the image below.

enter image description here Follow the arrows in the image and you end up at the Info tab. I tapped the Add button to create a Document Type and filled it in as shown. When you tap the Add button in the icon section it will ask you for an icns file. Once I had mine in the project it appeared in the window and I could select it.

Same for Configured UTIs.

That was it. In Files, Messages, or Mail the icon appears.

like image 27
Murray Sagal Avatar answered Nov 16 '22 02:11

Murray Sagal