Applescript: monitor a folder and report new file
in previous article, I talked about how to expose the public folder even outside of your Mac, in modo da allow anyone, It was also at the North Pole, to access it.
well, suppose that this poor wretch connect, from the North Pole, your public folder (Shared), and load some important documents: how do you know that you have loaded a new file, unless they are advised by phone or email?
In our help comes to us Applescript, friendly programming language made in Cupertino, that is able to perform an action, when certain conditions.
L’idea, I our case, and:
- Monitors the shared folder, and signals the arrival of new files
- Exclude from the account hidden files (starting with .), used by OSX
- Expects the upload of the file is actually completed
- Play sound
- Mostra un pop-up, that warns of the file, if you have not heard the sound
We open the app “Script Editor.app”, and type these lines of code:
[bash]– Once "attached" to a folder, an audible alert will be played every time a new file is added’ created in the folder
on adding folder items to this_folder after receiving added_items
if name of (info for added_items) does not start with "." then
–> Wait until the folder does not change size and the file is loaded
repeat
set x to (info for this_folder with size)
delay 1
set y to (info for this_folder with size)
if x = y then exit repeat
end repeat
try
tell application "Finder"
do shell script "afplay ‘/System/Library/PrivateFrameworks/ToneLibrary.framework/Versions/A/Resources/AlertTones/tweet_sent.caf’"
display alert "New file in Public folder or on Airport USB"
end tell
on error
display dialog "Error occured"
return
end try
end if
end adding folder items to[/bash]
Salviamo lo script in ~/Libreria/Scripts/Folder Action Scripts/
We select the folder you want to monitor, click on the right button, Click your “Service->Actions Setup folder“, and select the newly created script.
If all went well, copying a file in the folder, you should hear a chirping, and it should see a panel like mine:
The possibilities are endless fun with Applescript. If you modify this script and you have suggestions on how to improve it, do not hesitate to leave a comment!