MZimmerman6
MZimmerman6

Reputation: 8623

Automator or Applescript, detect a new file in folder

I am trying to write something that will detect a new file in a certain specified directory. I preferably would like this script to continue running indefinitely, and whenever I see a new file, I can copy it and move it somewhere else. I know this has to be possible, because dropbox does it, but I just do not know how to get this working or where to start. Any ideas?

Upvotes: 2

Views: 7570

Answers (2)

adayzdone
adayzdone

Reputation: 11238

Here is another example of a folder action. You should save the script in ~/Library/Scripts/Folder Action Scripts .

    on adding folder items to theFolder after receiving theFiles

    -- This should make the folder action wait until large files have finished copying to the folder
    set fSizes to {0}
    repeat
        tell application "System Events" to set end of fSizes to size of theFolder
        if (item -1 of fSizes) = (item -2 of fSizes) then exit repeat
        delay 1
    end repeat

    -- If you want to do something with each file ...
    repeat with aFile in theFiles
        -- your code goes here  
    end repeat

end adding folder items to

Keep in mind that if your script saves anything to the target folder, the folder action will be triggered again.

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89559

What you're looking for (to spawn off Applescripts when adding new files into a "hot folder") is called "Folder Actions", and there are a number of tutorials that can be found online that should point you in the right direction.

For example, here is one: "AppleScript: Exploring the power of Folder Actions, part I"

Upvotes: 0

Related Questions