Apple Mail Rule Script - syntax issue

I cobbled together the Apple Mail script below from various sources. I would like to attach this script to a rule and have it activate for specific incoming messages seined in my rule. I have the following problem; the script will run if I select a message and manually apply the rule but will not apply automatically to an incoming message.

I am new to applescript and am sure the syntax is off slightly, I was wondering if someone on this forum could give it a once over and comment.

The script, will save the attachment of a message to a folder, then create a new email and send to a specific person defined in the script with the original senders name in the subject line, original subject of the original email and the body of the original email but not the attachment.

Any help would be appreciated.

Thanks,
Ray

using terms from application “Mail” -----parts of bij mail rule aanzetten
on perform mail action with messages theMessages for rule theRule -----parts of bij mail rule aanzetten

	tell application "Mail"
		
		
		-- set theOutputFolder to (here's where you tell it the main folder to save in)
		tell application "Finder"
			set theOutputFolder to folder "Macintosh HD 2:Users:foo:Documents:Attachments"
		end tell
		
		-- Select the email account to apply the script
		
		set theMessage to message 1 of mailbox "Inbox" of account "RTEST"
		
		-- Set all object selections
		
		set theSelection to selection
		set theSelectedMessage to item 1 of theSelection
		set theSelectedMessageSender to sender of theSelectedMessage
		set theSelectedMessageSenderName to extract name from sender of theSelectedMessage
		set theSelectedMessageSenderAddress to extract address from sender of theSelectedMessage
		set theSelectedMessageSubject to subject of theSelectedMessage
		set theSelectedMessageContent to content of theSelectedMessage
		set ourText to " *** "
		
		
		
		set theMessage to make new outgoing message with properties {visible:true, subject:"You've Got Pictures from *** " & theSelectedMessageSenderAddress & ourText & theSelectedMessageSubject, content:theSelectedMessageContent, reply to:"me@foo.com"}
		
		-- Specify the recipients of the email
		
		tell theMessage
			make new to recipient at end of to recipients with properties {address:"me@foo.com"}
			
			send
			
			repeat with theMessage in theMessages
				
				--does the message have attachments? if not, then skip it
				if (every mail attachment of theMessage) ≠ {} then
					
					--save the attachment to the subfolder
					
					set theAttachments to (every mail attachment of theMessage)
					repeat with theAttachment in theAttachments
						set thePath to (theOutputFolder as string) & (name of theAttachment)
						
						--if same named file already exists in subfolder, skip it   
						if not (exists (alias thePath of application "Finder")) then
							save theAttachment in thePath
						end if
					end repeat
					set read status of theMessage to true
				end if
				
			end repeat
			
			
		end tell
		
		
	end tell
	
	
end perform mail action with messages

end using terms from

Model: 10.6
Browser: Safari 534.57.2
Operating System: Mac OS X (10.6)

Hello.

I use this so I can see the log results in the console, when the script is running! It is interesting to watch the error messages, given by statements within the try - end try blocks. (in the on error section ) by calling my logit("Error: " & e & n,“my_Logfile”).
You must of course have formed the on error statment like this then: on error e number n

(*
I usually use my own handler to write to a log file, so I'm not at the
mercy of any particular script editor. For instance:
*)

logit("going into file", "loggerlog")



to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

This should point you in the right direction …

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		tell application "Finder" to set theOutputFolder to (path to documents folder as text) & "Attachments"
		tell application "Mail"
			repeat with aMessage in theMessages
				
				set {theSubject, theContent, theSender} to {subject, content, sender} of aMessage
				set {senderName, senderAddress} to {extract name from sender, extract address from sender} of aMessage
				set ourText to " *** "
				
				--If the message has at least one attachment
				set theAttachments to (every mail attachment of aMessage)
				if theAttachments ≠ {} then
					repeat with theAttachment in theAttachments
						set thePath to (theOutputFolder as text) & ":" & (name of theAttachment)
						if not (exists file thePath of application "Finder") then save theAttachment in thePath
					end repeat
					set read status of theMessage to true
				end if
				
				set theMessage to make new outgoing message with properties {visible:true, subject:"You've Got Pictures from " & senderName & space & ourText & space & theSubject, content:theContent, reply to:"me@foo.com"}
				
				tell theMessage
					make new to recipient at end of to recipients with properties {address:"me@foo.com"}
					send
				end tell
				
			end repeat
		end tell
	end perform mail action with messages
end using terms from