AppleScript to extract data from a message in Mail.app

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’s text item delimiters.

This can be modified to fit your needs and could actually be tightened up quite a bit. The log commands were used to help debug what the data looked like as the script ran.

Sample data:

Date: 9/27/2011 1:04:46 PM
Some Guy has requested a complimentary copy of the Publication

SHIPPING INFORMATION
Firm Name: Some Guy
E-mail: someguysfakeaddress@example.com
Address: 123 Main Street
City: Big City
State: WY
Zip: 00000

Ideally you would receive the data formatted how you like, but in this case I have no control over the generating code. So this.

set theAddresses to ""
set theCount to 0
set old_delims to AppleScript's text item delimiters
tell application "Mail"
  set myMessages to selection as list
  repeat with i from 1 to the count of myMessages
    if subject of item i of myMessages is "FW: Request - Complimentary publication" then
    -- This matches a specific string. Replace "is" with "contains" to allow for partial matching.
      set myContent to content of item i of myMessages
      try
        
        set AppleScript's text item delimiters to {"Firm Name: "}
        set theClip to text item 2 of myContent
        --      log "Step 1 - " & theClip
        
        set AppleScript's text item delimiters to {"E-mail: "}
        set theFirm to text item 1 of theClip
        --      log "Step 2 - " & theFirm
        
        set AppleScript's text item delimiters to {"Address: "}
        set theClip to text item 2 of theClip
        --      log "Step 3 - " & theClip
        
        set AppleScript's text item delimiters to {"City: "}
        set theAddress to text item 1 of theClip
        --      log "Step 4 - " & theAddress
        
        set AppleScript's text item delimiters to {"City: "}
        set theClip to text item 2 of theClip
        --      log "Step 5 - " & theClip
        
        set AppleScript's text item delimiters to {"State: "}
        set theCity to text item 1 of theClip
        --      log "Step 6 - " & theCity
        
        set AppleScript's text item delimiters to {"State: "}
        set theClip to text item 2 of theClip
        --      log "Step 7 - " & theClip
        
        set AppleScript's text item delimiters to {"Zip: "}
        set theState to text item 1 of theClip
        --      log "Step 8 - " & theState
        
        set AppleScript's text item delimiters to {"Zip: "}
        set theZip to text item 2 of theClip
        --      log "Step 9 - " & theZip
        set AppleScript's text item delimiters to {""}
        set theAddresses to theAddresses & theFirm & theAddress & my removeLastChar(theCity) & ", " & my removeLastChar(theState) & " " & theZip & return
        --      log theAddresses
        
        set theCount to theCount + 1
      on error theErr
        log theErr
      end try
    end if
  end repeat
  if theCount is not 0 then
    set the clipboard to theAddresses
    display dialog (theCount as string) & " addresses were extracted from selected e-mails and placed in the clipboard. :)" with icon 1
  else
    display dialog "No addresses were extracted from selected e-mails. :(" with icon 2
  end if
end tell
set AppleScript's text item delimiters to old_delims

-- Sample data leaves newlines. We can recycle some, but a couple need to go.
on removeLastChar(the_input)
  -- set AppleScript's text item delimiters to {""} -- We set this earlier but may want to use this function elsewhere.
  return characters 1 thru ((count of characters of the_input) - 1) of the_input as string
end removeLastChar

Open in Script Editor.app

Be aware that as-is this script does not error check to see if an e-mail is actually selected.

1 thought on “AppleScript to extract data from a message in Mail.app

  1. Pingback: Parsing e-mail for info | strawhousepig.net

Leave a Reply

Your email address will not be published. Required fields are marked *