"Watched folder" and selecting file for processing in 3rd party app

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).

Any advice or guidance is much appreciated.

You can do this with Keyboard Maestro Actions by breaking the task down into three parts:

  1. Make the Macro trigger when files are added to your "Watch Folder". This is the part you have already worked out how to do.

  2. 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.

  1. 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.

EXAMPLE Open Latest PDF Added to Watched Folder.kmmacros (5.8 KB)

Click to Show Image of Macro

2 Likes

Isn't the path to the added file already available via the %TriggerValue% token, making the first "For Each..." unnecessary?

What am I missing?

2 Likes

Yes - of course you are absolutely right! Which means the Macro can be simplified to just one "For Each Action"

EXAMPLE Open Latest PDF Added to Watched Folder v2.00.kmmacros (4.8 KB)

2 Likes

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.

1 Like

Sorry, typo - I meant one "If Then" Action as I put in my example...

2 Likes

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.

I have tried ~/Desktop/untitled folder/%TriggerValue% and just %TriggerValue% but both say there is an error with the path.

What value is the 'Open' field requiring that will point to the file that is the %TriggerValue%?

Try just putting a Display Text in there, with %TriggerValue% as its only contents. What does that say the path is?

-rob.

1 Like

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.

Can you just post the path that's displayed by the Display Text option?

-rob.

1 Like

/Users/u8008006/Library/CloudStorage/GoogleDrive-aaron.timoshanko@gmail.com/My Drive/ScannerPro/Scan 10 Jul 2024 at 4.53 PM.pdf

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.

1 Like

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...

2 Likes

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.

OCR scan and compress pdf copy.kmmacros (3.1 KB)

Thanks again!

There are some ways to wait for the file to actually be done copying before the macro triggers—I use a loop like this in a few of my macros:

Group Action (v11.0.3)

Group.kmactions (4.6 KB)

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.

-rob.

2 Likes

Thanks, Rob - that's a novel solution!

As you correctly pointed out - the issue was a timing one. The 5 min pause means the macro is working now.

Thank you all for your help!

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.

Or whack in a big pause :wink:

1 Like

Thank you @Nige_S - good advice.

Much appreciated all.

So, I tried to adapt @griffman's macro to my circumstances and I get the following error:
image
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)

p.s. the 'display text' is just so I know something is happening.

Go to that folder in Terminal, and then type pwd -- does the output match the path to the folder as stored in the variable?

-rob.

1 Like