Add email after monitoring folder

Hi all,

I am really really struggling to understand where i need to add tell application ‘mail’ event within this code. I am trying to monitor a folder for new files, then when received send an email. I have taken this code from a previous posting it this code partly correct to perform my request. I am new to apple scripting.
The code is as follows:

global dirToMonitorize, idleAmount, dirToMonitorizePP, detectedItems, queueItems, queueSizes


set dirToMonitorize to alias "myfolderpath"
set idleAmount to 2

to processItem(filespec)
	tell application (path to frontmost application as text) to ¬
		display dialog (name of (info for filespec)) & return & return & ¬
			"Finished: " & (time string of (current date)) with icon 1 giving up after 5
end processItem


set dirToMonitorizePP to POSIX path of dirToMonitorize
set detectedItems to list folder dirToMonitorize without invisibles
set queueItems to {}
set queueSizes to {}

on idle
	set itemLst to list folder dirToMonitorize without invisibles
	if itemLst is not detectedItems then addNewItems(itemLst)
	if queueItems is not {} then monitorize()
	return idleAmount
end idle

to monitorize()
	set itemsInProcess to {}
	repeat with i from 1 to count queueItems
		set itemToMonitorizeNewSize to get eof of ¬
			(POSIX file (dirToMonitorizePP & queueItems's item i))
		if itemToMonitorizeNewSize is queueSizes's item i then --> item stopped growing
			processItem(POSIX file (dirToMonitorizePP & queueItems's item i))
			set itemsInProcess's end to i
		else
			set queueSizes's item i to itemToMonitorizeNewSize
		end if
	end repeat
	
	--> remove items sent to process from queue
	set queueItems to strip(itemsInProcess, queueItems)
	set queueSizes to strip(itemsInProcess, queueSizes)
end monitorize

to addNewItems(itemLst)
	repeat with i from 1 to count itemLst
		if itemLst's item i is not in detectedItems then
			set detectedItems's end to itemLst's item i
			set queueItems's end to itemLst's item i
			set queueSizes's end to 0
		end if
	end repeat
end addNewItems

to strip(itlst, lst)
	script o
		property a : lst
		property nl : {}
		property b : itlst
	end script
	set o's b to o's b as list
	repeat with i from 1 to count lst
		if i is in o's b then
		else
			set o's nl's end to o's a's item i
		end if
	end repeat
	o's nl
end strip

Hi,

this is actually the classical case for using a folder action.
Change the data of the recipient, save the script in (~)/Library/Scripts/Folder Action Script and attach the script to the folder


property recipName : "Henry Smith"
property recipAddress : "henry@smith.com"

on adding folder items to this_folder after receiving these_items
	repeat with oneItem in these_items
		set theSubject to name of (info for oneItem) & " received"
		tell application "Mail"
			set newMessage to make new outgoing message with properties {visible:true, subject:theSubject}
			tell newMessage
				make new to recipient at end of to recipients with properties {name:recipName, address:recipAddress}
			end tell
			activate
			send newMessage
		end tell
	end repeat
end adding folder items to

Stefan thanks for the reply.

I have attached the script as a folder action, the email is sent before the file has been fully received. This is the problem i am trying to solve. Can a folder be monitored for new files then when a file has stopped growing in size, email that it has arrived?

Andy

try this


property recipName : "Henry Smith"
property recipAddress : "henry@smith.com"

on adding folder items to this_folder after receiving these_items
	repeat with oneItem in these_items
		if checkStableSize(oneItem) then
			set theSubject to name of (info for oneItem) & " received"
			tell application "Mail"
				set newMessage to make new outgoing message with properties {visible:true, subject:theSubject}
				tell newMessage
					make new to recipient at end of to recipients with properties {name:recipName, address:recipAddress}
				end tell
				activate
				send newMessage
			end tell
		end if
	end repeat
end adding folder items to

on checkStableSize(myFile)
	set f to quoted form of POSIX path of myFile
	set sizeThen to first word of (do shell script "/usr/bin/du -d 0 " & f)
	repeat
		try
			delay 2 -- seconds (set to longer if needed)
			set sizeNow to first word of (do shell script "/usr/bin/du -d 0 " & f) as integer
			if sizeNow - sizeThen = 0 then return true
			set sizeThen to sizeNow
		on error e
			if e contains "No such file" and (sizeNow - sizeThen = 0) then return true
			return false
		end try
	end repeat
end checkStableSize