pasawaya
pasawaya

Reputation: 11595

Applying Custom Metadata to Files

I've been trying to figure out a way to give files custom metadata. An example of this would be giving files tags, but that's not the only sort of custom metadata I want to apply. I've looked at a couple posts like these ones - Adding custom metadata to jpeg files, Can I add custom metadata to files?, Set Custom Metadata to Files of any Type - but they all deal with languages other then Objective-C, which is the language that I'm trying to use for the custom metadata. I've looked at Apple's documentation like the File Metadata Attributes Reference and the NSMetadataItem Class Reference, but I haven't found anything related to custom metadata.

My question basically is, is it possible to apply custom metadata to files and if so, how would I go about doing it?

Upvotes: 4

Views: 3115

Answers (3)

Robin Stewart
Robin Stewart

Reputation: 3883

This post includes Swift code to read and write extended file attributes:

Write extend file attributes swift example

Upvotes: 0

Rolf Hendriks
Rolf Hendriks

Reputation: 421

I want to do the same thing for iOS.

the xattr library looks like just the right tool for the job. thanks for the tip. it's c based though, but i am going to use that to create an objc iOS file object class that supports custom metadata. xattr uses void* under the hood, so my custom metadata dictionary could support NSString keys and NSCoding values which would be quite flexible.

this should be a good utility to have. off the top of my head, it could be useful to store MIME types, checksums, or associated download URLs for a saved file. more generally, i could use a file object class to implement utilities other than file metadata - for example creating a file while automatically creating any necessary folders in its path.

Upvotes: 1

Andreas Fink
Andreas Fink

Reputation: 176

Custom metadata can be created by a custom MDImporter. This is a plug in for Spotlight which allows the operating system to view/scan/search custom documents. So your metadata would end up in the spotlight search index. However your spotlight importer plug in would have to extract the metadata from the file itself somehow.

You can also store metadata in the file's resource fork which is a MacOS 9 and older way of dealing with associated data. This would only work under HFS+ formatted volumes and would create two file under other filesystems such as NFS. Also simple unix tools such as cat FILE1 > FILE2 would only copy the data part and potentially break the purpose. I would not recommend this.

Or you could use so called extended attributes to a file which is for example the way the finder remembers from where you downloaded a specific file so it can ask you if you really wanted to execute downloaded from at first launch. (see getxattr(2), listxattr(2), removexattr(2) and setxattr(2) or

From the command line: "xattr" or "ls -l@" From C: getxattr(), listxattr(), removexattr() and setxattr()

Upvotes: 2

Related Questions