如何读取文件注释字段

时间:2022-10-18 17:18:59

In OS X Finder there is 'Comment' file property. It can be checked in finder by adding 'Comment' column or edited/checked after right clicking on file or folder and selecting 'Get info'.

在OS X查找程序中有一个“Comment”文件属性。可以在finder中添加“Comment”列,或者在右键单击文件或文件夹并选择“Get info”后进行编辑/检查。

How to read this value in swift or objective-c?

如何在swift或objective-c中读取这个值?

I checked already NSURL and none of them seems to be the right ones

我已经检查过了NSURL,但似乎没有一个是正确的。

2 个解决方案

#1


5  

Do not use the low-level extended attributes API to read Spotlight metadata. There's a proper Spotlight API for that. (It's called the File Metadata API.) Not only is it a pain in the neck, there's no guarantee that Apple will keep using the same extended attribute to store this information.

不要使用低级扩展属性API来读取Spotlight元数据。这里有一个合适的Spotlight API。(它被称为文件元数据API。)这不仅让人头疼,也不能保证苹果会继续使用同样的扩展属性来存储这些信息。

Use MDItemCreateWithURL() to create an MDItem for the file. Use MDItemCopyAttribute() with kMDItemFinderComment to obtain the Finder comment for the item.

使用MDItemCreateWithURL()为文件创建一个MDItem。使用MDItemCopyAttribute()与kMDItemFinderComment获取该项目的Finder注释。

#2


3  

Putting the pieces together (Ken Thomases reading answer above and writing answer link) you can extend URL with a computed property with a getter and a setter to read/write comments to your files:

将各个部分放在一起(Ken Thomases阅读上面的答案和写答案链接),您可以使用带有getter和setter的计算属性扩展URL,以便对文件进行读写注释:

update: Xcode 8.2.1 • Swift 3.0.2

更新:Xcode 8.2.1•Swift 3.0.2

extension URL {
    var finderComment: String? {
        get {
            guard isFileURL else { return nil }
            return MDItemCopyAttribute(MDItemCreateWithURL(kCFAllocatorDefault, self as CFURL), kMDItemFinderComment) as? String
        }
        set {
            guard isFileURL, let newValue = newValue else { return }
            let script = "tell application \"Finder\"\n" +
                String(format: "set filePath to \"%@\" as posix file \n", absoluteString) +
                String(format: "set comment of (filePath as alias) to \"%@\" \n", newValue) +
            "end tell"
            guard let appleScript = NSAppleScript(source: script) else { return }
            var error: NSDictionary?
            appleScript.executeAndReturnError(&error)
            if let error = error {
                print(error[NSAppleScript.errorAppName] as! String)
                print(error[NSAppleScript.errorBriefMessage] as! String)
                print(error[NSAppleScript.errorMessage] as! String)
                print(error[NSAppleScript.errorNumber] as! NSNumber)
                print(error[NSAppleScript.errorRange] as! NSRange)
            }
        }
    }
}

Xcode 7.3.1 • Swift 2.2.1

Xcode 7.3.1•Swift 2.2.1

extension NSURL {
    var finderComment: String? {
        get {
            guard fileURL else { return nil }
            return MDItemCopyAttribute(MDItemCreateWithURL(kCFAllocatorDefault, self), kMDItemFinderComment) as? String
        }
        set {
            guard let newValue = newValue where fileURL else { return }
            let script = "tell application \"Finder\"\n" +
                         String(format: "set filePath to \"%@\" as posix file \n", absoluteString) +
                         String(format: "set comment of (filePath as alias) to \"%@\" \n", newValue) +
                         "end tell"
            guard let appleScript = NSAppleScript(source: script) else { return }
            var error: NSDictionary?
            appleScript.executeAndReturnError(&error)
            if let error = error {
                print(error[NSAppleScriptErrorAppName] as! String)
                print(error[NSAppleScriptErrorBriefMessage] as! String)
                print(error[NSAppleScriptErrorMessage] as! String)
                print(error[NSAppleScriptErrorNumber] as! NSNumber)
                print(error[NSAppleScriptErrorRange] as! NSRange)
            }
        }
    }
}

#1


5  

Do not use the low-level extended attributes API to read Spotlight metadata. There's a proper Spotlight API for that. (It's called the File Metadata API.) Not only is it a pain in the neck, there's no guarantee that Apple will keep using the same extended attribute to store this information.

不要使用低级扩展属性API来读取Spotlight元数据。这里有一个合适的Spotlight API。(它被称为文件元数据API。)这不仅让人头疼,也不能保证苹果会继续使用同样的扩展属性来存储这些信息。

Use MDItemCreateWithURL() to create an MDItem for the file. Use MDItemCopyAttribute() with kMDItemFinderComment to obtain the Finder comment for the item.

使用MDItemCreateWithURL()为文件创建一个MDItem。使用MDItemCopyAttribute()与kMDItemFinderComment获取该项目的Finder注释。

#2


3  

Putting the pieces together (Ken Thomases reading answer above and writing answer link) you can extend URL with a computed property with a getter and a setter to read/write comments to your files:

将各个部分放在一起(Ken Thomases阅读上面的答案和写答案链接),您可以使用带有getter和setter的计算属性扩展URL,以便对文件进行读写注释:

update: Xcode 8.2.1 • Swift 3.0.2

更新:Xcode 8.2.1•Swift 3.0.2

extension URL {
    var finderComment: String? {
        get {
            guard isFileURL else { return nil }
            return MDItemCopyAttribute(MDItemCreateWithURL(kCFAllocatorDefault, self as CFURL), kMDItemFinderComment) as? String
        }
        set {
            guard isFileURL, let newValue = newValue else { return }
            let script = "tell application \"Finder\"\n" +
                String(format: "set filePath to \"%@\" as posix file \n", absoluteString) +
                String(format: "set comment of (filePath as alias) to \"%@\" \n", newValue) +
            "end tell"
            guard let appleScript = NSAppleScript(source: script) else { return }
            var error: NSDictionary?
            appleScript.executeAndReturnError(&error)
            if let error = error {
                print(error[NSAppleScript.errorAppName] as! String)
                print(error[NSAppleScript.errorBriefMessage] as! String)
                print(error[NSAppleScript.errorMessage] as! String)
                print(error[NSAppleScript.errorNumber] as! NSNumber)
                print(error[NSAppleScript.errorRange] as! NSRange)
            }
        }
    }
}

Xcode 7.3.1 • Swift 2.2.1

Xcode 7.3.1•Swift 2.2.1

extension NSURL {
    var finderComment: String? {
        get {
            guard fileURL else { return nil }
            return MDItemCopyAttribute(MDItemCreateWithURL(kCFAllocatorDefault, self), kMDItemFinderComment) as? String
        }
        set {
            guard let newValue = newValue where fileURL else { return }
            let script = "tell application \"Finder\"\n" +
                         String(format: "set filePath to \"%@\" as posix file \n", absoluteString) +
                         String(format: "set comment of (filePath as alias) to \"%@\" \n", newValue) +
                         "end tell"
            guard let appleScript = NSAppleScript(source: script) else { return }
            var error: NSDictionary?
            appleScript.executeAndReturnError(&error)
            if let error = error {
                print(error[NSAppleScriptErrorAppName] as! String)
                print(error[NSAppleScriptErrorBriefMessage] as! String)
                print(error[NSAppleScriptErrorMessage] as! String)
                print(error[NSAppleScriptErrorNumber] as! NSNumber)
                print(error[NSAppleScriptErrorRange] as! NSRange)
            }
        }
    }
}