【发布时间】:2018-08-12 20:11:13
【问题描述】:
我正在尝试创建一个 AppleScript,以通过邮件规则从邮件中获取数据、创建一个文本文件并将其保存到桌面上的文件夹中。我尝试了各种迭代,它们不会给我一个错误,但也不会创建文件。我将“使用电子邮件信息创建文档”部分剪切为一个新的 AppleScript,并将其上方的变量设置为测试并创建文本文件,因此某些内容不会从我的电子邮件信息变量定义转移到我的文本文档创建。我一直在通过阅读这些论坛来自学,但最终被卡住了,因此,如果您提供任何有关语法或错误的帮助,我们将不胜感激。
using terms from application "Mail"
on perform mail action with messages this_message for rule this_rule
tell application "Mail"
--CLEAR THE VARIABLES
set this_messageid to ""
set this_date to ""
set this_sender to ""
set this_subject to ""
set this_content to ""
set this_attachments to ""
-- GET THE MESSAGE ID OF MESSAGE
set this_messageid to the messageid of this_message
--GET THE DATE RECEIVED OF MESSAGE
set this_date to the date received of this_message
-- GET THE SENDER OF THIS MESSAGE
set this_sender to the sender of this_message
-- GET SUBJECT OF MESSAGE
try
set this_subject to (subject of this_message) as Unicode text
if this_subject is "" then error
on error
set this_subject to "NO SUBJECT"
end try
-- GET CONTENT OF MESSAGE
try
set this_content to (every character of content of this_message) as Unicode text
if this_content is in {"", "?"} then error
on error error_message
set this_content to "NO CONTENT"
end try
--DETERMINE IF THERE ARE ATTACHMENTS
if thisMessage's mail attachments is not {} then
set this_attachments to "Yes"
end if
--GET THE MAILBOX ACCOUNT OF MESSAGE
set this_account to account of mailbox of thisMessage
end tell
-- CREATE DOCUMENT WITH EMAIL INFORMATION
set this_document to open for access ("/Users/username/Desktop/SendToKeepIt/" & "Email " & this_subject & " " & this_messageid & ".txt") with write permission
write "Airmail Link: " & "airmail://message?mail=" & this_account & "&messageid=" & this_messageid & return & "Mail Link: " & "message://" & "&messageid=" & this_messageid & return & return & "Date: " & this_date & return & "Subject: " & this_subject & return & "Sender: " & this_sender & return & "Attachments?: " & this_attachments & return & return & this_content to this_document
close access this_document
end perform mail action with messages
end using terms from
2018-08-14 更新 根据下面的评论,我做了一些更改,但仍然得到相同的结果,没有创建文件。同样,如果我将脚本部分从“--CREATE ...”复制到“关闭访问”并将变量放在脚本上方,我会得到一个正是我想要的文件,所以由于某种原因我设置的变量来自邮件消息没有用于创建我假设的文档?
using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
set the message_count to the count of these_messages
repeat with i from 1 to the message_count
set this_message to item i of these_messages
--CLEAR THE VARIABLES
(*
set this_messageid to ""
set this_date to ""
set this_sender to ""
set this_subject to ""
set this_content to ""
set this_attachments to "No"
*)
-- GET THE MESSAGE ID OF MESSAGE
set this_messageid to the messageid of this_message
--GET THE DATE RECEIVED OF MESSAGE
set this_date to the date received of this_message
-- GET THE SENDER OF THIS MESSAGE
set this_sender to the sender of this_message
-- GET SUBJECT OF MESSAGE
set this_subject to subject of this_message
if this_subject is "" then set this_subject to "NO SUBJECT"
-- GET CONTENT OF MESSAGE
set this_content to (every character of content of this_message)
if this_content is in {"", "?"} then set this_content to "NO CONTENT"
--DETERMINE IF THERE ARE ATTACHMENTS
if thisMessage's mail attachments is not {} then set this_attachments to "Yes"
--GET THE MAILBOX ACCOUNT OF MESSAGE
set this_account to account of mailbox of thisMessage
-- CREATE DOCUMENT WITH EMAIL INFORMATION
set this_document to open for access ("/Users/russellread/Desktop/SendToKeepIt/" & "Email " & this_subject & " " & this_messageid & ".txt") with write permission
write "Airmail Link: " & "airmail://message?mail=" & this_account & "&messageid=" & this_messageid & return & "Mail Link: " & "message://" & "&messageid=" & this_messageid & return & return & "Date: " & this_date & return & "Subject: " & this_subject & return & "Sender: " & this_sender & return & "Attachments?: " & this_attachments & return & return & this_content to this_document
close access this_document
end repeat
end tell
end perform mail action with messages
end using terms from
【问题讨论】:
-
最明显的疏忽是
this_message是一个或多个消息的列表,但您的脚本将其视为单个message对象。在较小的注意事项上,您在任何地方都可以将Unicode text的类型分类为text。您的try...end try块是不必要的且构思不当。但是,如果您解决了主要问题,您就可以看到您离目标有多近。 -
我对脚本进行了一些更改,我将在下面重新发布。
标签: email applescript