Moving PDFs to a Selection of Different Folders Depending on Keywords in the File

Hello there KM users! I'd appreciate some help to know if a solution exists for the following dilemma (searched but didn't find anything). I want to develop a folder action that will monitor my Documents folder and and look for certain keywords in the files that are added to it and take action depending on the keyword found. All files will be in PDF format. Basically, if a PDF added to my Documents folder has the keyword "orange" in the content/text of the PDF, I want the PDF moved to a folder elsewhere that I have titled "Orange". If a file added to my documents folder has the keyword "purple" within the content/text of the PDF I want it moved to a folder elsewhere that I have named "Purple", etc.

Possible in KM? If not, is there a different tool to recommend?

Thanks in advance....

If there's a way to do this with Keyboard Maestro, I'm unaware. But this can be done with Hazel. I have it perform the types of actions you've described every day.

Thank you for the suggestion. I’ll check it out if KM just can’t do it. Anyone else know if Keyboard Maestro can manage this task as described?

Problem is, Keyboard Maestro can't read file contents.

Well -- there's not a native KM action for reading a PDF file's text content, but there is an action for reading a file:

Read a File action


It will read images and text, and it appears to read PDFs as images.

Nevertheless we can scrape the text out of a PDF with AppleScript:

# Auth: Christopher Stone
# dCre: 2015/09/27 19:15
# dMod: 2015/09/27 19:15 
# Appl: ASObjC
# Task: Convert PDF to Text
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Convert, @PDF, @Text
use framework "Foundation"
use framework "Quartz"
use framework "WebKit"
use scripting additions

my pdf2Text:"/Users/coffee/test_directory/pdf_test_files/Test_Doc.pdf"

on pdf2Text:thePath
   set theText to current application's NSMutableString's |string|()
   set anNSURL to current application's |NSURL|'s fileURLWithPath:thePath
   set theDoc to current application's PDFDocument's alloc()'s initWithURL:anNSURL
   set theCount to theDoc's pageCount() as integer
   repeat with i from 1 to theCount
      set thePage to (theDoc's pageAtIndex:(i - 1))
      (theText's appendString:(thePage's |string|()))
   end repeat
   return theText as text
end pdf2Text:

This was more work than I really wanted to do, but here's a simple working example.

Folder Trigger ⇢ Test.kmmacros (9.8 KB)

The sky is the limit.

BUT...  Hazel will probably be easier to use and configure.

I've always liked Hazel, but I've never bought a copy. I use Keyboard Maestro and AppleScript extensively and just don't want yet another utility on my system. Maybe when I finally upgrade to new tech...



I get that as it's the same reaction I have to many utilities. My typical thought process is, "That sounds good, but have I really missed much without that all these years?"

In defense of Hazel, I'm using older tech (late 2012 Mac mini and mid 2013 Air) on an older OS (10.11.6). On the mini, which is overdue for a restart (34 days uptime), the Hazel Helper process is using no CPU and just 8MB of memory. For how much the utility helps my organization, how much time it saves me, and how easy it is to configure, that's a good deal on my end. The Keyboard Maestro Engine, for comparison, is using about 1% CPU and 64MB of memory. Another good deal.

1 Like

Thank you for your help and proof of concept. After looking over your hard work.... I purchased Hazel. :wink: I would use this feature so much that it was an easy purchase just for that.