{"id":409,"date":"2011-09-30T16:19:32","date_gmt":"2011-09-30T23:19:32","guid":{"rendered":"http:\/\/10.0.1.201\/?p=409"},"modified":"2011-09-30T16:19:32","modified_gmt":"2011-09-30T23:19:32","slug":"applescript-to-extract-data-from-a-message-in-mail-app","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2011\/09\/30\/applescript-to-extract-data-from-a-message-in-mail-app\/","title":{"rendered":"AppleScript to extract data from a message in Mail.app"},"content":{"rendered":"<p><ins datetime=\"2020-09-23T02:08:33+00:00\">Newer more betterer version out now! <a href=\"http:\/\/strawhousepig.net\/parsing-e-mail-for-info\/\">Check it.<\/a><\/ins><\/p>\n<p>This is used to take selected e-mails, the contents of which are auto generated web form submissions, and parse them for the mailing address using AppleScript&#8217;s text item delimiters.<br \/>\n<!--more--><br \/>\nThis can be modified to fit your needs and could actually be tightened up quite a bit. The <strong>log<\/strong> commands were used to help debug what the data looked like as the script ran.<\/p>\n<p>Sample data:<\/p>\n<blockquote><p>\nDate: 9\/27\/2011 1:04:46 PM<br \/>\nSome Guy has requested a complimentary copy of the Publication<\/p>\n<p>SHIPPING INFORMATION<br \/>\nFirm Name: Some Guy<br \/>\nE-mail: someguysfakeaddress@example.com<br \/>\nAddress: 123 Main Street<br \/>\nCity: Big City<br \/>\nState: WY<br \/>\nZip: 00000\n<\/p><\/blockquote>\n<p>Ideally you would receive the data formatted how you like, but in this case I have no control over the generating code. So this.<\/p>\n<p><code>set theAddresses to \"\"<br \/>\nset theCount to 0<br \/>\nset old_delims to AppleScript's text item delimiters<br \/>\ntell application \"Mail\"<br \/>\n\tset myMessages to selection as list<br \/>\n\trepeat with i from 1 to the count of myMessages<br \/>\n\t\tif subject of item i of myMessages is \"FW: Request - Complimentary publication\" then<br \/>\n\t\t-- This matches a specific string. Replace \"is\" with \"contains\" to allow for partial matching.<br \/>\n\t\t\tset myContent to content of item i of myMessages<br \/>\n\t\t\ttry<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"Firm Name: \"}<br \/>\n\t\t\t\tset theClip to text item 2 of myContent<br \/>\n\t\t\t\t--\t\t\tlog \"Step 1 - \" & theClip<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"E-mail: \"}<br \/>\n\t\t\t\tset theFirm to text item 1 of theClip<br \/>\n\t\t\t\t--\t\t\tlog \"Step 2 - \" & theFirm<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"Address: \"}<br \/>\n\t\t\t\tset theClip to text item 2 of theClip<br \/>\n\t\t\t\t--\t\t\tlog \"Step 3 - \" & theClip<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"City: \"}<br \/>\n\t\t\t\tset theAddress to text item 1 of theClip<br \/>\n\t\t\t\t--\t\t\tlog \"Step 4 - \" & theAddress<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"City: \"}<br \/>\n\t\t\t\tset theClip to text item 2 of theClip<br \/>\n\t\t\t\t--\t\t\tlog \"Step 5 - \" & theClip<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"State: \"}<br \/>\n\t\t\t\tset theCity to text item 1 of theClip<br \/>\n\t\t\t\t--\t\t\tlog \"Step 6 - \" & theCity<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"State: \"}<br \/>\n\t\t\t\tset theClip to text item 2 of theClip<br \/>\n\t\t\t\t--\t\t\tlog \"Step 7 - \" & theClip<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"Zip: \"}<br \/>\n\t\t\t\tset theState to text item 1 of theClip<br \/>\n\t\t\t\t--\t\t\tlog \"Step 8 - \" & theState<\/p>\n<p>\t\t\t\tset AppleScript's text item delimiters to {\"Zip: \"}<br \/>\n\t\t\t\tset theZip to text item 2 of theClip<br \/>\n\t\t\t\t--\t\t\tlog \"Step 9 - \" & theZip<br \/>\n\t\t\t\tset AppleScript's text item delimiters to {\"\"}<br \/>\n\t\t\t\tset theAddresses to theAddresses & theFirm & theAddress & my removeLastChar(theCity) & \", \" & my removeLastChar(theState) & \" \" & theZip & return<br \/>\n\t\t\t\t--\t\t\tlog theAddresses<\/p>\n<p>\t\t\t\tset theCount to theCount + 1<br \/>\n\t\t\ton error theErr<br \/>\n\t\t\t\tlog theErr<br \/>\n\t\t\tend try<br \/>\n\t\tend if<br \/>\n\tend repeat<br \/>\n\tif theCount is not 0 then<br \/>\n\t\tset the clipboard to theAddresses<br \/>\n\t\tdisplay dialog (theCount as string) & \" addresses were extracted from selected e-mails and placed in the clipboard. :)\" with icon 1<br \/>\n\telse<br \/>\n\t\tdisplay dialog \"No addresses were extracted from selected e-mails. :(\" with icon 2<br \/>\n\tend if<br \/>\nend tell<br \/>\nset AppleScript's text item delimiters to old_delims<\/p>\n<p>-- Sample data leaves newlines. We can recycle some, but a couple need to go.<br \/>\non removeLastChar(the_input)<br \/>\n\t-- set AppleScript's text item delimiters to {\"\"} -- We set this earlier but may want to use this function elsewhere.<br \/>\n\treturn characters 1 thru ((count of characters of the_input) - 1) of the_input as string<br \/>\nend removeLastChar<br \/>\n<\/code><\/p>\n<p><a href=\"applescript:\/\/com.apple.scripteditor?action=new&#038;script=set%20theAddresses%20to%20%22%22%0Dset%20theCount%20to%200%0Dset%20old_delims%20to%20AppleScript%27s%20text%20item%20delimiters%0Dtell%20application%20%22Mail%22%0D%09set%20mymessages%20to%20selection%20as%20list%0D%09repeat%20with%20i%20from%201%20to%20the%20count%20of%20mymessages%0D%09%09if%20subject%20of%20item%20i%20of%20mymessages%20is%20%22FW%3A%20Request%20%2D%20Complimentary%20publication%22%20then%0D%09%09%09%2D%2D%20This%20matches%20a%20specific%20string%2E%20Replace%20%22is%22%20with%20%22contains%22%20to%20allow%20for%20partial%20matching%2E%0D%09%09%09set%20mycontent%20to%20content%20of%20item%20i%20of%20mymessages%0D%09%09%09try%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22Firm%20Name%3A%20%22%7D%0D%09%09%09%09set%20theClip%20to%20text%20item%202%20of%20mycontent%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%201%20%2D%20%22%20%26%20theClip%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22E%2Dmail%3A%20%22%7D%0D%09%09%09%09set%20theFirm%20to%20text%20item%201%20of%20theClip%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%202%20%2D%20%22%20%26%20theFirm%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22Address%3A%20%22%7D%0D%09%09%09%09set%20theClip%20to%20text%20item%202%20of%20theClip%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%203%20%2D%20%22%20%26%20theClip%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22City%3A%20%22%7D%0D%09%09%09%09set%20theAddress%20to%20text%20item%201%20of%20theClip%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%204%20%2D%20%22%20%26%20theAddress%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22City%3A%20%22%7D%0D%09%09%09%09set%20theClip%20to%20text%20item%202%20of%20theClip%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%205%20%2D%20%22%20%26%20theClip%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22State%3A%20%22%7D%0D%09%09%09%09set%20theCity%20to%20text%20item%201%20of%20theClip%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%206%20%2D%20%22%20%26%20theCity%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22State%3A%20%22%7D%0D%09%09%09%09set%20theClip%20to%20text%20item%202%20of%20theClip%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%207%20%2D%20%22%20%26%20theClip%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22Zip%3A%20%22%7D%0D%09%09%09%09set%20theState%20to%20text%20item%201%20of%20theClip%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%208%20%2D%20%22%20%26%20theState%0D%09%09%09%09%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22Zip%3A%20%22%7D%0D%09%09%09%09set%20theZip%20to%20text%20item%202%20of%20theClip%0D%09%09%09%09%2D%2D%09%09%09log%20%22Step%209%20%2D%20%22%20%26%20theZip%0D%09%09%09%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22%22%7D%0D%09%09%09%09set%20theAddresses%20to%20theAddresses%20%26%20theFirm%20%26%20theAddress%20%26%20my%20removeLastChar%28theCity%29%20%26%20%22%2C%20%22%20%26%20my%20removeLastChar%28theState%29%20%26%20%22%20%22%20%26%20theZip%20%26%20return%0D%09%09%09%09%2D%2D%09%09%09log%20theAddresses%0D%09%09%09%09%0D%09%09%09%09set%20theCount%20to%20theCount%20%2B%201%0D%09%09%09on%20error%20theErr%0D%09%09%09%09log%20theErr%0D%09%09%09end%20try%0D%09%09end%20if%0D%09end%20repeat%0D%09if%20theCount%20is%20not%200%20then%0D%09%09set%20the%20clipboard%20to%20theAddresses%0D%09%09display%20dialog%20%28theCount%20as%20string%29%20%26%20%22%20addresses%20were%20extracted%20from%20selected%20e%2Dmails%20and%20placed%20in%20the%20clipboard%2E%20%3A%29%22%20with%20icon%201%0D%09else%0D%09%09display%20dialog%20%22No%20addresses%20were%20extracted%20from%20selected%20e%2Dmails%2E%20%3A%28%22%20with%20icon%202%0D%09end%20if%0Dend%20tell%0Dset%20AppleScript%27s%20text%20item%20delimiters%20to%20old_delims%0D%0D%2D%2D%20Sample%20data%20leaves%20newlines%2E%20We%20can%20recycle%20some%2C%20but%20a%20couple%20need%20to%20go%2E%0Don%20removeLastChar%28the_input%29%0D%09%2D%2D%20set%20AppleScript%27s%20text%20item%20delimiters%20to%20%7B%22%22%7D%20%2D%2D%20We%20set%20this%20earlier%20but%20may%20want%20to%20use%20this%20function%20elsewhere%2E%0D%09return%20characters%201%20thru%20%28%28count%20of%20characters%20of%20the_input%29%20%2D%201%29%20of%20the_input%20as%20string%0Dend%20removeLastChar%0D\">Open in Script Editor.app<\/a><\/p>\n<p>Be aware that as-is  this script does not error check to see if an e-mail is actually selected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Newer more betterer version out now! Check it. This is used to take selected e-mails, the contents of which are auto generated web form submissions, and parse them for the mailing address using AppleScript&#8217;s text item delimiters.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,28],"class_list":["post-409","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-os-x"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=409"}],"version-history":[{"count":0,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/409\/revisions"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}