Watch folder trigger after set time

Is there a way to use a folder as a trigger but format it as such

"Trigger x, after folder hasn't been updated in 5 minutes"

seems basic enough but I can't find any information on this anywhere

The purpose of the Folder trigger is to trigger the macro when a file is added and/or removed from the Folder.

If you just want to process all (or some) of the items already in the folder, then you should use the For Each action with a "The files in directory" Collection:

image

The full POSIX path of each item in the directory/folder is put into the KM Variable "Local__FilePath" as the For Each Action loops through each item in the directory/folder.

in such cases I would use the tool "Hazel".

If you already own both KM and Hazel, then you can evaluate which tool you like best for any given automation task. But if you own KM, but not Hazel, then I don't find it cost effective to buy another tool for which the one you own can do the job very well.

1 Like

Make two macros:

Macro 1:
Triggered: every time a change happens to the folder.
Actions:

  • Semaphore Lock “Last Folder Changed”
  • Set Variable “DND Last Folder Changed” to calculation “NOW()”
  • Enable Macro 2

Macro 2 (initially disabled)
Triggered: every minute
Actions:

  • Semaphore Lock “Last Folder Changed”
  • If calculation “NOW() - DND Last Folder Changed > 5 * 60”
    • Do stuff
    • Disable Macro 2
1 Like

Peter, how would you get a folder watcher to recognize and act on multiple files added to the folder simultaneously?

  • Thanks!

The macro would be triggered for each file, so you would have to store all the files in a variable and then process the variable at some latter time.

Something like:

  • Semaphore Lock "Folder Changed"
  • Append Variable "Folder Changes" with "%TriggerValue%%Return%"
  • Semaphore Unlock "Folder Changed"
  • Pause (some length of time to allow all the triggering to happen)
  • Semaphore Lock "Folder Changed"
  • If Variable "Folder Changes" is not empty
    • act on the content of variable "Folder Changes"
    • Set Variable "Folder Changes" to ""
  • Semaphore Unlock "Folder Changed"

How this works is that the lock stops the paths being added to the variable and the variable being acted upon both happening at the same time, as well as multiple different “act ons” happening at the same time.

So that will result in each triggered macro adding the path to the variable, and then waiting, and then stopping any further additions or any other macros, it will process the additions, and then release the lock, allowing the other macros to complete without doing anything and further additions to happen.

Thanks so very much. Looking upthread at the example you gave for the 5 minute trigger delay, how would that translate to multiple items hitting the watched folder more or less simultaneously? I feel like the time-delay-macro in your example upthread would not be able to handle multiple incoming items. Would semaphore lock play a role in that event?

Alternatively, is it advisable to use a Pause action for a lengthy pause of up to a week? Thanks again.

That one simply runs the process several minutes after the last folder change. So the macro then has to process the folder to determine what to do and how many files to process.

Thank you. Final question: is there any reason not to have an action like this pause for hours or even days before acting on the target file or files?

Yes. Keyboard Maestro Engine is highly efficient when it is idle (waiting on a trigger). But when any macro is running, energy and CPU usage increase. Version 11 has some mitigations for that when it is simply a Pause or Pause Until, but even so it is better not to have macros running indefinitely. Also, if the engine is restarted for any reason, then the final actions will never happen. So it's better to handle the actions via a second trigger if it is going to be a long day. For example, if you want to delete screenshots when they are over an hour old, trigger a macro that runs every hour and looks for screenshots that are over an hour old and deletes them.

1 Like

Peter,
I just want to thank you for your informative, and kind, responses to my questions. Your responses prodded me to engage in very productive experimentation that helped me master the folder addition trigger and periodic folder monitoring trigger to the extent that I am now able to discard the Hazel app and rely completely on Keyboard Maestro to monitor folders and act on files.

Just one example: here is a macro that trashes files - but not folders, and not items tagged "do-not-delete" - added more than three days ago to my Downloads folder (which happens to be in Dropbox).

I deleted Hazel ($42 for single user license) after replicating each and every task it was doing for me using KM ($36 per license). Then, I paused to reflect on what a brilliant and amazing app you have created. I think I have been using it for about ten years. Every now and then I hit a plateau as far as creating new macros. But then, I encounter some additional use-case and, unfailingly, I discover a corresponding capability in KM that can handle the new task, usually with elegance and efficiency. The feeling I get when that happens is one of deep satisfaction and gratitude. KM - along with the amiable community that has grown up around it, fostered by your constant, thoughtful presence - encourages creative problem solving and inspires the imagination. It's the app that, by automating so much of the tedium, has, more than any other, made my computer what Steve Jobs envisioned: a bicycle for the mind.

THANK YOU, PETER!

5 Likes