AppleScript running VERY slowly

lnoelstorr

Registered
OK, I wanted to wrtie an AppleScript that would allow me to send a dispatch email to people who had bought items from me through the Amazon Marketplace. What I wanted was a script that allowed me to select one or more emails, run the script, and it would reply to the selected emails with a dispatch note. I also wanted to copy the order details from the original email into the reply. Anyway this is what I came up with (the text added to the email has been trimmed down a bit here):

Code:
using terms from application "Mail"
	
	on perform mail action with messages selectedMessages
		
		set AppleScript's text item delimiters to ""
		
		repeat with eachMessage in selectedMessages
			
			tell application "Mail"
				
				set theReplyEmail to reply eachMessage
				
				set theOrderDetails to ""
				set startCopying to false
				
				repeat with eachParagraph in the paragraphs of theReplyEmail
					
					if eachParagraph begins with "Here are the details of your completed Amazon Marketplace sale:" then
						set startCopying to true
					end if
					
					if startCopying is true then
						set theOrderDetails to theOrderDetails & eachParagraph
					end if
					
					if eachParagraph begins with "Total Amount:" then
						set startCopying to false
					end if
					
				end repeat
				
				tell theReplyEmail
					
					set the subject to "Amazon Marketplace Order Dispatched"
					
					set the content to "Thank you very much ..." & theOrderDetails & return "Many Thanks..."
					
				end tell
				
				send theReplyEmail
				
			end tell
			
		end repeat
		
	end perform mail action with messages
	
end using terms from

Anyway, it currently runs slower than a legless tortoise running against the current in a river of treacle, and I'd really appreciate some suggestions to speed it up.

The bit that really slows it down seems to be the following:

Code:
set theOrderDetails to ""
set startCopying to false
				
repeat with eachParagraph in the paragraphs of theReplyEmail
					
	if eachParagraph begins with "Here are the details of your completed Amazon Marketplace sale:" then
		set startCopying to true
	end if
					
	if startCopying is true then
		set theOrderDetails to theOrderDetails & eachParagraph
	end if
					
	if eachParagraph begins with "Total Amount:" then
		set startCopying to false
	end if
					
end repeat

which basically extracts the order details from the original email so they can be copied into the reply.


I know there are obvious areas where the code could be improved (if I knew AppleScript better), like how the repeat loop continues after it's already done what it's going to do, but even considering that it just runs sooooooo slowly.

The equivalent written in Java would take no time at alll to run, even written in the inefficient way it is, what can I do to make this AppleScript faster?
 
Back
Top