1
votes

I have an AppleScript application which creates an email (in Mail.app) with attachments from the options I choose through dialogs. The text templates are stored in .rtf format so non-programers can alter the text templates to their wishes.

I am able to create an email from a .txt plain text file, but when I import an .rtf text file it imports the formatting commands, which I don’t want in the mail.

Here is an example of a .rtf import into an email:

{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360 {\fonttbl\f0\fswiss\fcharset0 Helvetica-Light;} {\colortbl;\red255\green255\blue255;} \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural

\f0\fs24 \cf0 technology mail for english speakers\

Here is part of my script:

-- Import the .rtf file and store content in the mycontent variable    
set mycontent to (read "/Users/kiara/Desktop/mailer/technology/tech-en-content.rtf")
…
…
-- create mail from values stored in variables
tell application "Mail"
    set theMessage to make new outgoing message with properties {visible:true, subject:mysubject, content:mycontent}
    tell content of theMessage
        make new attachment with properties {file name:this_file} at after last paragraph
    end tell
end tell

Is it possible to import formatted text from .rtf files into a new mail without the formatting codes (I choose rtf because mostly bold and color are used to format text)?

3

3 Answers

2
votes

Here is another approach:

set the clipboard to (read "/Users/kiara/Desktop/mailer/technology/tech-en-content.rtf" as «class RTF »)

tell application "Mail"
    activate
    set theMessage to make new outgoing message with properties {visible:true, subject:"mysubject"}
end tell

tell application "System Events"
    tell process "Mail"
        repeat until focused of UI element 1 of scroll area 4 of window 1
            keystroke tab
        end repeat
        keystroke "v" using command down
    end tell
end tell
1
votes

I think Mail sends an email as either plain text or html, not rtf. So you would need to send your email as html not rtf. Note there's a trick to sending a html email with Mail via applescript. For some reason you can't set the visible property of the new message to true. It won't work if you do.

Here's how this can help you. You can use the command line tool textutil to convert the rtf to html and then send it as an email. Notice I use "tidy" in the command to make sure the html code is clean.

So all you need to do is put the receiver's email address and a subject in this script and run it...

set emailAddress to "[email protected]"
set theSubject to "My converted rtf to html"

set rtfFile to choose file with prompt "Choose the RTF file to email as HTML:" without invisibles
set theHTML to do shell script "/usr/bin/textutil " & " -stdout -format rtf -convert html " & quoted form of POSIX path of rtfFile & " | /usr/bin/tidy -b -utf8"

tell application "Mail"
    set newMessage to make new outgoing message at end of outgoing messages with properties {visible:false}
    tell newMessage
        make new to recipient at end of to recipients with properties {address:emailAddress}
        set subject to theSubject
        set html content to theHTML
        send
    end tell
end tell
0
votes

adayzdone answer works for me - more or less. under Mac os X 10.15.6 , Mail 13.4 the scroll area is at another position / index. And instead of using keystroke tab to get the content-Area an alternative way is to

set value of attribute "AXFocused" of UI element of scroll area 1 of window 1 to true

if you need to find the scroll area or identify other UI elements - you might want to use

on FindScrollAreas()
    set Indices to {}
    set the clipboard to "hello World"
    tell application "System Events"
        tell process "Mail"
            activate
            set i to 1
            repeat with UIElement in UI elements of front window
                -- button, text field, scroll area, static text
                if class of UIElement is scroll area then
                    set end of Indices to i
                end if
                set i to i + 1
            end repeat
        end tell
    end tell
    return Indices
end FindScrollAreas

and there is also the fantastic approach in using Automator watch me do to get the menu items: see this post
(you have to copy-paste the Events from Automator to some text-Editor to get some corresponding applescript)

and there is Xcode -> Xcode(menu) -> Open Developer Tool -> Accessibility Inspector - but i find it hard to transfer info to applescript

hope that helps, best -tom