使用 AppleScript 通过邮件规则创建包含电子邮件详细信息的文本文件
Using AppleScript to create text file with email details via Mail Rule
我正在尝试创建一个 AppleScript,以通过邮件规则使用它来从邮件中获取数据,创建一个文本文件,并将其保存到桌面上的一个文件夹中。我尝试了各种迭代,这些迭代不会给我错误,但也不会创建文件。我将 "CREATE DOCUMENT WITH EMAIL INFORMATION" 部分剪切到一个新的 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 ...”复制到 "close access" 并将变量放在脚本上方,我得到的文件正是我想要的,因此出于某种原因我设置的变量来自邮件的信息没有被用来创建我假设的文档?
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
的变量,但稍后在脚本中引用了一个名为 thismessage
.[=13 的未定义变量=]
无论如何,这是您脚本的修订版:
property text item delimiters : linefeed
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
set msg_attr to {this_messageid:message id ¬
, this_date:date received ¬
, this_sender:sender ¬
, this_subject:subject ¬
, this_content:content ¬
, this_account:name of account of mailbox ¬
, has_attachments:mail attachments} of this_message
if msg_attr's this_subject is "" then ¬
set msg_attr's this_subject to "NO SUBJECT"
ignoring white space
if msg_attr's this_content is in {"", "?"} then ¬
set msg_attr's this_content to "NO CONTENT"
end ignoring
if msg_attr's has_attachments ≠ {} then
set msg_attr's has_attachments to "Yes"
else
set msg_attr's has_attachments to "No"
end if
-- CREATE DOCUMENT WITH EMAIL INFORMATION
set fp to "/Users/CK/Desktop/" & "Email " & ¬
msg_attr's this_subject & space & ¬
msg_attr's this_messageid & ".txt"
my Write_To_File(fp, msg_attr)
end repeat
end tell
end perform mail action with messages
end using terms from
to Write_To_File(filepath as text, msg_attr as record)
local filepath, msg_attr
set folpath to (POSIX file filepath) & "::" as text as alias
tell application "System Events" to make new file with properties ¬
{name:folpath & "tmp-unnamed" as text}
set this_document to folpath & "tmp-unnamed" as text as alias
tell msg_attr to set txt to the contents of {¬
["Airmail Link: ", "airmail://message?mail=", its this_account, ¬
"&messageid=", its this_messageid], ¬
["Mail Link: ", "message://", "messageid=", its this_messageid], ¬
[], ["Date: ", its this_date], ¬
["Subject: ", its this_subject], ¬
["Sender: ", its this_sender], ¬
["Attachments?: ", its has_attachments], ¬
[], its this_content} as text
write the txt to this_document as «class utf8»
tell application "System Events" to set name of this_document to filepath
end Write_To_File
我正在尝试创建一个 AppleScript,以通过邮件规则使用它来从邮件中获取数据,创建一个文本文件,并将其保存到桌面上的一个文件夹中。我尝试了各种迭代,这些迭代不会给我错误,但也不会创建文件。我将 "CREATE DOCUMENT WITH EMAIL INFORMATION" 部分剪切到一个新的 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 ...”复制到 "close access" 并将变量放在脚本上方,我得到的文件正是我想要的,因此出于某种原因我设置的变量来自邮件的信息没有被用来创建我假设的文档?
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
的变量,但稍后在脚本中引用了一个名为 thismessage
.[=13 的未定义变量=]
无论如何,这是您脚本的修订版:
property text item delimiters : linefeed
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
set msg_attr to {this_messageid:message id ¬
, this_date:date received ¬
, this_sender:sender ¬
, this_subject:subject ¬
, this_content:content ¬
, this_account:name of account of mailbox ¬
, has_attachments:mail attachments} of this_message
if msg_attr's this_subject is "" then ¬
set msg_attr's this_subject to "NO SUBJECT"
ignoring white space
if msg_attr's this_content is in {"", "?"} then ¬
set msg_attr's this_content to "NO CONTENT"
end ignoring
if msg_attr's has_attachments ≠ {} then
set msg_attr's has_attachments to "Yes"
else
set msg_attr's has_attachments to "No"
end if
-- CREATE DOCUMENT WITH EMAIL INFORMATION
set fp to "/Users/CK/Desktop/" & "Email " & ¬
msg_attr's this_subject & space & ¬
msg_attr's this_messageid & ".txt"
my Write_To_File(fp, msg_attr)
end repeat
end tell
end perform mail action with messages
end using terms from
to Write_To_File(filepath as text, msg_attr as record)
local filepath, msg_attr
set folpath to (POSIX file filepath) & "::" as text as alias
tell application "System Events" to make new file with properties ¬
{name:folpath & "tmp-unnamed" as text}
set this_document to folpath & "tmp-unnamed" as text as alias
tell msg_attr to set txt to the contents of {¬
["Airmail Link: ", "airmail://message?mail=", its this_account, ¬
"&messageid=", its this_messageid], ¬
["Mail Link: ", "message://", "messageid=", its this_messageid], ¬
[], ["Date: ", its this_date], ¬
["Subject: ", its this_subject], ¬
["Sender: ", its this_sender], ¬
["Attachments?: ", its has_attachments], ¬
[], its this_content} as text
write the txt to this_document as «class utf8»
tell application "System Events" to set name of this_document to filepath
end Write_To_File