使用AppleScript将.rtf文本复制到电子邮件正文中

时间:2012-05-18 19:14:13

标签: applescript rtf

我有一个AppleScript应用程序,它创建一个电子邮件(在Mail.app中),其中包含我通过对话框选择的选项中的附件。文本模板以.rtf格式存储,因此非编程人员可以根据自己的意愿更改文本模板。

我能够从.txt纯文本文件创建电子邮件,但是当我导入.rtf文本文件时,它会导入格式化命令,这些命令在邮件中是我不想要的。

以下是.rtf导入电子邮件的示例:

  

{\ 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英语发言人的技术邮件\

以下是我的脚本的一部分:

-- 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,因为大多数情况下使用粗体和颜色来格式化文本)?

3 个答案:

答案 0 :(得分:1)

我认为Mail会以纯文本或HTML格式发送电子邮件,而不是rtf。因此,您需要将您的电子邮件发送为html而不是rtf。请注意,通过AppleScript发送带有Mail的html电子邮件是个窍门。由于某种原因,您无法将新消息的visible属性设置为true。如果你这样做就行不通了。

以下是这对您有何帮助。您可以使用命令行工具textutil将rtf转换为html,然后将其作为电子邮件发送。注意我在命令中使用“tidy”来确保html代码是干净的。

所以你需要做的就是把接收者的电子邮件地址和主题放在这个脚本中并运行它......

set emailAddress to "someone@somewhere.com"
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 :(得分: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

答案 2 :(得分:0)

adayzdone的答案或多或少对我有用。 在Mac os X 10.15.6,Mail 13.4下,滚动区域位于另一个位置/索引。 并且除了使用keystroke tab来获取内容区域之外,另一种方法是

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

如果您需要查找滚动区域或标识其他UI元素-您可能要使用

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

还有一种使用Automator 看我做的神奇方法来获取菜单项: see this post
(您必须将“事件”从Automator复制粘贴到某些文本编辑器中,以获得相应的AppleScript)

有Xcode-> Xcode(菜单)-> Open Developer Tool->辅助功能检查器-但是我发现很难将信息传输到applescript

最有帮助的希望-tom