views:

197

answers:

2

Hi,

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...

+1  A: 

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.

Yuji
Thank you.I found out that I need to use finderInfoFlags:kIsAlias type:0 creator:0 but I don't know how to give it a path...
Thanks heaps man! Note: in your code you use getFinderInfo and it does not exist. You have to use finderInfoFlags like you said a few lines higher.
You're perfectly right. Stupid me. Fixed.
Yuji
A: 
NSString *path;
BOOL isAliasFile=NO;  
FSRef fsRef;
FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &fsRef, NULL);
Boolean isAliasFileBoolean, isFolder;
FSIsAliasFile (&fsRef, &isAliasFileBoolean, &isFolder);
if(isAliasFileBoolean)
    isAliasFile=YES;
NSLog([NSString stringWithFormat:@"%d %@",isAliasFile,path]);

The fastest I've found. You can use it to check if it's a folder - check the link FSIsAliasFile.

Yahoo