Jef Jef - 18 days ago 10
Objective-C Question

Check if path is folder/bundle/alias in ObjC

I come from the AppleScript land and there we use

alias of (info for thePath)
package folder of (info for thePath)
folder of (info for thePath)


to see if a path is either of the above. But I can't seem to find out how to do it in ObjC/Cocoa. I'm pretty sure it's quite easy but I fail to find any info about it.

Thanks...

Answer

Typically you use either NSFileManager or NSWorkspace.

To see if a path is a folder/directory, use NSFileManager's -fileExistsAtPath:isDirectory:.

To see if a path is a package, use NSWorkspace's isFilePackageAtPath:.

I don't know any native Cocoa way to check if a path is an alias (it's a pre-OS X concept...). I always use Nathan Day's Cocoa wrapper for Alias, NDAlias. See finderInfoFlags:type:creator: in his NSString category. To use it, do

UInt16 flags;
OSType type;
OSType creator;
if([@"/path/to/file" finderInfoFlags:&flags type:&type creator:&creator]){
    if(flags&kIsAlias){
         the file is an alias...
    }
}else{
   some error occurred... 
}

Well it looks unnecessarily complicated, but that's life. Alias belongs to Classic Mac OS technology, while Cocoa belongs to the NeXTStep heritage.

Comments