Search code examples
macoscocoansfilemanagerfinder

Distinguish things like .app/.xcodeproj and actual folders in Cocoa


I am trying to build an alternative file manager that works similar to the default Finder in Cocoa.

As you can imagine, the app needs to show a list of files/subfolders in certain directory, and when the user click on an item, it checks whether it's a folder or a file that a user has clicked. If it's a folder, the app simply shows the content of the folder. If it's a file, it will be opened with the default application.

I used NSFileManager.file​Exists(at​Path:​is​Directory:​) to determine if an item at certain path is a folder. This works well in most cases, but for things like something.app or project.xcodeproj, they are also considered as directories according to the method.

I know it's true that technically they are just folders, but is there a way in Cocoa to distinguish them from actual folders?


Solution

    • Use (NS)URL.
    • Get the values for resource keys isDirectoryKey and isPackageKey via resourceValues(forKeys.

    In case of bundles isPackage is true.