繁体   English   中英

从 Mail.app 获取撰写窗口中的消息

[英]Get message in compose window from Mail.app

我正在尝试制作一个脚本,该脚本将获取我在 Mail 中撰写的电子邮件的内容,对数据进行处理,然后发送邮件。 我知道如何使用 AppleScript 从头开始​​制作和发送新消息,但我找不到一种方法来获取我已经在编写的消息。 我不在乎使用什么语言,我愿意尝试不同的电子邮件客户端。 谢谢你的帮助!

Mail 对 Applescript 有很大的限制,处理它的内容区域是一个主要的限制。 最好的办法是使用 GUI 脚本切换到内容区域,键入 cmd-C,然后处理剪贴板中的数据。

可悲的是,从我所看到的 Applescript 在 Lion 中根本没有得到改进。

有可能,但是很痛苦。 痛苦到我仍在努力弄清楚如何在 Safari 中做类似的事情。 我已经到了可以找到 textarea 的地步,但是我找到的用于获取内容的文档不起作用。 (不幸的是,这对于 AppleScript 的课程来说几乎是标准的;每个程序做的东西都与下一个程序略有不同。)

编辑:好的,有一些可怕的邪恶,希望可以适应与邮件一起使用: http : //www.ece.cmu.edu/~allbery/edit_textarea.script

如果我们做出两个相当弱的假设,那就太直接了:您正在处理的消息是最前面的,并且所有草稿消息的主题都是唯一的。 然后,在运行脚本之前,保存您正在处理的消息; 这会将它放在草稿邮箱中。 然后,由于消息的主题是窗口的名称,我们可以很容易地访问它; 由于我们可以轻松访问草稿邮箱,因此我们可以将两者结合起来。 这给了我们:

tell application "Mail"
    set msgs to messages of drafts mailbox ¬
        whose subject is (name of window 1 as string)
    if (count of msgs) = 1 then
        -- Do whatever
    else
        -- Error, disambiguate, whatever
    end if
end tell

可能可以让脚本保存最前面的窗口,如果新保存的消息总是草稿邮箱的第一项,我不会感到惊讶,但这些留给读者作为练习:-)

所以我在 2020 年遇到了这个问题,并且有了这个 Apple 脚本,它(现在?)有可能(只要它仍然有点笨拙,因为我必须为此使用剪贴板):

activate application "Mail"

tell application "System Events"
    tell process "Mail"
        set initialClipboardContent to (the clipboard as text)
        set composeWindow to (first window whose title does not contain "Inbox")
        set value of attribute "AXFocused" of UI element 1 of scroll area 1 of composeWindow to true

        delay 0.05

        # CMD + A   
        key code 0 using command down
        delay 0.1

        # CMD + C
        key code 8 using command down
        delay 0.1

        set message to (the clipboard as text) as string

        log message

        set the clipboard to initialClipboardContent
    end tell
end tell

这是一个概念证明:

概念证明

做你需要的事情实际上很容易。

  1. 如果您想运行某种内联处理(例如分配给热键(在邮件中,例如 Cmd+D 未被占用),或“仅”列在“服务”菜单中,选择某些内容后可访问),您可以只需使用Automator。 一个演示 Automator 脚本读取当前选择,进行一些更改(这里,将一些 ASCII 字符 + 数字组合转换为一些重音字符),最后,返回修改后的文本如下:

     on run {input, parameters} set myText to replaceText("a1", "á", (input as text)) set myText to replaceText("e1", "é", myText) set myText to replaceText("i1", "í", myText) return myText end run on replaceText(find, replace, someText) set prevTIDs to text item delimiters of AppleScript set text item delimiters of AppleScript to find set someText to text items of someText set text item delimiters of AppleScript to replace set someText to "" & someText set text item delimiters of AppleScript to prevTIDs return someText end replaceText

    如果您想用返回的内容覆盖原始内容,请确保启用“替换所选文本”。

  2. 如果您想编写一个不是从本地服务菜单(或通过热键)调用的外部脚本,您还需要添加剪贴板处理。 类似于上面的解决方案,带有额外的剪贴板复制/粘贴:

     on replaceText(find, replace, someText) set prevTIDs to text item delimiters of AppleScript set text item delimiters of AppleScript to find set someText to text items of someText set text item delimiters of AppleScript to replace set someText to "" & someText set text item delimiters of AppleScript to prevTIDs return someText end replaceText tell application "Mail" activate tell application "System Events" tell process "Mail" click menu item "Select All" of menu "Edit" of menu bar 1 click menu item "Copy" of menu "Edit" of menu bar 1 end tell end tell end tell tell application "Mail" set textclip to (the clipboard) end tell set myText to replaceText("a1", "á", textclip) set myText to replaceText("e1", "é", myText) set myText to replaceText("i1", "í", myText) set the clipboard to myText tell application "Mail" activate tell application "System Events" tell process "Mail" click menu item "Paste" of menu "Edit" of menu bar 1 end tell end tell end tell

请注意,后一个脚本选择(然后覆盖)整个窗口内容。 仅处理当前选择应该很容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM