I have a scanner that does batch operations. I want to have a script that send me a notification (simple email will do) that no more images have been added to a folder after a certain time limit - say 90 seconds.

Can someone help me with the basic structure? This machine is locked on OS X Lion for context.

  • 1
    Is this going to be started by a folder action? What have you done so far?
    – red_menace
    Nov 3, 2021 at 23:02
  • Did you implement what was in my answer and does it work for you? Dec 5, 2021 at 15:09

1 Answer 1


In macOS there are several built-in ways to watch a folder and have something automatically done when the contents of the watched folder changes.

  • The quickest and easiest way may be to use Automator to create a Folder Action workflow. However, in this use case I prefer the next method.
  • There is also Folder Actions Setup in which one can attach an AppleScript script to a watched folder and or also manage it and or a Folder Action workflow created by Automator. This is where this answer will focus for creating the Folder Action.
  • The least user friendly, without the use of a third-party utility, is manually creating a Launch Agent and using launchctl in Terminal to implement and manage it, and will not be covered herein.

Normally when watching a folder, the assigned action/script is triggered as the contents of the watched folder changes and it does its thing immediately and its done. It does not care if the Folder Action has not been triggered again to then do something else. As such, this presented an interesting look at how to e.g. send an email after e.g. 90 seconds from the time the last scanned document of the current scan batch is added to the watched folder, and essentially do nothing when documents are removed from it.

To that end, the solution presented herein has a few different working parts to get it set up. However, once its set up, its on autopilot thereafter...

  • AppleScript shell script triggered by the AppleScript script attached to the Folder Action.
  • Terminal to create the AppleScript shell script executable file.
  • TextEdit to edit the AppleScript shell script executable file.
  • AppleScript Editor to create the AppleScript script assigned to the Folder Action.
  • Folder Actions Setup, a GUI application to configure and manage the Folder Action as needed.
  • Mail to handle sending the email.

AppleScript shell script executable file

  • Create the AppleScript shell script executable file in Terminal.

  • In Terminal, run the following compound command:

    f='scandone'; touch "$f"; open -e "$f"; chmod +x "$f"

            • Hint: Copy and paste the compound command into Terminal, then press: enter

  • Then in the opened blank scandone document, in e.g. TextEdit, copy and paste the example AppleScript code, shown further below, into it.

  • Then make appropriate edits to the value of each property at the top of the script, then save and close the scandone document.

  • Note that by default the AppleScript shell script is created in the root of one's Home folder. As it will not be needed to run directly from Terminal it does not need to be in a location defined in the PATH passed to the shell. In OS X Lion I created a folder named bin within the root of my Home folder and moved the script there, even though it is not a binary file.

AppleScript script to attach to the Folder Action

  • In AppleScript Editor, create a new document adding the example AppleScript code, shown further below after the example AppleScript code used for the AppleScript shell script.

    • Setting '/path/to/scandone' to the actual POSIX path of the AppleScript shell script executable file created from the directions further above.
  • Save the AppleScript script as, e.g. Watch Scanning.scpt in ~/Library/Scripts/Folder Action Scripts noting that the ../Scripts/Folder Action Scripts folder does not exist by default. If it does not already exist from previous usage, then just open Terminal and run the following command:

    mkdir -p -m 0700 "$HOME/Library/Scripts/Folder Action Scripts"

            • Hint: Copy and paste the command into Terminal, then press: enter

Folder Actions Setup

  • Open Folder Actions Setup using one of the methods mentioned in the Notes: section below, starting at: To bring up Folder Actions Setup in OS X Lion

  • Add the watched folder and AppleScript script using the [+] button on each side of the Folder Actions Setup window. Adding the watched folder from the left side, and the AppleScript script, e.g. Watch Scanning.scpt, from the right side of the window.

  • On the Folder Actions Setup window, if the [] Enable Folder Action checkbox is unchecked, be sure to check it.

Assuming Mail is already configured with your email account and working normally, then you are all done and ready to start scanning. (Obviously do some testing first by just adding any old file into the watched folder, as if it was done by the scanner.)

How It Works

