I am trying to create a macro that watches a folder, when a new file (pdf) arrives, the macro automatically opens the file with a third-party app (Readiris23, for OCRing and compressing).
The very simple Macro I have so far uses 'Open a File, Folder or Application', but I don't know what to include in the Open field to get the most recent file to open. This file will have a variable file name (consisting of 'Scan' followed by date and time).
You can do this with Keyboard Maestro Actions by breaking the task down into three parts:
Make the Macro trigger when files are added to your "Watch Folder". This is the part you have already worked out how to do.
A "For Each" Action that gets the path/filename/extension of the latest added file and saves that info to a Local Variable.
The "For Each" Action is set up to look in the contents of your Watched Folder, ordered "by date added" and to "Break From Loop" after one pass i.e. this means it only gets the latest added file.
A "For Each" Action to test to see if that file is a pdf (by seeing if the Local Variable contains the extension pdf). At this point you could include further tests such as whether the filename contains certain words. If the test passes, do something. If not, do nothing.
Here is an Example Macro. You would replace the Magenta Action with what you actually want to do i.e. open the file with your specific Application etc.
It doesn't even need that -- if you drop a bunch of files into a folder, each will trigger the macro individually. Six files, six instances, six separate trigger values. Depending on the macro you may be into "Semaphore Lock" territory at that point.
You'd need "For Each..." or similar if you wanted to process the files inside a folder that got added, of course.
Thank you @Zabobon and @Nige_S for your help. Your suggestions will no doubt work, however, I am now getting stuck with the path for the open field in the 'Open a file, folder or application' action.
Thanks @griffman! I used the path from the 'Display Text' action and received the same error, which reads:
Open File failed with non-existent path “/Users/u8008006/Library/CloudStorage/GoogleDrive-aaron.timoshanko@gmail.com/My Drive/ScannerPro/%TriggerValue%”. Macro “OCR scan and compress pdf copy” cancelled (while executing Open “/Users/u8008006/Library/CloudStorage/GoogleDrive-aaron.timoshanko@gmail.com/My Drive/ScannerPro/%TriggerValue%”).
Could the issue be the space in My Drive? I tried to remove the space and test but apparently that is not possible.
I would try making a test folder on your Desktop and setting the Macro to watch that folder. Then drag a copy of your PDF Scan 10 Jul 2024 at 4.53 PM.pdf into that folder.
Also (for testing) change your open Action to open with Preview to check that the Macro is basically functioning. This will open the PDF in the Preview App. If that works, alter the Action to open with Readiris PDF 23
If that works (with the test folder on the Desktop) but you still can't get it to work with the folder in CloudStorage folder... I think the next thing I would try is putting a pause Action right at the start of the Macro. Start with a very long pause of say 5 seconds to see if it is some sort of timing issue with maybe CloudStorage delays in syncing.
The issue looks to be that, for some reason, the %TriggerValue% token isn't being processed. Can you post your actual macro (not an image of it) so people can poke at various options?
Otherwise, what @Zabobon said about testing -- particularly the bit about trying it for a folder on your Desktop, since cloud storage services can do "interesting" things with the file system in their attempts to make files appear to be there before they've fully downloaded, leaving stubs for online files, and so on...
Thank you to you both for these suggestions. After some playing around, it seems to be an issue with the third-party app that I use to capture images as pdfs and save it to a folder location (ScannerPro). The macro, attached, worked perfectly on desktop and even when I drag and dropped a pdf into the sample folder on Google Drive (so it can't be an issue with the space in 'My Drive').
It might be that I have to wait ~5-10 minutes for ScannerPro to finish uploading before trying to open it in Readiris.
It would need one bit of modifying, of course—the $KMVAR_local_theAddedFile should be a variable pointing to your downloaded file's name. And that path might be problematic in a shell script.
I wrote this because the "ignore partial files" setting doesn't always seem to work. This loop gets the file size from Terminal and saves it in a variable. It waits 0.3 seconds, then gets it again, and compares the new size to the old size. As soon as it's done changing, it exits the loop.
You get this a lot with "Watched Folder" triggers, so keep @griffman's solution to hand if you are going to use them.
It's because the OS-level event fires when the contents of the folder start to change -- there's no "has finished changing" to tag onto. KM's "ignore partial files" can handle many cases -- it "knows" that Safari appends ".download" to a downloading file's name and removes it on completion, for example -- but sometimes you have to roll your own check.
So, I tried to adapt @griffman's macro to my circumstances and I get the following error:
Is this what @griffman meant when he said "path might be problematic in a shell script"? OCR scan and compress pdf copy.kmmacros (10.3 KB)