1 Reply Latest reply on Feb 12, 2010 5:51 PM by FentonJones

    Losing Text Returns When Using Applescript Email Import

    avah12

      Title

      Losing Text Returns When Using Applescript Email Import

      Post

      I have a script that I use to import emails from Apple Mail to Filemaker.  Everything works as I'd like, except for the email body.  When it imports the body data it loses any style and text returns that the original had.  I'm fine with losing color, bolding, etc, but I would like for the returns to stay, otherwise it makes the email text hard to read.

       

      What's odd is that after it has been imported, if I copy the text out of the field and into another program like Stickies, the text returns are intact.  Anybody have advice?  Any help would be appreciated.  I am using Filemaker 9 on OS X Leopard.

       

      The following is the AppleScript that I'm using.

       

      tell application "Mail"

      set selectedMessages to selection

      set selectionCount to (count of selectedMessages)

      repeat with messageNumber from 1 to selectionCount

      set theMessage to item messageNumber of selectedMessages

      set selectionCount to (count of selectedMessages)

      set emailbody to content of theMessage

      set emailsender to sender of theMessage

      set emailsubject to subject of theMessage

      set emaildate to date received of theMessage as string

       

      tell application "FileMaker Pro"

      set cell "gEmailFrom" of current record to emailsender

      set cell "gEmailSubject" of current record to emailsubject

      set cell "gEmailMessageBody" of current record to emailbody

      set cell "gEmailRawDateInput" of current record to emaildate

      end tell

      end repeat

      end tell 

        • 1. Re: Losing Text Returns When Using Applescript Email Import
          FentonJones
            

          The problem is that Mail uses the more modern Unix line ending (ASCII 10), whereas FileMaker expects the older Mac return (ASCII 13). When you use the OS Clipboard, it is coerced to what the program wants (or it always uses Mac returns, not sure which). But AppleScript itself is "line ending agnostic"; it does not care, can handle either (also PC line ending/returns). So it just gives FileMaker what it is in Mail.

           

          Likely the line endings are still there in FileMaker, but do not function as carriage returns. You can find/replace one character with the other. In FileMaker 10, you could use the Char(ASCII number) function in a Substitute() function; that should work.  

           

          But you can also do it within AppleScript, using the standard AppleScript "find/replace", which is done via text item delimiters (which looks odd at first, but is quite powerful; like what other find/replaces do behind the scenes). This is an extract from a Mail AppleScript I have. The step above, within the whole Mail tell block (nested) calls the subroutine (which are generally put at the end of the script, out of the way). It sets the theEmail variable (which was earlier set to the message's content; "emailbody" in your AppleScript) to its converted result.

           

          ---- 

          set theEmail to my switch(ASCII character 10, ASCII character 13, theEmail)

          -----

           

          on switch(char1, char2, str)

          -- switch char1 to char2 in str

          set od to AppleScript's text item delimiters

          set AppleScript's text item delimiters to char1

          settemptotext itemsofstr

          set AppleScript's text item delimiters to char2

          telltemptosettemptobeginning & ({""} & rest)

          set AppleScript's text item delimiters to od

          returntemp

          end switch