Need help cleaning up... Auto Responder on Steroids

After following the Posting Guidelines, I’ve already searched for hours for code I could possibly borrow and twist to my needs. I’ve also thought it through the best I can not understanding AppleScript and Mail.app better.

Here’s the Big Picture:

After a total of four days (over two weeks), I’ve built a very simple Mail.app rule wrapped around two Automator applets (yes I know this is the Applescript OS X Forums… keep reading) which handles the following and works… it REALLY WORKS:

Automator Applet #1 - “Email to Text”
– Creates a text file from body of email (from an online form)
– Filters it twice due to what I believe to be (I really don’t know) invisible ASCII characters (¸Ë‡) handed over by the online form which errors out the merge. So I scripted a very rude section that selects text from the bottom of the body upwards to beginning of needed text, cuts and pastes into second plain text file.

Automator Applet #2 - “Merge and Create PDF”
– Merges second plain text file into a document and creates a PDF
– Creates and sends a New Email Message with PDF attached (currently recipient is hard coded)
– Finished up with some house cleaning (to prevent errors on next processing and just because I’m “that way”)

I’ll spare everyone the part where I almost hurt myself dancing around the room when this dream actually worked. Unfortunately, after testing it I realized it needs to be “fail-safe”. And I also realized that most, if not all of the above could be done as one long AS.

The major issue is that if more then one new message arrives at a time, the body of all new messages are combined into the first plain text file, which means that only the last new message (since the filter starts at the bottom of the text and moves up) is merged and the balance is ignored. This also means that the first new messages will receive a reply with the last new messages info and the others will not be processed at all.

Understand so far?

To simplify this I believe an Auto Responder script would work best. This would also correct the fact that the current New Email Message’s Recipient is hard coded into the script and I’d prefer the script have more flexibility.

With that said…
I’ve already borrowed an Auto Responder script:
http://email.about.com/od/macosxmailtips/qt/et022106.htm
… and wedged the Automator applets into it. And as I found out… it’s not that easy. The text file written is empty and the Automator applet runs but the PDF is never attached to the Auto Responder’s reply.

I also borrowed a Save Mail to Text script:
http://bbs.applescript.net/viewtopic.php?id=17655
… but switched over to Automator to handle this (see above ASCII issue)

So to recap… simple AS wrapper around two Automator Applets WORK… but…
I’d appreciate ANY help in creating a script in whole or in parts that:
– Loop for processing one new email message at a time
– Holds onto the senders email address
– Creates a clean plain text file of message (if not, I already have work around for this one)
– Run my Automator applets
– Replies to above sender with attached PDF
– And finally moves processed email to “Follow-Up” mailbox and tags read and flagged
– Repeat until no new email messages

And lastly… delays… each applet takes between 5-8 seconds to process.

I know it may look as if I’m asking alot… and I know I am, but did I mention IT REALLY WORKS? And basically, I looking for a very clean wrapper for my Automator Applets.

Thanks in advance, and thank you so much to everyone posting their code…I’ve learned faster by reviewing and play, it really helps!


I can’t code… but I can dream.

