I need an AppleScript application on the desktop to watch a folder, also on the desktop, for files that have been moved to it.

So far, I have:

global folderSpool
tell application "Finder" to set folderSpool to folder "Spool" of desktop

on adding folder items to folderSpool after receiving the_files
    repeat with each_file in the_files
        set theDialogText to "File is: " & (each_file) & "."
        display dialog theDialogText
    end repeat
end adding folder items to

When I export the script as an application, I set it to: "Stay open after run handler." I then open the application and keep it running.

However, when I move files to the "Spool" folder, nothing happens.

I tried the on idle option, but it kept creating dialogue after dialogue, rather than doing so when files were moved to the folder.

Any ideas?

  • 1
    Is the any reason why you can't just set up a Folder Action on the target Folder you want to watch? Oct 21 '17 at 13:46
  • At least two reasons: 1. I don't know how; 2. if it involves creating additional items and placing them in folders (which is what I suspect to be the case), then it's not practical given the usage we have in mind. Oct 21 '17 at 13:49
  • 1
    I do not believe you can watch a Folder from a plain AppleScript application and this will need to setup as a Folder Action. You can create a Folder Action and apply it to the target Folder two different ways natively, using an Automator Folder Action workflow or using Script Editor and Folder Action Setup... from the Services context menu in Finder. Both methods have been shown how to do it here on AD and on the Internet. Search AD and or Google using: Folder Action or Folder Action Setup Oct 21 '17 at 13:55
  • 1
    Also see Folder Actions Reference in the AppleScript Language Guide Oct 21 '17 at 14:03

The command you're using, "on adding folder item", is specific to folder actions, so it won't work in a non-folder action script. I agree with the others that a folder action is the best way to handle it, but if you really want to do it without, you can create a stay open application that queries the contents of the folder and compares it to the last time it ran, and displays the different files. Change the runInterval property to run at your preferred frequency.

Note: For simplicity, I only wrote it to check for files added to the spool folder. You can tweak it to also check for removed files by duplicating the second section in the code below, and looping through spoolFiles and seeing if they are in currentFiles.

AppleScript Code, as a stay-open application, below:

property spoolFiles : {}
property currentFiles : {}
property runInterval : 5 -- interval is in seconds
global folderSpool

on idle
    tell application "Finder"
        set folderSpool to folder "Spool" of desktop
        if (count of spoolFiles) is 0 then set spoolFiles to name of every file of folderSpool
        set currentFiles to name of every file of folderSpool
    end tell

    set changedFiles to {}--Reverse and repeat this section to check for missing files
    repeat with i from 1 to count of currentFiles
        if currentFiles's item i is not in spoolFiles then
            set changedFiles's end to currentFiles's item i
        end if
    end repeat

    repeat with each_file in changedFiles
        set theDialogText to "File is: " & (each_file) & "."
        display dialog theDialogText
    end repeat
    set spoolFiles to currentFiles --important to update the spoolFiles so only new files will get displayed going forward.
    return runInterval
end idle

You must log in to answer this question.

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