I thought this was gonna be simple.... [Mail rule; find/replace]

For an old fart who worked with punch cards on an IBM 1620 and retired after writing assembly level source code for Motorola’s 6809 chip, this ‘simple’ scripting stuff is embarrassingly exasperating.

I’ve set a rule in Mail that invokes a script when it finds the text string “Part #” in the message (so far, so good). Here is a sample of what I’m typically working with.

I need to perform a replace operation in the number string (‘1’ becomes ‘v’, ‘2’ becomes ‘j’, etc.) and then generate a reply to the address listed above it. The messages are forwarded from a third party, so I can’t just do an auto-reply in Mail.

For the first part, I’ve tried variations on:

using terms from application "Mail"
	on perform mail action with messages for rule XYZ
		searchReplace(content, "4", "p") -- I've also tried messageContent as the text string
	end perform mail action with messages
end using terms from

…and I’m not even close to extracting the eMail address.

So now I’m reduced to begging… And hitting the tip jar.

Thanks in advance for your patient counsel.

Lee

Model: Mac tower, 2x 1.25
AppleScript: 1.10.7, editor 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

First a note, I have never tried scripting Mail before so this prolly isn’t in full working order, but I based it off some saple scripts from Apple so it should give you a jumping board to work from. hopefully =)

(*
	Assumes incoming messages formated as follow
	
	==========================
	Name: Sam [optional MI Last Name]

	Email: Sam@SamsClub.com

	Part #: 123456
	==========================
	
*)

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theContents to content of eachMessage
				set theName to words 2 through -1 of paragraph 1 of theContents as string
				set email to words 2 through -1 of paragraph 3 of theContents as string
				-- set the subject line to whatever you like
				set theSubject to "Order from " & theName
				set part to word 3 of paragraph 5 of theContents
				set part to srch_rep(part, "1", "v")
				set part to srch_rep(part, "2", "j")
				-- set message body to whatever you like
				set theBody to part
				-- set the sender address to whatever you like
				set theSender to "apple@pretendco.com"
				tell application "Mail"
					set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
					tell newMessage
						-- Default is false. Determines whether the compose window will
						-- show on the screen or whether it will happen in the background.
						set visible to true
						set sender to theSender
						make new to recipient at end of to recipients with properties {name:theName, address:email}
					end tell
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on srch_rep(dataStr, fndStr, repStr)
	set TID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {fndStr}
	set tmpStr to text items of dataStr
	set AppleScript's text item delimiters to {repStr}
	set dataStr to tmpStr as string
	set AppleScript's text item delimiters to TID
	return dataStr
end srch_rep

[EDIT] Put the create new message where it belongs /hand & face

Hi James,

may I suggest a easier routine to replace the numeric characters (assuming there are only numeric characters)


...

set part to ""
repeat with i in (get characters of word 3 of paragraph 5 of theContents)
	set part to part & character ((i as integer) + 1) of "xvjlpurwfq" -- characters corresponding to 0-9
end repeat
...

Thanks to both replies.

I’ve been away from the keyboard for a while and haven’t yet tested the suggestions - but I really, really appreciate the feedback.

Lee

It may be a kludge, and certainly could be more elegant, but here is what I ended up with. More to the point, it works.

The message text is generated by my ISP when people wish to join our VIP mailing list.

using terms from application “Mail”
on perform mail action with messages theMessages for rule theRule
set TID to AppleScript’s text item delimiters – saves old ones
– message is formatted like this:
–"
–Name: Lee Earle

–Phone: 123-425-5989

–Email–: info@mentalismUnlimited.com

–Serial #: tnmrlgkfyz"

	tell application "Mail"
		repeat with eachMessage in theMessages
			set theBody to content of eachMessage

			-- strip out "Email--: "
			tell application "TextCommands"
				set part to search theBody for "Email--: " replacing with ""
			end tell
			set hisEmail to sixth paragraph of part as string

			-- strip out "Name: "
			tell application "TextCommands"
				set part to search theBody for "Name: " replacing with ""
			end tell
			set hisName to second paragraph of part as string

			-- strip out "Serial #: "
			tell application "TextCommands"
				set part to search theBody for Serial #: " replacing with ""
			end tell
			set theSerial to fourth paragraph of part

                 end repeat
	end tell
	set AppleScript's text item delimiters to TID -- restores old ones
end perform mail action with messages

end using terms from

I appreciate the input that got me started on the right path.

Thanks,

Lee