Model: Model: 1GHz G4 (12" PowerBook)
AppleScript: AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi.

The autoresponder script you found will repeat through the messages that meet the rule you set.

To add a delay to that script, you can do this:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				
				(* do your thing *)
				
				delay 10 --pauses 10 seconds before going to the next message
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Also, I seem to recall reading that using TextEdit for the problem text will help with the invisible characters so you can merge, but I can’t remember where I read that. Is that what you are doing with Automator?

It might be a good idea describe your mail rule and list all the steps in your Automator applets to make clear exacly what you want to do with AppleScript.

j

capitalj,

Thanks for responding!

As I mentioned in my original post… the Auto Responder I found (and linked to) didn’t process as needed. Or should I really say, I tried it but I’m not sure where exactly I should add the code to run the two Automator Applets.

You asked for it…

---- Description of Mail Rule ----
– if any message with subject “online form submission”
– move to “online form” mailbox and run "online_form.scpt

---- Steps in Automator applets ----
Automator Applet #1 - “Email to Text”

– Run AppleScript (house cleaning – finds and trashes all previous files)
– Get Selected Mail Items
– Copy to Clipboard
– New Text File named “temp.txt” (created on desktop)
– Open Finder Items
– Run AppleScript (this is the text cleaner which selects from the bottom of the body upwards… see ASCII issue from original post)
– Hide Mail
– New Text File name “master.txt” (created on desktop
– Open Finder Items
– Run Applescript (this pastes above “cleaner” as into master.txt)
– Run Applescript (more house cleaning (just to be on the save side)

Automator Applet #2 - “Merge and Create PDF”
– Get Specified Finder Items (Quark document)
– Open Finder Items
– Run AppleScript (this merges “master.txt” into Quark document – this also has some added UI code to get the process completed)
– Create a PDF of above document
– Rename above sequentially
– Render above file as image (200dpi JPG)
– New Mail Message
– Add Attachments to Front Message
– Send New Message
– Run AppleScript (more house cleaning)

Hi.

The goal is to replace Automater with AppleScript, but I’ll look at that, too.

A couple more questions.

Do you clear out the “online form” mailbox or something else? And after the PDF is attached, does it get kept or trashed?

Is it always the same document?

I have no experience with Quark so hopefully somebody else will have an answer for that part, but I should be able to help get a script started. I’m a little busy right now, but I’ll try to get back later today.

j

Um, maybe tomorrow.

j,

Lets backup a little. Going back to the original post… the two Automater Applets work and I’m looking for a Mail.app wrapper. Don’t get me wrong, I’ll switch to a complete AS once I get the whole process working properly. That way I can reverse engineer each section at a time a learn what I’m actually doing. Plus… I’ve worked so hard to get what I have working, I don’t what to take 10 steps back.

To answer some of your questions:
I very reluctant to move the first applet back into AS due to hours of testing and frustration with the ASCII character issue. The majority of my testing was done inside of TextEdit and selecting different file formats to “save as” that would work inside the second applet. I was finally able to see the invisible characters after using TextWrangler. The current method works and I really don’t need the extra stress of fighting this again. Unless you know how to eliminate the ASCII characters from the source. This project is consuming me and all my free time.

The “house cleaning” does trash all unnecessary files. And yes, all the files in the whole process are the same name (i.e. temp.txt, master.txt, master_output.qxd, master_output.eps, master_output.pdf, master_output.log). The only exception is the pdf which is renamed to, “requested_info_xxxx.pdf” (the xxxx is a sequential number). I have an idea for a work-around for the numbering that would be handed down from the online form.

Also the “online order” mailbox is not cleared out and all items are not deleted. All email should go into a “follow-up” mailbox.

Moving the Quark/Merge process into AS is fine… later… much later.

Just to double-check… where in the code below do I add my applets?

using terms from application "Mail"
	on «event emalcpma» theMessages given «class pmar»:theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theName to «event emaleafn» sender of eachMessage
				if exists reply to of eachMessage then
					set theAddress to reply to of eachMessage
				else
					set theAddress to «event emaleaua» sender of eachMessage
				end if
				set newMessage to make new outgoing message
				tell newMessage
					make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
					set visible to false
					set subject to "Re: " & subject of eachMessage
					set content to "###enter message content here###"
				end tell
				send newMessage
				tell eachMessage
					set «class isrp» to true
				end tell
			end repeat
		end tell
	end «event emalcpma»
end using terms from

Hi Ryan.

I went back to your original post. Seems I missed this line:

which lead to the confusion.

So…

That code didn’t compile for me and I don’t have time to figure that out now so I’ve simplified it for now. Also, your Applets already do some of the same things.

I haven’t had time to test this, but try it and post back:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				
				tell application "Applet 1" to activate--Replace "Applet 1" with the name you gave your Automator App
				
				delay 10--give Applet 1 time to run
				
				tell application "Applet 2" to activate
				
				delay 10--give Applet 2 time to run before moving to the next message
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Your applet creates a new message, but you want to use Applescript to put in the “To” address, correct? It looks easy enough to do without altering Applet 2 much (switch send message to the script,) but I haven’t actually tried it. If the above works properly, all do that next.

j,

Sorry… I should have clarified that the code I displayed on my previous post was the code I found (link on my very first post) for the Auto Responder. It contains some to the parts I need (i.e. Sender and Mark As Read).

I used your posted code and it does cycle as needed. I tried adding the applets to the “SimpleAutoResponder” code before I started this new topic post and it didn’t work properly… so thank you!

Also, let me correct myself… I’m simply reluctant to switch some of the parts inside the Automator Applets over to Applescript due to my personal frustration with the time consumed building them. This is my own fault for not researching and learning Applescript first. But… if a “non-frustrating” solution is developed from this (or any other post), I’ll do my best to be opened minded and at least give it a try.

Thanks for your help! It is very appreciated.

I’ll hopefully have more feedback for you later this evening.

Ryan


j,

I just noticed that the code I posted (in previous post) has some odd parts I didn’t add.

Please use this link
http://email.about.com/od/macosxmailtip . 022106.htm
to download clean version of “SimpleAutoResponder”.

I’m glad progress has been made.

I’d be reluctant to change what works, too. Besides, I don’t (yet) know how to script some of the functions of your applets.

I don’t know how to pass an AppleScript variable like the address to Automator. I’ll have to see how or if it can be done.

For now, I was thinking that the easiest way (from my end, since I don’t have experience with Quark and can’t reproduce your situation exactly) to pass the address to the emails would be to paste them in, which would require the outgoing message to briefly be visible, so the “send message” step would have to be removed from Applet 2 and since the last step is a run AppleScript that could (and might have to be, depending on what it is) be taken out of the applet and moved into the script.

Or maybe a new message can be scripted invisibly before Applet 2 runs, and the “new message” step taken out - the “add attachment to front message” should still work, I think.

If neither of those ideas work it would mean more changes to (or elimination of) your applets, so we’ll try to avoid that for now.

I thought that processing the e-mails would mark them read, if not that can be added.

I just downloaded the responder script; I should have more done tomorrow.

j

j,

It works! It processes one email at a time.

now onto…

… I’m ok with this!

As for marking each email as read… I already copied it from SimpleAutoResponder and added it.

Thanks again!

Ryan

I’m looking at better ways of doing this, and can’t test it, but building on my previous post:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				
				tell eachMessage--corrected typo (thisMessage instead of eachMessage)
					set replyTo to sender
				end tell
				set newMessage to make new outgoing message
				tell newMessage
					set visible to true
				end tell
				tell application "System Events" to keystroke replyTo
				
				
				tell application "Applet 1" to activate--Replace "Applet 1" with the name you gave your Automator App
				
				delay 10 --give Applet 1 time to run
				
				tell application "Applet 2" to activate--remove the new message step
				
				delay 10 --give Applet 2 time to run before moving to the next message
				
				send newMessage
				
			end repeat
		end tell
	end perform mail action with messages
end using terms from

So see how that goes.

To test within the editor, which might be easier, select a message and use this:


tell application "Mail"
	set thisMessage to item 1 of (get selection)
	
	tell thisMessage
		set replyTo to sender
	end tell
	set newMessage to make new outgoing message
	tell newMessage
		set visible to true
	end tell
	tell application "System Events" to keystroke replyTo
	
	
	tell application "Applet 1" to activate--Replace "Applet 1" with the name you gave your Automator App
	
	delay 10 --give Applet 1 time to run
	
	tell application "Applet 2" to activate
	
	delay 10 --give Applet 2 time to run before moving to the next message
	
	send newMessage
	
end tell
end

When I try the script as a rule… nothing happens.

When I try the running the script from within Script Editor, I get…

And when I remove both of the applets and their delays…
I get a new message with no recipient.

Did you run the one that begins with “using terms from…” from the editor? That won’t work. The other doesn’t have the variable “theMessages” - but I made a mistake anyway. I left out a line.

I commented out a bit so it would work for me. Select a message and try this. And also try it with the applets uncommented. I’m hoping that if you remove “new mail message” from Applet 2, “add attachments to front message” with work on the scripted new message. You might have to take “hide mail” out of Applet 1. I’m trying to avoid changes to the applets, because they work.



tell application "Mail"
	activate--this was in the version I meant to post
	set thisMessage to item 1 of (get selection)
	
	tell thisMessage
		set replyTo to sender
	end tell
	set newMessage to make new outgoing message
	tell newMessage
		set visible to true
	end tell
	tell application "System Events" to keystroke replyTo
	
	
	--tell application "Applet 1" to activate--Replace "Applet 1" with the name you gave your Automator App
	
	delay 10 --give Applet 1 time to run
	
	--tell application "Applet 2" to activate
	
	delay 10 --give Applet 2 time to run before moving to the next message
	
	--send newMessage
	
end tell
end

It’s easy to switch this back to working from a rule, but it’s easier to test this way.

j

j,

In the previous code (before this lasest one) I ran both scripts… both ways, just to see if anything worked.

As for the lastest…
From withing Script Editor with one message selected it works.

As a stand alone script called by a rule inside Mail.app… it does not.
It processes one email (which didn’t even match the rule) and did not process any others.

The new message part does work and my second applet handed the created file over to it.
(yes I disabled all the Mail steps inside the second applet except “Add Attachment”.)

Was there a reason for removing the “repeat” line?

Ryan

The reason for the changes and strange behavior is that it’s a test script that was meant to run from the editor, acting on a single selection.

That’s good news, I was afraid it wouldn’t. So back to a rule, keeping the changes you made to your applets:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		tell application "Mail"
			activate --this was in the version I meant to post
			repeat with eachMessage in theMessages
				
				tell eachMessage--corrected when I edited this post (was thisMessage, a remnant of another version)
					set replyTo to sender
				end tell
				set newMessage to make new outgoing message
				tell newMessage
					set visible to true
				end tell
				tell application "System Events" to keystroke replyTo
				
				
				--tell application "Applet 1" to activate--Replace "Applet 1" with the name you gave your Automator App
				
				delay 10 --give Applet 1 time to run
				
				--tell application "Applet 2" to activate
				
				delay 10 --give Applet 2 time to run before moving to the next message
				
				--send newMessage
				
			end repeat
		end tell
	end perform mail action with messages
end using terms from

[b]EDIT: I just fixed a typo in the script that I just realized that I also made in the first script of post #11. I’ll go fix that next.

And I came back to fix another typo. Hopefully I caught them all. [/b]:rolleyes:

j,

ok…
I’ve setup your latest code as a rule inside Mail.app.
The actual Auto Responder part works!!! The new message is created and when the applets finish up, the new message contains an attachment and sends.

Cycling thru each message one at a time… does not. The latest code processes only one message and stops.

Ryan

Hi Ryan.

Good news.

Bad news.

I’m probably making an embarrassingly obvious mistake. It’s getting late, so if I don’t post back soon it’s because I decided to start fresh in the morning

j

How about switching this


tell application "Mail"
           activate
           repeat with eachMessage in theMessages

to this


tell application "Mail"
			repeat with eachMessage in theMessages
				activate

What I’m thinking is that when the applets launch, mail is no longer frontmost. Maybe that causes a problem. Putting “activate” in the repeat will make Mail frontmost for each message.

Telling the applets to activate could be switched to telling them to launch, but I don’t know how that will affect them.

I’m also trying a few tests, but I’m a little bleary-eyed so this might be my last post tonight.

first… i think that may have done it! I’m going to delete and empty all messages and mailboxes, go online to my form and submit several times.

second… GO SLEEP!

thank you j!

I’ll post a more detailed response tomorrow.

Cool.

I was getting good results with my tests, but I also moved my dektop folder instead of a script into the rule actions folder, so it’s definitely bedtime.

j