Run script every time there's a change in a folder?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi Everyone,

I'm sure running a command based on a change in a folder is a common question, but I can't seem to find it through my searching. I tried searching and looking through the manual but I'm probably just not clear on what to look up and would appreciate help or being pointed in the right direction.

I'm trying to have Hazel run an apple script every time there is any change in a folder (new file, file updated, etc..). The script then makes Devonthink reindex that folder. The apple script works on its own. I was able to get it to work within Devothink by making it an Automator workflow (for some reason pasting it as an embedded apple script resulted in errors). I should also mention that the folder has many subfolders.

In my attempts, I used the "any" of the following conditions are met option. With the following conditions:

sub-file/folder count - did change
size - did change
created - did change

Here's the Apple script incase anyone is interested

Code: Select all
-- Update indexed items of all databases
-- Created by Christian Grunenberg on Tue Mar 25 2014.
-- Copyright (c) 2014. All rights reserved.

-- Set to true if indexed items are not only located at the root of the database
property pAllRecords : false

tell application id "DNtp"
      set theDatabases to databases
      show progress indicator "Updating Indexed Items" steps (count of theDatabases)
      repeat with theDatabase in theDatabases
         step progress indicator (name of theDatabase as string)
         set theRecords to records of theDatabase -- Children of root
         repeat with theRecord in theRecords
            if pAllRecords or theRecord is indexed then
               synchronize record theRecord -- Updates children of theRecord too
            end if
         end repeat
      end repeat
      hide progress indicator
   on error error_message number error_number
      hide progress indicator
      if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
   end try
end tell
Posts: 1
Joined: Tue Nov 13, 2018 11:13 am

That won't be bulletproof in that it will miss certain changes, like if a file is edited but the size is maintained.

Check the manual on "syncing folders". There's a chapter on how to do this. While you are not trying to actually sync folder contents, the conditions for the rule illustrated there should align with what you are trying to do, which is to trigger anytime something changes in a folder or anything below it.
Site Admin
Posts: 8615
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Return to Support