When a file is added to the watched folder the AppleScript script attached to the watched folder in Folder Actions Setup runs the AppleScript script, which in this case spawns the AppleScript shell script executable file and then quits waiting for the Folder Action to be triggered again.

The spawned AppleScript shell script executable file then counts the number of files in the watched folder, waits for the e.g. 90 seconds and then counts the number of files in the watched folder again. Then if the previous count equals the current count the AppleScript shell script sends an email via Mail, the GUI application, not the command line mail, otherwise the shell script just terminates.

Obviously every time the Folder Action is triggered it is going to spawn another occurrence of the AppleScript shell script, however, in my testing I did not see this as an issue. It also was the easiest way I could think of to achieve the goal.

The thing to keep in mind is that the total time the shell script waits in-between checking the file count obviously has to be more than the time it takes to scan the document and save it to the watched folder and also includes an appropriate amount of buffer seconds so the timing works.

AppleScript shell script executable file

Example AppleScript code:


property waitTimeSeconds : 90
property watchedFolder : "/path/to/WatchedFolder"
property toName : "John Doe"
property toEmail : "johndoe@domain.com"
property |Subject| : "Finished Scanning"
property |Content| : "Finished Scanning"
property quitMail : false

set previousFileCount to countFilesInWatchedFolder()

delay waitTimeSeconds

set currentFileCount to countFilesInWatchedFolder()

if previousFileCount = currentFileCount then
    tell application "Mail"
        set theMessage to ¬
            make new outgoing message with properties ¬
                {subject:|Subject|, content:|Content|}
        tell theMessage to ¬
            make new to recipient at end of ¬
                to recipients with properties ¬
                {name:toName, address:toEmail}
        send theMessage
    end tell
    if quitMail then
        delay waitTimeSeconds
        quit application "Mail"
    end if
end if

on countFilesInWatchedFolder()
    tell application "System Events" to ¬
        return count files of folder watchedFolder
end countFilesInWatchedFolder

AppleScript script to attach to the Folder Action

Example AppleScript code:

on adding folder items to
    do shell script "'/path/to/scandone' &>/dev/null &"
end adding folder items to

on removing folder items from
end removing folder items from


The example AppleScript code, shown above, was throughly tested in Script Editor (AppleScript Editor in OS X Lion) and as an AppleScript shell script and Mail under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

As the OS X Lion system I tested this on is not connected to a network, I could not actually send the outgoing message in Mail, however the basic instructions work the same as it did in macOS Catalina. Meaning sans actually sending an email in OS X Lion when the contents of the watched folder changed, it did everything else programmed to do.

To bring up Folder Actions Setup in OS X Lion, it's not as easy as in testing in macOS Catalina. In macOS Catalina, bring up Spotlight and starting to type Folder Actions Setup makes it available to click to open it. This is not the case in OS X Lion. In OS X Lion I use Terminal and ran the following command:

open -a 'Folder Actions Setup'

In OS X Lion, it is located at: /System/Library/CoreServices/Folder Actions Setup.app

In OS X Lion, Folder Actions Setup can also be opened from the Script menu on the menu bar if so configure in AppleScript Editor > Preferences… by checking the [√] Show Script menu in menu bar checkbox, then from the Script menu on the menu bar, Folder Actions > Configure Folder Actions opens Folder Actions Setup.

In OS X Lion, the predecessor to System Preferences > Security & Privacy > Privacy > Accessibility, in e.g. macOS Catalina, is System Preferences > Universal Access > [] Enable access for assistive devices. In my testing I did not need to have this checked. (YMMV)

In the example AppleScript code, if property quitMail : false is set true it will quit Mail after the same value of the value of waitTimeSeconds. Keep in mind that if the email address used for this Folder Action normally has a lot of traffic and Mail was not already opened, you may want to set the value for the delay command in the if quitMail then block manually and greater than the value set for waitTimeSeconds. This is to avoid a race condition between receiving mail and sending mail if the former occurs before the later. You obviously do not want Mail to quit before the message has been sent if a large amount is incoming slows down the outgoing message.

Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately..

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .