2012-05-18 89 views
1

我有一個AppleScript應用程序,它使用通過對話框選擇的選項創建電子郵件(在Mail.app中)。文本模板以.rtf格式存儲,非編程人員可以根據自己的意願更改文本模板。使用AppleScript將.rtf文本複製到電子郵件正文

我能夠從一個.txt純文本文件創建一個電子郵件,但是當我導入.rtf文本文件,它進口的格式化命令,這是我不希望的郵件。

這裏是到電子郵件一個.rtf進口的一個示例:

{\ RTF1 \ ANSI \ ansicpg1252 \ cocoartf1038 \ cocoasubrtf360 {\ fonttbl \ F0 \ fswiss \ fcharset0黑體光強;} { \ colortbl; \ red255 \ green255 \ blue255;} \ PARD \ tx560 \ tx1120 \ tx1680 \ tx2240 \ tx2800 \ tx3360 \ tx3920 \ tx4480 \ tx5040 \ tx5600 \ tx6160 \ tx6720 \ QL \ qnatural \ pardirnatural

\ F0 \ fs24 \ cf0技術郵件給英語人士\

這裏是我的腳本的一部分:

-- 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 

是否有可能從.rtf文件導入格式的文本到一個新的郵件,而格式代碼(我選擇RTF,因爲大多是大膽的顏色是用來格式化文本) ?

回答

1

我認爲郵件發送電子郵件爲純文本或html,而不是rtf。所以你需要發送你的電子郵件爲html而不是rtf。請注意,通過applescript發送帶有Mail的HTML電子郵件是個竅門。出於某種原因,您不能將新消息的可見屬性設置爲true。如果你這樣做,它將不起作用。

下面是這可以幫助你。您可以使用命令行工具textutil將rtf轉換爲html,然後將其作爲電子郵件發送。注意我在命令中使用「tidy」來確保html代碼是乾淨的。

因此,所有你需要做的就是把收件人的電子郵件地址,並在該腳本的對象,並運行它...

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 
+1

很好的 - 我沒有意識到沒有記錄的「html內容」(很好地提醒人們應該總是檢查對象屬性而不是依賴字典)。但是有兩點意見:設置'visible:false'並不是必需的 - 忽略該屬性也會起作用(除了'visible:true'外,任何人都會懷疑);並且使用'tiny -bare'將不會很好地處理ASCII範圍以外的字符 - 除了退出非零(這會錯誤執行shell腳本),它會讓您留下臭名昭着的「förmatting」版本的UTF -8文本。使用'tiny -raw'或'tiny -utf8'會得到更好的結果。 – kopischke

+0

感謝您的評論kopischke。我已經更新了代碼以使用您對整潔的建議。這是一個不錯的和必要的改進。 – regulus6633

+0

在原始源代碼視圖中檢查了郵件,發現「Content-Type:text/html」,所以是的,你在正確的地方將豐富的文本結尾爲HTML。我硬編碼了一個rtf文件的路徑並刪除了發送命令,因爲我的用戶必須能夠在發送前最後一次檢查郵件。我現在面臨的問題是郵件在沒有內容的情況下出現。你知道如何讓內容出現在身體中嗎? – elhombre

1

這裏是另一種方法:

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 
+0

剛剛保存了我的培根,一直在戰鬥Mail的愚蠢模板「功能」多年。非常感謝! – skiaddict1

相關問題