This script is titled ForwardMail.scpt and is located in my user "Library/Application Scripts/com.apple.mail" directory
The desire is to run this Applescript in a Mail rule that acts on messages that arrive to my in box with certain criteria. I want to forward the selected emails after adding YYYY-MM-DD SPACE to the begining of the incoming message subject.
My Mail rule is set as "If any of the following conditiond are met: Subject contains test, Perform the following actions: Run Applescript ForwardMail."
using terms from application "Mail"
on perform mail action with messages tMsgs -- for rule tsRul <--Is this necessary? I've tried both ways.
repeat with aMsg in tMsgs
set fwdMsg to forward aMsg -- with opening window <--Is this necessary? I've tried both ways.
tell fwdMsg
activate
set sentDate to (short date string of (get date sent of aMsg))
set datedSubject to sentDate & space & subject of aMsg
set subject to datedSubject
make new to recipient with properties {address:"test@gmail.com", visible:true}
-- send fwdMsg -- I want to see the message that will be forwarded befgore I activate this command, so I try with "visible:true" above
end tell
end repeat
end perform mail action with messages
end using terms from
The problem is I am not seeing any forwarded message and it appears that Mail is not even running the script. When I send a test message nothing happens. I know the rule works because I can change the "Run Applescript" to "Delete" and the test message gets deleted. However, when I add some "say" or "display dialog" commands, nothing.
I'm still using Mojave, so I can't test beyond it at the moment – but I'll bet you a beer Apple hasn't done squat to fix this particular problem.
I used to use AppleScript filters all the time and was utterly furious when they bollixed it up – and beyond livid when they blew me off after I reported the problem.
I'm having a play, and this certainly works in Monterey when messages are selected and "Apply Rules" chosen from the "Message" menu:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with eachMessage in theMessages
set theSubject to (subject of eachMessage)
display dialog theSubject
end repeat
end perform mail action with messages
end using terms from
I'll have a proper crack at your forwarding problem tomorrow, but if the above doesn't work for you then anything I try might be pointless.
I've a feeling your script is erroring. This line:
make new to recipient with properties {address:"test@gmail.com", visible:true}
...in particular looks wrong -- visible isn't a property of recipient, at least not in Monterey Mail. It'll compile in Script Editor, but I think it will bork when run.
This is working for me -- tested using an incoming rule, and will pop up the new message for you to inspect, can easily be changed to automatically send in the background.
using terms from application "Mail"
on perform mail action with messages messageList in mailboxes mbox for rule aRule
repeat with eachMessage in messageList
set forwardMessage to forward eachMessage with opening window
set sentDate to (short date string of (get date sent of eachMessage))
set the subject of forwardMessage to (sentDate & " " & subject of forwardMessage)
tell forwardMessage to make new to recipient with properties {address:"test@example.com"}
end repeat
end perform mail action with messages
end using terms from
I believe I got it fixed, for now. I have some more changes planned.
Because it is now operating as intended, I thought I would take a few minutes and let members know what I did.
As I found it quite difficult to analyze and debug the script while it was being called from a Mac Mail rule, I decided to write the code to function on selected mail messages. Accordingly I wrote and debugged the following script:
tell application "Mail"
set theMessages to the selection
repeat with aMessage in theMessages
set forwardMessage to forward aMessage with opening window
tell forwardMessage
activate
set dateReceived to (get date received of aMessage)
set {year:y, month:m, day:d} to dateToConvert
tell (y * 10000 + m * 100 + d) as string to rich text 1 thru 4 & "-" & rich text 5 thru 6 & "-" & rich text 7 thru 8
set datedSubject to dateReceived & space & subject of aMessage
set subject to datedSubject as rich text
set sender to "<ken@gmail.com>"
set myRecipient to make new to recipient at end of to recipients with properties {address:"<test@gmail.com>"}
send forwardMessage
end tell
end repeat
end tell
Then it became a simple matter of altering the code to function from a Mail rule by commenting out line two, adding two lines of code to the beginning and end of the script. Now the script looks like this and functions being called from a Mail rule:
using terms from application "Mail"
on perform mail action with messages theMessages
tell application "Mail"
--set theMessages to the selection
repeat with aMessage in theMessages
set forwardMessage to forward aMessage with opening window
tell forwardMessage
activate
set dateReceived to (get date received of aMessage)
tell script file "ConvertDate" to set dateReceived to my convertDate(dateReceived)
set datedSubject to dateReceived & space & subject of aMessage
set subject to datedSubject as rich text
set sender to "<ken@lindscape.com>"
set myRecipient to make new to recipient at end of to recipients with properties {address:"<test@lindscape.com>"}
send forwardMessage
end tell
end repeat
end tell
end perform mail action with messages
end using terms from
on convertDate(dateToConvert)
set {year:y, month:m, day:d} to dateToConvert
tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
end convertDate
I also turned the date conversion function into a handler so I could use it elsewhere.
I'm open to suggestions if anyone sees something that can be accomplished better.
I used part of Paxapunch's excellent Mail rules script as a model for my simple requirement to forward messages with new addresses added to the bcc recipient list. However I got a compiler error message that access to bcc recipients was not allowed.
I then reverted back to try his original line adding addresses to the to recipients list as follows:
set myRecipient to make new to recipient at end of to recipients with properties {address:"test@gmail.com"}
However I got the same error message.
Suggestions:
I am on a MacBook Pro (Apple Silicon) with macOS Sonoma 14.1.2 (the latests) and Script Editor 2.11 (230) and AppleScript 2.8
According to the mail.sdef you can send to BCC recipients.
I noticed that you were trying to use the account test@gmail.com. Does that account exist? Perhaps you are getting that error because that account does not exist. Also it is possible that Google does not allow for an account titled 'test'.