Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the Finder obtain the "date added" of an item in a folder?

If a folder is placed in the Dock you can sort it by "date added" - this is usually the default for the Downloads folder. (Sometimes the Finder does not appear to be using the date added but the date modified, but it can find the date added.) Where is the Finder figuring this out from? The standard file metadata, i.e. as obtained by stat, getattrlist or FSGetCatInfo) does not contain it. TIA

like image 780
CRD Avatar asked Dec 13 '22 13:12

CRD


2 Answers

Yep, the date added could be inferred from other structures. In fact, it resides in Spotlight metadata.

NSDate *dateAdded(NSURL *url)
{
    NSDate *rslt = nil;
    MDItemRef inspectedRef = nil;

    inspectedRef = MDItemCreateWithURL(kCFAllocatorDefault, (CFURLRef)url);
    if (inspectedRef){
        CFTypeRef cfRslt = MDItemCopyAttribute(inspectedRef, (CFStringRef)@"kMDItemDateAdded");
        if (cfRslt) {
            rslt = (NSDate *)cfRslt;
        }
    }
    return rslt;
}
like image 52
Wojtek Avatar answered Jan 18 '23 23:01

Wojtek


Note: out of date now that Lion’s out.

The Finder isn’t, the Dock is. It tracks this data internally. If you remove a folder and put it back, the “date added” information is lost for existing items.

like image 39
Jens Ayton Avatar answered Jan 19 '23 00:01

Jens Ayton