Help with string parsing - Mail script for Mac Mail Rules

Hi -

First, let me thank the admins for allowing me into this group. It’s exciting to find such a great group of experts supporting newbies as we all make the most out of the automation tools available in macOS! It’s fine if someone can answer my questions, but I am also totally happy to go research things myself if someone can point me in the right direction. Most of the content I’ve found for AppleScript (including books) goes back a decade or more and aside from forum posts, very little is even as recent as 2014.

Here’s what I’m doing:

  • Process incoming email from particular senders with particular subject lines, as these are appointment notifications from an appointment scheduling plugin and our ability to customize the subject line output is very limited, as is our ability to route these emails to multiple people. In this environment, there is a primary mail recipient and at least one backup recipient to make sure someone is available to conduct a “tour” of a facility for lease.

I’ve run into a couple of problems:

  1. I want to add the text content from a couple of lines (these begin with a field name followed by a colon and the field contents). Unfortunately, these fields are being read in as lines from paragraphs of text in the incoming message.
  2. I can add just one of the lines to the subject, but I am unable to find instructions on how to remove the linefeed (or is it just a return? sorry - I’m dated back to ASCII codes for CR/LF).

I’m pasting the code below, in hopes someone can direct me to how to do this.

As you can guess, the subject line is constructed as "Booking " with the "Customer name: " line added on, resulting in something like “Booking Customer name: John Smith” as the subject. I’d also like to add the contents of the line that begins “Date” as well, but when I do that, the subject becomes blank.

Additionally, I’d like to append the entire incoming message to the text “FORWARDED FROM TOUR BOOKING SYSTEM” & return – so that it’s clear to the recipients that the message is forwarded.

So my two questions are:

  1. How can I strip the text of these two lines (customer name and date) to add to the subject?
  2. How can I append the text of the original message to the text I specified above?

I have a few debugging lines in there so I realize there are some changes that I’ll make once I resolve this issue. The scripts will be attached to existing rules that simply forward emails based on the current subjects (which are not distinct enough - they thread all messages for a particular location in gmail together).

Thanks in advance for any suggestions or links anyone can provide to help me solve this!

Christopher

Model: MacBook Pro
Browser: Safari 602.2.14
Operating System: Mac OS X (10.10)

If I understand well your questions, you may try to use:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

using terms from application "Mail"
	on perform mail action with messages messageList for rule aRule
		
		tell application "Mail"
			repeat with eachMessage in messageList --    your code goes here...
				set newSubject to "Booking "
				set nameForSubject to ""
				set newForSubject to ""
				set newMsgText to "FORWARDED FROM TOUR BOOKING SYSTEM" & return
				set msgText to content of eachMessage
				repeat with eachline in (paragraphs of msgText)
					if ((eachline as rich text) starts with "Customer name:") then
						set nameForSubject to my clearLineBreaks(eachline) -- EDITED
					end if
					--if ((eachline as rich text) starts with "Date") then
					--    set nameForSubject to nameForSubject & " " & eachline
					--end if
				end repeat
				display dialog msgText with title nameForSubject
				set newForSubject to "Booking " & nameForSubject
			end repeat
			if msgText ≠ "" then
				set newMsg to make new outgoing message with properties {subject:newForSubject, content:newMsgText & msgText} -- EDITED
				tell newMsg
					make new to recipient at end of to recipients with properties {address:"notreal@20zen.com"}
					make new to recipient at end of to recipients with properties {address:"fakename@gmail.com"}
				end tell
				set theResult to send newMsg
				log newForSubject
			end if
		end tell
	end perform mail action with messages
end using terms from

--=====
-- ADDED handler
on clearLineBreaks(t)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {linefeed, return}}
	set l to text items of t
	set AppleScript's text item delimiters to ""
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end clearLineBreaks

--=====

Click on : [Open this Scriplet in your Editor:] to get the contents of the script in your editor.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mardi 22 novembre 2016 10:17:49

Yvan –

Thank you SO MUCH! I haven’t gotten to it yet to test, but reading your code example just taught me several features of AppleScript, not to mention reminding me how to make a function!

I look forward to the day that I can contribute in a similar manner for other less experienced AppleScript developers!

Chris