Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Quoting specific message content in an autoresponse

Hi, I'm new to applescripts, and have been struggling for a while with the following problem;


Multiple times per day a database is updated, and an email containing the changes in plain text is sent to my email address. Now, some of these changes require me to forward them to a third party, but not all of the information, only the ID and its corresponding name.


Example:


||001 || ||Apple||

||002 || ||Macintosh||


What I need from this is to extract the number "002" and "Macintosh", and forward onto a third party. How should my AppleScript look?


Many thanks in advance!

Posted on Apr 21, 2018 5:59 PM

Reply
Question marked as Best reply

Posted on Apr 22, 2018 2:05 PM

I think you are looking for someone to write you a Mail rule that does everything for you when it detects one of these inbound emails containing the id and name fields. That won't be me as it would require time that I simply do not have available.


What I have done though, is send myself a test email with message content that has a couple of your ||nnn || ||name|| fields randomly located in it, and have written a short Ruby handler to extract those fields and return them to AppleScript as list items. The test content contains ||001 || ||Apple|| and ||002 || ||Macintosh||.


The following AppleScript gets the selected Mail message content, and scans it with Ruby — returning the relevant Id, and Name values in an AppleScript list. The regular expression to do this is an acquired taste.


tell application "Mail"

set theMsg to item 1 of (get selection)

set theContent to content of theMsg

end tell

set id_names to paragraphs of parse_id_name(theContent'squoted form)

logid_names

-- id_names: {"001", "Apple", "002", "Macintosh"}

return


on parse_id_name(atxt)

-- Copy and paste the following correctly indented Ruby code into this AppleScript handler in Script Editor

-- Script Editor is agnostic about other scripting language indentation. Ruby is particular.

return do shell script "ruby <<EOF - " & atxt & "

#!/usr/bin/ruby # code: UTF-8 # look for matching string format: ||nnn || ||name|| in Apple Mail content (atxt) # regular expression with captures for Id and Name regex = /[|]{2}(?<Id>\\d{3})\\s+[|]{2}\\s+[|]{2}(?<Name>.*?)[|]{2}/mu matches = ARGV.first.scan(regex) if matches puts matches end

EOF"

end parse_id_name

1 reply

There are no replies.

Quoting specific message content in an autoresponse

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.