Applescript: monitor a folder and report new file | Flavio's blog
 

Applescript: monitor a folder and report new file

in OSX | | 0 Comments » | Last modified:

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:

-- Una volta "attaccato" ad una cartella, verra suonato un avviso sonoro ogni volta che un nuovo file verra' creato nella cartella
on adding folder items to this_folder after receiving added_items
    if name of (info for added_items) does not start with "." then
        
        --> Aspetta che la cartella non cambi dimensione ed il file sia caricato
        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 "Nuovo file in cartella Pubblica o su Airport USB"
            end tell
        on error
            display dialog "Error occured"
            return
        end try
    end if
end adding folder items to

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:

Alert: new file in the folder

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!

 

0 Comments »

Please accept third-party cookies to be able to comment on the post! The CHANGE COOKIE CHOICES button is located in the footer of the site. / In order to comment this post, please accept the third party cookies! The button CAMBIA LE SCELTE DEI COOKIE is in the footer of the website.