Application Last opened metadata

I am using the following code to determine the last opened date of an application


public extension URL {
    var lastUsedDate: Date? {
        guard let item = MDItemCreateWithURL(kCFAllocatorDefault, self as CFURL) else { return nil }
        guard let date = MDItemCopyAttribute(item, kMDItemLastUsedDate) as? Date else { return nil }
        return date
    }
}


In general this works very well, providing the URL being inspected is either /Applications/xyz.app or even ~/Applications/xyz.app. The code returns the same date as that shown by the Finder 'Last opened' attribute for the application.


If the application is NOT in either of these locations then the kMDItemLastUsedDate attribute for the application is never updated. I can observe this with both the code snippet above and also by manually inspecting the attribute in the Finder.


An example failure case:

I have unpacked the developer 'Additional Tools' into ~/Applications/Additional Tools/ preserving the hierarchy from Apples .dmg file. 'Last opened' is never updated for any of these tools, no matter how I open them (i.e. double click in the Finder, or 'open path/to/tool' from a terminal session.


Is this an OS issue (I am running 10.15.3) or possibly a Spotlight problem?

Replies

also by manually inspecting the attribute in the Finder.

So if I understand this properly, you can reproduce this problem without writing any code, that is, using just the Finder? If so, that’s clearly a bug and you should file it as such.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks Quinn

You are correct - no code required to reproduce

I filed a bug - FB7603823