Short version: Is there an easy way in Keyboard Maestro to find the last file added to a given folder?
<Unnecessarily_long_version>
I would like to be able to find the last file added to my Downloads folder. I have an inbox folder, conveniently named _Inbox (the underscore so that it will sort to the top of a Finder window) where pretty much every file will wind up for sorting to its final destination. Most of the sorting is done with an extensive set of Hazel rules. Files usually wind up in there by being dragged from email attachments, directly scanned in via ScanSnap, or dumped there after creation on the Desktop.
However, I do have a significant set of files that are downloaded in Safari, which wind up in my Downloads folder. Many of them are eventually sent to Trash, so I donāt want to make the _Inbox the default download location. Therefore, it would be convenient to have a KM macro that I could trigger with a key combination that would move the last added file in the Downloads folder to the _Inbox for sorting. I would probably set it up so that if the macro is trigged with the ā„ key it would pop up a chooser dialog to allow me to move the file to another destination as well.
The part I donāt see easily doable in KM is finding the last added file. That is NOT the latest creation or modification date. MacOS keeps a separate date, a date added properly, which is actually Spotlight metadata and not filesystem metadata, which is the date the file was added to the folder, and that is the proper date to use for this purpose.
I am uncertain how to access this directly in KM. (Hazel can access this property, but is not the right tool here as I donāt want files automatically moved.)
I can do this programmatically, so the problem is āsolved,ā but I wonder if Iām being stupid and missing something in KM that eliminates the need for custom code.
<Even_more_unnecessary_information>
For the curious, you can get the date last added metadata from the shell using mdls:
mdls <file> | grep 'kMDItemDateAdded '
which will give you something that looks like:
kMDItemDateAdded = 2022-05-15 13:25:04 +0000
which you can parse with the tool of your choice, such as awk, and write a shell script or something in KM to find the last date. Note that the space after the last ādā is needed to avoid also pulling out the kMDItemDateAdded_Ranking field.
For the even more curious, my script for this is in Swift:
import Foundation
let fd = FileManager.default
let homeURL = fd.homeDirectoryForCurrentUser
let downloadsFolderURL = homeURL.appendingPathComponent("Downloads")
guard var downloadsFiles = try? fd.contentsOfDirectory(atPath: downloadsFolderURL.path) else {
print("Unable to enumerate Downloads folder")
exit(1)
}
var lastAddedFile: String = ""
var lastAddedDate: Date = Date.distantPast
do {
try downloadsFiles.forEach { (file) throws in
let dateAdded = try? downloadsFolderURL.appendingPathComponent(file)
.resourceValues(forKeys: [.addedToDirectoryDateKey])
.addedToDirectoryDate
if let dateAdded = dateAdded {
if dateAdded > lastAddedDate {
lastAddedFile = file
lastAddedDate = dateAdded
}
}
}
}
catch {
print("Unable to find last added file")
exit(1)
}
print("\(lastAddedFile):\(lastAddedDate)")
</Even_more_unnecessary_information>
</Unnecessarily_long_version>