I downloaded this script, however the folder action fires the script before the file is finished writing (file being written from ftp server) I need the script to wait for the file to finish being written before sending the email. As written, most times only a partial file is emailed…
Thanks
on adding folder items to thefolder after receiving theAddedItems
repeat with eachitem in theAddedItems
set theSender to "Brian<xxx@xxx.com>"
set recipCommon to "Brian"
set recipAddress to "xxx@xxx.com"
set msgText to "Camera Alert"
tell application "Mail"
set newmessage to make new outgoing message with properties {subject:"CAMERA ALERT", content:msgText & return & return}
tell newmessage
set visible to true
set sender to theSender
make new to recipient with properties {name:recipCommon, address:recipAddress}
make new attachment with properties {file name:eachitem} at after the last paragraph
end tell
send newmessage
end tell
end repeat
I think this post is in the wrong forum. I see it in the Mac OS forum (all Mac OS releases prior to 10), but it probably belongs in the OS X forum (since you use Mail).
The adding folder items tofldafter receivingitms handler of Folder Action scripts is triggered as soon as a new item is added to the folder, without regard to whether the program that created the new entry has closed the file. That can mean that the Folder Action script is run before the file is complete if it is being written slowly (i.e. if it takes more time to completely write out the file than the system takes to launch the script).
The typical solution is to repeatedly check the size of the file or folder in question to see if it has stopped growing. Here are a couple of solutions from past threads (thanks, Adam!):
An IsSizeStable handler that loops until the size of the given file does not change over a 3 second period.
A folderReady handler that tools until the total size of the folder does not change over a 3 second period. This one will effectively check all of the files in the attached directory at once. If there are multiple overlapping file transfers, it may delay the processing of earlier files while later files finish transferring.
An example of doing IsSizeStable with inline code.