在swift中获取文件的别名路径

时间:2015-10-26 14:47:20

标签: macos swift cfurl-encoding

我无法解析mac上的别名链接。我正在检查文件是否是别名然后我想要接收原始路径。相反,我只获得了文件ID。 想法?

func isFinderAlias(path:String) -> Bool? {

    var isAlias:Bool? = false // Initialize result var.

    // Create a CFURL instance for the given filesystem path.
    // This should never fail, because the existence isn't verified at this point.
    // Note: No need to call CFRelease(fUrl) later, because Swift auto-memory-manages CoreFoundation objects.
    print("path before \(path)");
    let fUrl = CFURLCreateWithFileSystemPath(nil, path, CFURLPathStyle.CFURLPOSIXPathStyle, false)
    print("path furl \(fUrl)");
    // Allocate void pointer - no need for initialization,
    // it will be assigned to by CFURLCopyResourcePropertyForKey() below.
    let ptrPropVal = UnsafeMutablePointer<Void>.alloc(1)

    // Call the CoreFoundation function that copies the desired information as
    // a CFBoolean to newly allocated memory that prt will point to on return.
    if CFURLCopyResourcePropertyForKey(fUrl, kCFURLIsAliasFileKey, ptrPropVal, nil) {

        // Extract the Bool value from the memory allocated.
        isAlias = UnsafePointer<CFBoolean>(ptrPropVal).memory as Bool


        // it will be assigned to by CFURLCopyResourcePropertyForKey() below.
        let ptrDarwin = UnsafeMutablePointer<DarwinBoolean>.alloc(1)

        if ((isAlias) == true){
            if let bookmark = CFURLCreateBookmarkDataFromFile(kCFAllocatorDefault, fUrl, nil){
                let url = CFURLCreateByResolvingBookmarkData(kCFAllocatorDefault, bookmark.takeRetainedValue(), CFURLBookmarkResolutionOptions.CFBookmarkResolutionWithoutMountingMask, nil, nil, ptrDarwin, nil)
                print("getting the path \(url)")
            }
        }

        // Since the CF*() call contains the word "Copy", WE are responsible
        // for destroying (freeing) the memory.
        ptrDarwin.destroy()
        ptrDarwin.dealloc(1)
        ptrPropVal.destroy()
    }

    // Deallocate the pointer
    ptrPropVal.dealloc(1)

    return isAlias
}

修改 这两个答案都是正确的! 我会选择mklement0的答案,因为最初没有声明要求代码在10.9上运行,这使得它更灵活

4 个答案:

答案 0 :(得分:5)

这是使用NSURL的解决方案。

它希望NSURL对象作为参数,如果网址是别名,则返回原始路径或nil

func resolveFinderAlias(url:NSURL) -> String? {

  var isAlias : AnyObject?
  do {
    try url.getResourceValue(&isAlias, forKey: NSURLIsAliasFileKey)
    if isAlias as! Bool {
      do {
        let original = try NSURL(byResolvingAliasFileAtURL: url, options: NSURLBookmarkResolutionOptions())
        return original.path!
      } catch let error as NSError {
        print(error)
      }
    }
  } catch _ {}

  return nil
}

斯威夫特3:

func resolveFinderAlias(at url: URL) -> String? {
    do {
        let resourceValues = try url.resourceValues(forKeys: [.isAliasFileKey])
        if resourceValues.isAliasFile! {
            let original = try URL(resolvingAliasFileAt: url)
            return original.path
        }
    } catch  {
        print(error)
    }
    return nil
}

如果在沙盒环境中调用该函数,请注意提供适当的权利。

答案 1 :(得分:2)

vadian's answer适用于 OS X 10.10 +

这是实施,也适用于OS X 10.9

// OSX 10.9+
// Resolves a Finder alias to its full target path.
// If the given path is not a Finder alias, its *own* full path is returned.
// If the input path doesn't exist or any other error occurs, nil is returned.
func resolveFinderAlias(path: String) -> String? {
  let fUrl = NSURL(fileURLWithPath: path)
  var targetPath:String? = nil
  if (fUrl.fileReferenceURL() != nil) { // item exists
    do {
        // Get information about the file alias.
        // If the file is not an alias files, an exception is thrown
        // and execution continues in the catch clause.
        let data = try NSURL.bookmarkDataWithContentsOfURL(fUrl)
        // NSURLPathKey contains the target path.
        let rv = NSURL.resourceValuesForKeys([ NSURLPathKey ], fromBookmarkData: data) 
        targetPath = rv![NSURLPathKey] as! String?
    } catch {
        // We know that the input path exists, but treating it as an alias 
        // file failed, so we assume it's not an alias file and return its
        // *own* full path.
        targetPath = fUrl.path
    }
  }
  return targetPath
}

注意:

  • 与vadian的解决方案不同,这将为 -alias文件返回一个值,即该文件的拥有完整路径,并使用路径字符串而不是NSURL实例作为输入。

  • vadian的解决方案需要适当的权利才能在沙盒应用程序/环境中使用该功能。 似乎这个至少不需要到相同的范围,因为它 Xcode中运行游乐场,与vadian的解决方案不同。 如果有人能说清楚这一点,请帮忙。

    • 无论哪种解决方案, shell脚本中使用shebang行#!/usr/bin/env swift运行。
  • 如果您想明确地测试给定路径是否是Finder别名,请参阅this answer,这是源自vadian的,但由于其较窄焦点也在10.9上运行。

答案 2 :(得分:1)

这是一个Swift 3实现,主要基于vadian的方法。我的想法是返回一个文件URL,因此我将其与fileURLWithPath有效地结合起来。它是一个NSURL类扩展,因为我需要能够从现有的Objective-C代码调用它:

extension NSURL {
    class func fileURL(path:String, resolveAlias yn:Bool) -> URL {
        let url = URL(fileURLWithPath: path)
        if !yn {
            return url
        }
        do {
            let vals = try url.resourceValues(forKeys: [.isAliasFileKey])
            if let isAlias = vals.isAliasFile {
                if isAlias {
                    let original = try URL(resolvingAliasFileAt: url)
                    return original
                }
            }
        } catch {
            return url // give up
        }
        return url // really give up
    }
}

答案 3 :(得分:0)

网址变体我需要返回nil(不是别名或错误)其他原始版本 - Swift4

func resolvedFinderAlias() -> URL? {
    if (self.fileReferenceURL() != nil) { // item exists
        do {
            // Get information about the file alias.
            // If the file is not an alias files, an exception is thrown
            // and execution continues in the catch clause.
            let data = try NSURL.bookmarkData(withContentsOf: self as URL)
            // NSURLPathKey contains the target path.
            let rv = NSURL.resourceValues(forKeys: [ URLResourceKey.pathKey ], fromBookmarkData: data)
            var urlString = rv![URLResourceKey.pathKey] as! String
            if !urlString.hasPrefix("file://") {
                urlString = "file://" + urlString
            }
            return URL.init(string: urlString)
        } catch {
            // We know that the input path exists, but treating it as an alias
            // file failed, so we assume it's not an alias file so return nil.
            return nil
        }
    }
    return nil
}