Strip attachment incoming mail and forward it using mail rule

I have been trying to write a scrip that will be triggered by a mail rule strip an attachment and forward it. Started with an Apple script to check that the script was trigged by the rule all it returned was the rule name and confirmation it had been triggered. That worked but when I added the name of the attachment to the display dialog it stopped working and before preceding I wondered if anyone can tell me what is wrong.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theSubject to subject of eachMessage
				repeat with theAttachment in eachMessage
					set attachmentname to name of attachment
				end repeat
				try
					set theRuleName to name of theRule
					set theText to "The rule named '" & theRuleName & "' matched this message:"
					set theText to theText & return & return & "Subject: " & theSubject
					display dialog theText & return & attachmentname
					set theText to ""
				end try
			end repeat

		end tell
	end perform mail action with messages
end using terms from

Thanks very much

Peter

Model: Macbook Air
AppleScript: 2.4.3
Browser: Safari 534.57.2
Operating System: Mac OS X (10.7)

Hi,

Mail rules don’t support user interaction.

Hi Stefan

I am dumb and not sure I understand the user interaction. what I am trying to do is when I receive an email with an attachment to create a new eamil and send it with the attachment to different but always the same person just received.

for the purpose of testing I am just sending the message to myself.

This scrip works with data I have set up and at this point all I was trying to do was get the subject form the incoming mail and set it as the subject of the new mail. You can see I have commented out the bits to active this. The rule checks to see who it is from in my test if I get it working I will also test for a word in the subject.

on perform_mail_action(thedata)
	
	set theSender to "mitchbvi@mac.com"
	set theSubject to "Tech Letter"
	set therecipient to "mitchvail@mac.com"
	
	--set theselectedmessages to |SelectedMessages| of thedata
	--set theSubject to the subject of theselectedmessages
	
	tell application "Mail"
		set themessage to make new outgoing message with properties {visible:true, subject:theSubject, sender:"thesender"}
		tell themessage
			make to recipient at end of to recipients with properties {address:therecipient}
		end tell
		send themessage
	end tell
	
	
end perform_mail_action

s many times before thanks for your help.

Peter

user interaction means commands like display dialog

I remember that creating and sending a new mail from a mail rule was broken, but I can’t remember the system version and whether it has been fixed later

Thanks Stefan

Interesting I use display Dialog to try and check where I get to in a scrip and I found I got a display after the prerom action but not when I moved into the tell block.

I will keep working thanks again

peter

Stefan

I emailed you directly on this but thought I should post here as well. My script is nearly working at least the one that follows does send a message and tell me if the message I received had an attachment. what I want to do is to add the attachment I received to the new email I am sending and I just cannot figure that out. this is the scrip.

-- 	Object of this script is to receive email get the attachment and add it to a new email and send it to another recipient.
--	As 08 01 2012 still does no have attachment
on perform_mail_action(thedata)
	set theSender to "mitchbvi@mac.com" --Addresses are for testing
	set therecipient to "mitchvail@mac.com"
	
	tell application "Mail"
		set theselectedmessages to |SelectedMessages| of thedata
		repeat with themessage in theselectedmessages
			set theSubject to subject of themessage
			set theSubject to theSubject -- of the incoming message
			set {year:y, month:m, day:d, hours:h, minutes:min} to themessage's date sent
			set theattachmentcount to count every mail attachment of themessage
			
			if theattachmentcount > 0 then
				repeat with thisattachment in themessage's mail attachments
					set theAttachmentname to name of thisattachment
					set sendattachment to thisattachment
				end repeat
				
				set body to "This is a test routine, and there is an attachment, message sent on " & m & d & y & " at " & h & " " & min & return & "Name of attachment is: " & theAttachmentname
			else
				set body to "This is a test routine, NO attachments, message sent on " & m & d & y & " at " & h & " " & min
			end if
		end repeat
		set themessage to make new outgoing message with properties {visible:true, subject:theSubject, sender:"thesender", content:body}
		tell themessage
			make to recipient at end of to recipients with properties {address:therecipient}
			make new attachment with properties {name:theAttachment, content:sendattachment} at after last paragraph
		end tell
		--tell content of themessage
		--make new attachment thisattachment
		--make new attachment with properties {file name:thisattachment} at after last paragraph
		--end tell
		send themessage
	end tell
	
