patrickkidd
patrickkidd

Reputation: 3137

How to get macos to treat document package as a file?

I am trying to create a document package with it's own Icon that macOS treats like a file, and you have to choose "Show Package Contents" to open it as a folder. I have added a document type, and NSDocument::initWithContentsOfURL is opening it correctly when passed ofType:"Family Diagram" which matches the UTI.

But the icon is not set on folders with the .fd extension and macOS still treats them like a folder. I have LSTypeIsPackage set to true.

Here is my info.plist:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>BuildMachineOSBuild</key>
        <string>17A362a</string>
        <key>CFBundleDevelopmentRegion</key>
        <string>English</string>
        <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeExtensions</key>
                <array>
                    <string>fd</string>
                </array>
                <key>CFBundleTypeIconFile</key>
                <string>PKDiagram-Document</string>
                <key>CFBundleTypeMIMETypes</key>
                <array>
                    <string>application/x-fd</string>
                </array>
                <key>CFBundleTypeName</key>
                <string>Family Diagram</string>
                <key>CFBundleTypeOSTypes</key>
                <array>
                    <string>FD</string>
                </array>
                <key>CFBundleTypeRole</key>
                <string>Editor</string>
                <key>LSHandlerRank</key>
                <string>Default</string>
                <key>LSTypeIsPackage</key>
                <true/>
                <key>NSDocumentClass</key>
                <string>DiagramDocument</string>
            </dict>
        </array>
        <key>CFBundleExecutable</key>
        <string>Family Diagram</string>
        <key>CFBundleGetInfoString</key>
        <string>Created by Vedanā Media</string>
        <key>CFBundleIconFile</key>
        <string>PKDiagram-Filled.icns</string>
        <key>CFBundleIdentifier</key>
        <string>com.vedanamedia.familydiagrammac</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleSupportedPlatforms</key>
        <array>
            <string>MacOSX</string>
        </array>
        <key>DTCompiler</key>
        <string>com.apple.compilers.llvm.clang.1_0</string>
        <key>DTPlatformBuild</key>
        <string>9A235</string>
        <key>DTPlatformVersion</key>
        <string>GM</string>
        <key>DTSDKBuild</key>
        <string>17A360</string>
        <key>DTSDKName</key>
        <string>macosx10.13</string>
        <key>DTXcode</key>
        <string>0900</string>
        <key>DTXcodeBuild</key>
        <string>9A235</string>
        <key>LSApplicationCategoryType</key>
        <string>public.app-category.healthcare-fitness</string>
        <key>NSHighResolutionCapable</key>
        <true/>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
    </dict>
    </plist>

And here is a screenshot of the UTI in Xcode, notice that the icon is recognized:

UTI listing in Xcode

Upvotes: 5

Views: 625

Answers (1)

patrickkidd
patrickkidd

Reputation: 3137

Turns out you have to add it to exported types as well, and it has to conform to com.apple.package. Then you have to bump the CBundleVersion number in your Info.plist to get the system to re-read the UTI settings. This will reliably enable and disable your UTI package being seen as a file instead of a directory.

NOTE: This also changes how NSMetadataQuery treats the file as well, so if you were relying on recursive results from NSMetadataQuery this should be replaced by implementing fileWrapperOfType and readFromFileWrapper in NSDocument.

I do not recall seeing this explicitly stated anywhere in the Apple docs.

Exported Types

Upvotes: 8

Related Questions