Is there an easy way to use the date last added metadata in Keyboard Maestro?

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>

KM has two examples macros, one called ā€œFind Last Addedā€ and another one called ā€œFind Latest Fileā€. Do any of these solve your use case?

Thereā€™s also a macro here that checks latest added item:

Thanks.

Triggering on a file being added to Downloads isnā€™t exactly what I need, as I donā€™t always want to move the file; I want to trigger it manually when I want this to happen.

The other references should solve the problem. I can loop through a collection of the files the Downloads folder, reverse sorted, move the file in the first iteration of the loop, and then exit the loop, which will do the job.

Great tip, much appreciated. Now that I wrote the code to find the last file, I have at least two options.

1 Like

Remembered they have a forum, visit https://forum.keyboardmaestro.com and youā€™ll get many more suggestions, very knowledgeable people there.

Thanks. Your suggestion was perfect. I might stick with my script just because I bothered to write it, but I am going to test the sorting by date added to see if they match.

1 Like