I have cobbled together some code found here and elsewhere that is triggered to run by a certain rule in Mail. The script is designed to add a reminder to be completed the following day, titled with the subject line of the email, a URL of the message added to the reminder ‘notes’, and an iMessage sent to my phone.
This all works great so long as there is only one email message processed by that rule. If there are two messages, the repeat with statement will run twice (as it should) but the message information such as the subject or the URL will not update. As a result I get a duplicate reminder. I’m new to AppleScript so I’m not sure where I’m going wrong – any ideas?? Here’s the script:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set emailSubject to the subject of eachMessage
set remindMeDate to (current date) + 1 * days
set time of remindMeDate to 60 * 60 * 9
# Get the unique identifier (ID) of selected email/message
set theOrigMessageId to the message id of eachMessage
#we need to encode % with %25 because otherwise the URL will be screwed up in Reminders and you won't be able to just click on it to open the linked message in Mail
set theUrl to {"message:%3C" & my replaceText(theOrigMessageId, "%", "%25") & "%3E"}
tell application "Reminders"
#Using the work reminder list below
tell list "Work"
# create new reminder with proper due date, subject name and the URL linking to the email in Mail
make new reminder with properties {name:emailSubject, remind me date:remindMeDate, body:theUrl}
end tell
end tell
tell application "Messages"
set targetBuddy to "+12223334444"
set targetService to (id of 1st service whose service type = iMessage)
set textMessage to "ALERT: " & emailSubject
set theBuddy to buddy targetBuddy of service id targetService
send textMessage to theBuddy
end tell
end repeat
end tell
end perform mail action with messages
end using terms from
# string replace function
# used to replace % with %25
on replaceText(subject, find, replace)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
As I’m fighting against an other script triggered by a rule, I tested your script using this edited version:
# four extraneous instructions used for tests
tell application "Mail"
set test_list to selection
tell me to perform mail action with messages (test_list)
end tell
using terms from application "Mail"
on perform mail action with messages theMessages --for rule theRule
--tell me to say "I received " & (count theMessages) & " messages"
tell application "Mail"
set report to {}
repeat with i from 1 to count theMessages
set eachMessage to contents of theMessages's item i
set emailSubject to the subject of eachMessage
# without "tell me to" the nex instruction issue a non fatal --> error number -10004
tell me to set remindMeDate to (current date) + 1 * days
set time of remindMeDate to 9 * hours # neater than your original syntax
# Get the unique identifier (ID) of selected email/message
set theOrigMessageId to the message id of eachMessage
#we need to encode % with %25 because otherwise the URL will be screwed up in Reminders and you won't be able to just click on it to open the linked message in Mail
set theUrl to {"message:%3C" & my replaceText(theOrigMessageId, "%", "%25") & "%3E"}
set end of report to theUrl
-- tell me to say "message number " & i
tell me to delay 0.1
end repeat
end tell
--log "prèt à sortir"
set the clipboard to my recolle(report, linefeed)
end perform mail action with messages
end using terms from
# string replace function
# used to replace % with %25
on replaceText(subject, find, replace)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
With two messages selected I got this log events :
tell application "Mail"
get selection
--> {message id 227511 of mailbox "INBOX" of account id "0EC90E4E-2781-40D8-89F3-FE7474BB9DD0", message id 227583 of mailbox "INBOX" of account id "606ACFB8-F0A8-4845-8EC4-E3F5A6D1979C"}
get message id 227511 of mailbox "INBOX" of account id "0EC90E4E-2781-40D8-89F3-FE7474BB9DD0"
--> message id 227511 of mailbox "INBOX" of account id "0EC90E4E-2781-40D8-89F3-FE7474BB9DD0"
get subject of message id 227511 of mailbox "INBOX" of account id "0EC90E4E-2781-40D8-89F3-FE7474BB9DD0"
--> "Eva Green : "Je viens d'une autre planète""
end tell
tell current application
current date
--> date "lundi 16 janvier 2017 Ã 21:10:11"
end tell
tell application "Mail"
get message id of message id 227511 of mailbox "INBOX" of account id "0EC90E4E-2781-40D8-89F3-FE7474BB9DD0"
--> "100084.517919641.201701161706167475358.0006349757@redaction.telerama.fr"
get message id 227583 of mailbox "INBOX" of account id "606ACFB8-F0A8-4845-8EC4-E3F5A6D1979C"
--> message id 227583 of mailbox "INBOX" of account id "606ACFB8-F0A8-4845-8EC4-E3F5A6D1979C"
get subject of message id 227583 of mailbox "INBOX" of account id "606ACFB8-F0A8-4845-8EC4-E3F5A6D1979C"
--> "Re: AS Bug with 10.12?"
end tell
tell current application
current date
--> date "lundi 16 janvier 2017 Ã 21:10:11"
end tell
tell application "Mail"
get message id of message id 227583 of mailbox "INBOX" of account id "606ACFB8-F0A8-4845-8EC4-E3F5A6D1979C"
--> "B676143F-A62F-4176-80BD-D47BC2329B00@gmail.com"
end tell
tell application "Script Editor"
set the clipboard to "message:%3C100084.517919641.201701161706167475358.0006349757@redaction.telerama.fr%3E
message:%3CB676143F-A62F-4176-80BD-D47BC2329B00@gmail.com%3E"
end tell
After disabling the four calling instructions I saved it as a rule and never got more than one url in the clipboard.
I enabled the first say instruction and it always said “I received 1 message(s)”
Hoping that it would help, I used a loop using an index after getting the same behavior with the original one.
But no positive effect.
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) lundi 16 janvier 2017 21:24:16
It’s strange that it will work with a selection but not when triggered by a rule. I’m starting to wonder whether this is just a mysterious Mail/Rule/Applescript bug.
I just found something of a workaround, though may not be interesting for everyone:
if I delete the messages before the next repeat (delete theMessage), the script moves on to the next message and completes as it should. This works for me as I’m processing these mails and don’t need the originals, but of course may not be suitable for everyone… Hope this can serve as a hint for others.
Belatedly, welcome to MacScripter and thanks for posting your insight into getting round this problem!
I’ve been looking into it myself for the past couple of days, but am still puzzled as to how the list can contain more than one item when invoked by an incoming mail rule (does the rule save up all the matching messages and then pass them to the script together?), why the script sees all the items in the list as same message, and how deleting the message in Mail manages to change what’s in the list!
It turns out that a less destructive alternative to deleting the message is to move it to a mailbox “On My Mac” — that is, to set its mailbox to that mailbox. “On My Mac” mailboxes belong to the application rather to an account.
using terms from application "Mail"
on perform mail action with messages theMessages
tell (current date) + days to set {time, remindMeDate} to {9 * hours, it}
repeat with thisMessage in theMessages
tell application "Mail"
set {subject:emailSubject, message id:theOrigMessageID} to thisMessage
set thisMessage's mailbox to mailbox "Noted for action" -- Or whatever name or slash-separated path's used.
end tell
set theURL to "message:%3C" & my replaceText(theOrigMessageID, "%", "%25") & "%3E"
makeReminder(emailSubject, remindMeDate, theURL)
-- textMe(emailSubject) -- Not tested.
end repeat
end perform mail action with messages
end using terms from
on replaceText(txt, find, replace)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set textItems to txt's text items
set AppleScript's text item delimiters to replace
set output to textItems as text
set AppleScript's text item delimiters to prevTIDs
return output
end replaceText
on makeReminder(nameText, remindMeDate, bodyText)
tell application "Reminders"
activate
make new reminder at list "Work" with properties {name:nameText, remind me date:remindMeDate, body:bodyText}
end tell
end makeReminder
on textMe(msg)
tell application "Messages"
set targetBuddy to "+12223334444"
set targetService to (id of 1st account whose service type = iMessage)
set textMessage to "ALERT: " & msg
set theBuddy to participant targetBuddy of account id targetService
send textMessage to theBuddy
end tell
end textMe