简体   繁体   English

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

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

I am trying to make a script that will get the contents of an email message that I'm composing in Mail, do something with the data, and then send the message.我正在尝试制作一个脚本,该脚本将获取我在 Mail 中撰写的电子邮件的内容,对数据进行处理,然后发送邮件。 I know how to make and send a new message from scratch with AppleScript, but I can't find a way to get a message that I'm already writing.我知道如何使用 AppleScript 从头开始​​制作和发送新消息,但我找不到一种方法来获取我已经在编写的消息。 I don't care what language is used, and I would be open to trying a different email client.我不在乎使用什么语言,我愿意尝试不同的电子邮件客户端。 Thanks for your help!谢谢你的帮助!

Mail has huge limitations with regards to Applescript and dealing with its content area is a major one. Mail 对 Applescript 有很大的限制,处理它的内容区域是一个主要的限制。 The best bet is to use GUI scripting to tab to the content area, type cmd-C, and then work off the data in the clipboard.最好的办法是使用 GUI 脚本切换到内容区域,键入 cmd-C,然后处理剪贴板中的数据。

Sadly from what I can see Applescript has not been improved at all in Lion.可悲的是,从我所看到的 Applescript 在 Lion 中根本没有得到改进。

It's possible, but painful.有可能,但是很痛苦。 Painful enough that I'm still trying to work out exactly how to do something similar but in Safari.痛苦到我仍在努力弄清楚如何在 Safari 中做类似的事情。 I've gotten to the point where I can find the textarea, but the documentation I've found for getting the content isn't working.我已经到了可以找到 textarea 的地步,但是我找到的用于获取内容的文档不起作用。 (Unfortunately, that's pretty much par for the course for AppleScript; every program does stuff just a little bit differently from the next program.) (不幸的是,这对于 AppleScript 的课程来说几乎是标准的;每个程序做的东西都与下一个程序略有不同。)

EDIT: ok, have some horrible evil which hopefully can be adapted to work with Mail: http://www.ece.cmu.edu/~allbery/edit_textarea.script编辑:好的,有一些可怕的邪恶,希望可以适应与邮件一起使用: http : //www.ece.cmu.edu/~allbery/edit_textarea.script

This is striaghtforward if we make two reasonably weak assumptions: that the message you're working on is frontmost, and that the subject of all draft messages is unique.如果我们做出两个相当弱的假设,那就太直接了:您正在处理的消息是最前面的,并且所有草稿消息的主题都是唯一的。 Then, before running the script, save the message you're working on;然后,在运行脚本之前,保存您正在处理的消息; this will place it in the drafts mailbox.这会将它放在草稿邮箱中。 Then, since the subject of the message is the name of the window, we can easily access it;然后,由于消息的主题是窗口的名称,我们可以很容易地访问它; and since we can easily access the drafts mailbox, we can combine the two.由于我们可以轻松访问草稿邮箱,因此我们可以将两者结合起来。 This gives us:这给了我们:

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

It's probably possible to make the script save the frontmost window, and it wouldn't surprise me if a freshly-saved message is always the first item of the drafts mailbox, but these are left as an exercise for the reader :-)可能可以让脚本保存最前面的窗口,如果新保存的消息总是草稿邮箱的第一项,我不会感到惊讶,但这些留给读者作为练习:-)

So I came across this in 2020 and with this Apple Script it is (now?) possible (whenever its still a bit hacky since I have to use the clipboard for this):所以我在 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

Here a proof of concept:这是一个概念证明:

概念证明

It's actually pretty easy to do what you need.做你需要的事情实际上很容易。

  1. If you want to run some kind of an inline processing (assigned to, say, a hotkey (in Mail, eg Cmd+D is not occupied), or "just" listed in the Services menu, accessible after selecting something), you can simply use Automator.如果您想运行某种内联处理(例如分配给热键(在邮件中,例如 Cmd+D 未被占用),或“仅”列在“服务”菜单中,选择某些内容后可访问),您可以只需使用Automator。 A demo Automator script reading the current selection, making some changes (here, converting some ASCII char+number combinations to some accented characters) and, finally, returning the modified text is as follows:一个演示 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

    Make sure you enable the "Replaces selected text", should you want to overwrite the original content with the returned one.如果您想用返回的内容覆盖原始内容,请确保启用“替换所选文本”。

  2. if you want to write an external script not invoked from the local Services menu (or via a hotkey), you'll also need to add clipboard handling.如果您想编写一个不是从本地服务菜单(或通过热键)调用的外部脚本,您还需要添加剪贴板处理。 A solution similar to the above with additional clipboard copy/paste:类似于上面的解决方案,带有额外的剪贴板复制/粘贴:

     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

Note that the latter script selects (and, then, overwrites) the entire window contents.请注意,后一个脚本选择(然后覆盖)整个窗口内容。 It should be easy to work on the current selection only.仅处理当前选择应该很容易。

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

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