3

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

4

6 回答 6

4

通常您使用NSFileManagerNSWorkspace

要查看路径是否为文件夹/目录,请使用NSFileManager's -fileExistsAtPath:isDirectory:

要查看路径是否为包,请使用NSWorkspace's isFilePackageAtPath:

我不知道任何本地 Cocoa 方法来检查路径是否是别名(这是 OS X 之前的概念......)。我总是使用 Nathan Day 的 Cocoa 包装器作为别名,NDAlias。见finderInfoFlags:type:creator:他的NSString类别。要使用它,请执行

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

好吧,它看起来不必要地复杂,但这就是生活。Alias 属于 Classic Mac OS 技术,而 Cocoa 属于 NeXTStep 遗产。

于 2010-02-12T23:39:15.793 回答
3
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]);

我找到的最快的。您可以使用它来检查它是否是一个文件夹 - 检查链接FSIsAliasFile

于 2010-03-06T21:08:12.990 回答
3

从 OS X 10.6 开始,您可以执行以下操作来确定文件是否为别名:

- (BOOL) fileIsAlias: (NSString*) thePath {
    NSURL* url = [NSURL fileURLWithPath:thePath];
    CFURLRef cfurl = (__bridge CFURLRef) url;
    CFBooleanRef cfisAlias = kCFBooleanFalse;
    BOOL success = CFURLCopyResourcePropertyForKey(cfurl, kCFURLIsAliasFileKey, &cfisAlias, NULL);

    BOOL isAlias = CFBooleanGetValue(cfisAlias);
    return isAlias && success;
}

(雅虎的解决方案在 OS X 10.8 中已弃用)

于 2013-07-29T11:52:04.630 回答
3

NSURLIsAliasFileKey您可以在 macOS 10.6+ 上使用资源值键

NSNumber * number = nil;
[fileUrl getResourceValue:&number forKey:NSURLIsAliasFileKey error:nil];
BOOL isAlias = [number boolValue];
于 2018-09-08T06:01:59.263 回答
0

我检查 NSURL 的方法是别名(对我有用):

// url - is alias?
NSData * bookmarkData = [NSURL bookmarkDataWithContentsOfURL:url error:nil];
BOOL isAlias = (bookmarkData) ? YES : NO;

祝你好运!

于 2014-11-04T15:00:13.047 回答
0

我一直在寻找使用 Swift 3.0 检测别名和符号链接的解决方案,并在 core Foundation中找到了响应

所以你可以创建一个 URL 扩展

extension URL{

    var isAnAlias:Bool{
        // Alias? https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/URL.swift#L417
        let resv:URLResourceValues? = try? self.resourceValues(forKeys: [URLResourceKey.isAliasFileKey])
        return resv?.isAliasFile ?? false
    }

}
于 2016-11-14T16:37:36.913 回答