2012-05-18 92 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

相关问题