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?