I am trying to create an AppleScript to use via a Mail rule to take data from the message, create a text file, and save it to a folder on the desktop. I have tried various iterations that do not give me an error but also don't create the file. I cut the "CREATE DOCUMENT WITH EMAIL INFORMATION" portion into a new AppleScript and set the variables above it as a test and the text file is created, so something isn't moving from my email information variable defining over to my text document creating. I have been teaching myself by reading these forums and am finally stuck, so any help with syntax or errors you see would be appreciated.
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
Update 2018-08-14 Based on the comment below, I made some changes but am still getting the same result, no file is created. Again, if I copy the script portion from "--CREATE..." to "close access" and put variables above the script, I get a file that is exactly what I want, so for some reason the variables I am setting from the mail message are not being used to create the document I assume?
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
is a list of one or more messages, but you're script treats it as a singularmessage
object. On a lesser note, everywhere you've type-classed something asUnicode text
can (and probably should) be changed just totext
. Yourtry
...end try
blocks are unnecessary and ill-conceived. But if you address the main issue, you can then see how close that gets you to your goal. – CJK