Macos 从Mail.app在撰写窗口中获取消息
我正在尝试制作一个脚本,该脚本将获取我在邮件中编写的电子邮件的内容,对数据进行处理,然后发送消息。我知道如何使用AppleScript从头开始创建和发送新消息,但我找不到一种方法来获取我正在编写的消息。我不在乎使用什么语言,我愿意尝试不同的电子邮件客户端。谢谢你的帮助 这是可能的,但很痛苦。这已经够痛苦的了,我仍然在努力弄清楚如何在Safari中做类似的事情。我已经到了可以找到textarea的地步,但是我找到的获取内容的文档不起作用。(不幸的是,这对于Apple脚本来说是相当标准的,每个程序都和下一个程序有点不同。)Macos 从Mail.app在撰写窗口中获取消息,macos,email,applescript,Macos,Email,Applescript,我正在尝试制作一个脚本,该脚本将获取我在邮件中编写的电子邮件的内容,对数据进行处理,然后发送消息。我知道如何使用AppleScript从头开始创建和发送新消息,但我找不到一种方法来获取我正在编写的消息。我不在乎使用什么语言,我愿意尝试不同的电子邮件客户端。谢谢你的帮助 这是可能的,但很痛苦。这已经够痛苦的了,我仍然在努力弄清楚如何在Safari中做类似的事情。我已经到了可以找到textarea的地步,但是我找到的获取内容的文档不起作用。(不幸的是,这对于Apple脚本来说是相当标准的,每个程序都
编辑:好的,有一些可怕的邪恶,希望能够适应邮件:邮件对于Applescript有巨大的限制,处理其内容区域是一个主要的限制。最好的办法是使用GUI脚本来标记内容区域,键入cmd-C,然后处理剪贴板中的数据
遗憾的是,从我所看到的情况来看,Applescript在Lion中没有得到任何改进。如果我们做出两个相当薄弱的假设,这就是向前迈进:您正在处理的消息是最前端的,所有草稿消息的主题都是唯一的。然后,在运行脚本之前,保存正在处理的消息;这将把它放在草稿邮箱中。然后,由于消息的主题是窗口的名称,因此我们可以轻松访问它;由于我们可以轻松访问草稿邮箱,因此我们可以将两者结合起来。这给了我们:
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
可能可以让脚本保存最前面的窗口,如果新保存的邮件始终是草稿邮箱的第一项,我也不会感到惊讶,但这些都是留给读者的练习:-)做你需要的事情其实很容易
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
如果要用返回的文本覆盖原始内容,请确保启用“替换选定文本”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
请注意,后一个脚本选择(然后覆盖)整个窗口内容。仅在当前选择上工作应该很容易 因此,我在2020年遇到了这个问题,通过这个苹果脚本,它(现在?)是可能的(因为我必须使用剪贴板来完成这个任务,所以每当它仍然有点黑客时): 以下是概念证明:
我昨天试过这个。我首先得到了所有草稿邮件的列表。然后我关上前窗,让它成为一个草稿。然后我得到了一个新的草稿列表,并将其与旧的列表进行了比较,这给了我实际的信息。但是,我无法更改该消息的文本。我可以打开邮件,可以获取当前内容,但无法设置新内容。你能做到吗?噢,见鬼。这似乎不起作用,至少不直接起作用:-/好吧,看起来没有好的方法来做这件事,所以我在AppleScript中只使用了按键,效果很好:
tell application“System Events”延迟0.25次按键“a”使用command down按键“c”使用command down按键“d”使用{command down,shift down}end tell
我注意到的一个问题是,在之后告诉应用程序“Mail”;设置消息以生成传出消息
消息类型为传出消息
,而在之后告诉应用程序“Mail”;将selection设置为selection;将消息设置为所选内容的第1项
它的类型为消息
(即使它是草稿)。我可以编辑使用前一个命令创建的消息的内容,但不能编辑使用后一个命令创建的消息的内容。
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