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

3

5

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

Bedraggled answered 15/5, 2011 at 7:50 Comment(0)
A
12

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;
}
Aguste answered 3/8, 2011 at 19:15 Comment(0)
I
2

Here's a Swift 5.x version of Wojtek's answer:

public extension URL {
    var dateAdded: Date? {
        guard let metadataItemValue = MDItemCreateWithURL(kCFAllocatorDefault, (self as CFURL)) else {
            return nil
        }
        return MDItemCopyAttribute(metadataItemValue, kMDItemDateAdded) as? Date
    }
}

I've tested this back to Swift 4.x, and I think it'll compile without modification back to Swift 3.x if you need that too. Just be aware that, before Swift 5, its inferred visibility would be internal rather than public.

Irish answered 4/8, 2019 at 2:53 Comment(0)
A
1

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.

Auxiliaries answered 15/5, 2011 at 7:53 Comment(4)
Sounds a reasonable explanation. But take a look at <appleinsider.com/articles/11/02/27/…> - now this is a rumor site and some might say the shots are all fake, but it does show the Lion Finder supporting sort by date added... So maybe Apple is adding this tracking to the OS (either in or outside the file system) or Finder in 10.7?Bedraggled
Entirely likely, and it wouldn’t be hard to do. But it ain’t there yet.Auxiliaries
I was hoping I'd missed something and the date added could be inferred from other structures, but no answers to that effect. As to what Lion may/may not be doing, this is the wrong forum to discuss that!Bedraggled
@JensAyton So how can I fetch the date for all folders?Leatherjacket

© 2022 - 2024 — McMap. All rights reserved.