Automator or Applescript, detect a new file in folder

≡放荡痞女 提交于 2019-12-10 23:05:33

问题


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?


回答1:


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"




回答2:


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.



来源:https://stackoverflow.com/questions/11889992/automator-or-applescript-detect-a-new-file-in-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!