end perform_mail_action

What happens is that he process hangs with an outgoing message open wit the text entered for a mail received with an attachment but no attachment is there.

Thanks

Peter

the proper syntax to create an attachment is


tell application "Mail"
	-- .
	tell themessage
		make to recipient at end of to recipients with properties {address:therecipient}
		tell content to make new attachment with properties {file name:theAttachment} at after last paragraph
	end tell
end tell

the variable theAttachment must be an alias specifier.

Hi Stefan

Must be doing something wrong and I think it is to do wit the “alias specifier” for theattachment . In the loop where get the attachment name I am also trying tot set theattchment.

repeat with thisattachment in themessage’s mail attachments
set theAttachmentname to name of thisattachment
set theAttachment to thisattachment
–set theAttachment to content of thisattachment
end repeat

I have set up the tell message as you suggest.

tell themessage
make to recipient at end of to recipients with properties {address:therecipient}
tell content to make new attachment with properties {file name:theAttachment} at after last paragraph
end tell
send themessage

As I said my apologies but I have tried all sorts of combinations and I assume I am not picking up the actual attachment although the mail it sends me has the correct name in the body. That is if it sends me an email which it does not do if I leave th eline that sets up the attachment as per your instructions.

thank you

Peter

I just noticed, that saving attachments in Mail.app on 10.7.4 works !!
So you could use something like this, it takes a selected message, saves the first attachment in the temporary items folder, creates a new mail, assigns the attachment and deletes the temporary item


set tempFolder to path to temporary items folder from user domain as text

tell application "Mail"
	activate
	try
		set theMessage to item 1 of (get selection)
		set theAttachment to mail attachment 1 of theMessage
		set filePath to tempFolder & name of theAttachment
		save theAttachment in file filePath
		
		set newMail to make new outgoing message with properties {visible:true}
		set content of newMail to "Content" & return
		tell content of newMail
			make new attachment with properties {file name:(filePath as alias)} at after last paragraph
		end tell
	on error e
		display dialog e buttons {"Cancel"} default button 1
	end try
end tell
do shell script "rm " & quoted form of POSIX path of filePath


Hi Stefan

Once again my thanks I will try and put it into my script and hopefully will no thane to bother you again.

One thing that occurs to me is that sort of feature could be useful to others is there anywhere one can post scripts that others can use. After all the help I have had from you it may well benefit others as well.

take care

Peter

Hi Stefan

Thank you this is now working I incorporated your last advice in tot what I had done . While fit works it looks very clumsy. In particular I had to keep my repeat loop on the messages when I know the rules is only operating on one message at a time. Any further advice greatly appreciated .

--Test Routine to extract an attachment from incoming email and send it as an attachment to another fixed recipient
--With assitance from Stefan
on perform_mail_action(thedata)
	set tempFolder to path to temporary items folder from user domain as text --creates path to /Users/user/Library/Caches/TemporaryItems/
	set theSender to "mitchbvi@mac.com" --Addresses are for testing
	set therecipient to "mitchvail@mac.com"
	tell application "Mail"
		set theselectedmessages to selectedmessages of thedata
		repeat with themessage in theselectedmessages
			tell application "Mail"
				activate
				try
					--set themessage to item 1 of | selectedmessages| of thedata
					--set themessage to item 1 of (get selection)
					set theAttachment to mail attachment 1 of themessage
					set filePath to tempFolder & name of theAttachment
					set theSubject to subject of themessage
					set {year:y, month:m, day:d, hours:h, minutes:min} to themessage's date sent
					save theAttachment in file filePath
					set newMail to make new outgoing message with properties {visible:true, sender:theSender, subject:theSubject}
					set content of newMail to "Letter Received on" & m & "/" & d & "/" & y & return
					tell content of newMail
						make new attachment with properties {file name:(filePath as alias)} at after last paragraph
					end tell
					tell newMail
						make to recipient at end of to recipients with properties {address:therecipient}
					end tell
					send newMail
				on error e
					display dialog e buttons {"Cancel"} default button 1
				end try
			end tell
		end repeat
	end tell
	do shell script "rm " & quoted form of POSIX path of filePath --deletes temporary file
end perform_mail_action

Peter