Email gets fired before transfer is complete

Hi All,

I’ve already gotten some fantastic help today, and then I realize I have yet another problem. I have looked around on this forum for an answer to what I am sure is very simple, but what I’ve tried hasn’t worked. I have a folder action watching a folder, and when a new file comes in it fires off an email to tell me the file is there. However, it sends the email before the file is finished coming in. These are really big files, so it can take an hour or so for them to complete their transition into this folder. I want the email to happen after it’s finished. Here is what I have tried:


on adding folder items to this_folder after receiving added_items 
	repeat with i in added_items
		set {Nm, Ex} to {name, name extension} of (info for i)
		
		tell application "Mail"
			set addrVar to "email@email.com"
			set subjectvar to Nm & " download complete"
			set alert_message to Nm & " Your file is ready! It has been added to folder " & name of (info for this_folder as alias) & "."
			set composeMessage to make new outgoing message with properties {subject:subjectvar}
			tell composeMessage
				make new to recipient at beginning of to recipients with properties {address:addrVar}
				set the content to alert_message
			end tell
			send composeMessage
		end tell
	end repeat

end adding folder items to

I am sure there has got to be an easy solution here, but I can’t seem to figure it out. Help is appreciated.
Thanks,
js

Hi,

take a look at this thread.
Hope it helps

Wow, thanks. That was almost too simple. Great thread; I hadn’t found that one and it’s very helpful!

Well, of course it was too simple. So here’s the situation: now I get 2 emails each time a file completes its transition to the folder with these actions on it. I am not sure if it has something with the delay command. When I was moving a whole folder full of files, the processing starts immediately on the next item, and so the folder script would not send the email as it was already checking the next file. This is not good, as I need an email sent each time every item gets complete. So I put in my first process (where the files are being moved) a delay of 45 seconds before it moves to the next item. This gives my folder script enough time to check the latest file and the fire off the email. Except now it sends two emails. Any idea why? They are identical, and it’s always 2 emails at exactly the same time that say the exact same thing. Here is the script:


on adding folder items to this_folder after receiving added_items
	-- insert this command after the cpu-eating command
	checkStableSize(added_items) -- change to correct variable name
	
	repeat with i in added_items
		set {Nm, Ex} to {name, name extension} of (info for i)
		
		tell application "Mail"
			set addrVar to "email@email.com"
			set subjectvar to Nm & " download complete"
			set alert_message to Nm & " has been moved successfully. It has been added to folder " & name of (info for this_folder as alias) & "."
			set composeMessage to make new outgoing message with properties {subject:subjectvar}
			tell composeMessage
				make new to recipient at beginning of to recipients with properties {address:addrVar}
				set the content to alert_message
			end tell
			send composeMessage
		end tell
	end repeat
end adding folder items to


-- Add this handler after main script.

-- Handler to wait until a file is fully loaded.
on checkStableSize(theItem)
	set F to quoted form of POSIX path of theItem
	set sizeThen to first word of (do shell script "du -d 0 " & F) as integer --coercing to integer fixes the quote problem
	repeat
		try
			delay 5 -- seconds (set to longer if needed)
			set sizeNow to first word of (do shell script "du -d 0 " & F) as integer --coercing to integer fixes the quote problem
			if sizeNow - sizeThen = 0 then exit repeat
			set sizeThen to sizeNow
		on error
			exit repeat
		end try
	end repeat
end checkStableSize