SO when I say Newbie, I am a newbie to applescript and MAC OSX but have been an IT professional with Windows Server for 30+ years. I am struggling a little with learning another new scripting language especially one for which I don’t have a reference manual of any sort.
I want to be able to trigger a script in the mail app when a message is received from a particular sender. I would like to redirect this message with a different subject. I found this script online, but I found it in a forum asking why it copied the content into the message twice… i thought I’d have a play about and see what i could learn from deconstructing it
using terms from application "Mail"
on perform mail action with messages theMessages
repeat with thisMessage in theMessages
tell application "Mail"
set the newSubject to subject of thisMessage
set newMessage to redirect thisMessage with opening window
tell newMessage
make new to recipient at beginning of to recipients with properties {address:"sailingbike.1730e@m.evernote.com"}
set the sender of newMessage to "ian.davies.asl@gmail.com"
set subject of newMessage to newSubject & " @amazon #receipt #amazon"
-- delay 3
send
end tell
end tell
end repeat
end perform mail action with messages
end using terms from
So I tried to change the format and instead of redirecting a message I thought I would create a new one doing this:
set notebookName to "@amazon"
set tags to "#receipt #amazon"
using terms from application "Mail"
on perform mail action with messages theMessages
repeat with thisMessage in theMessages
tell application "Mail"
set myBody to content of thisMessage
set the mySubject to subject of thisMessage & notebookName & tags
set newmessage to make new outgoing message with properties {visible:true, subject:mySubject, content:myBody}
end tell
delay 1
-- send newMessage
end repeat
end perform mail action with messages
end using terms from
this does nothing at all…
if I remove the on perform mail action and the repeat with loop I get this code which produces a new email
set notebookName to "@amazon"
set tags to "#receipt #amazon"
using terms from application "Mail"
tell application "Mail"
set newmessage to make new outgoing message with properties {visible:true, subject:"mySubject", content:"myBody"}
end tell
delay 1
-- send newMessage
end repeat
end perform mail action with messages
end using terms from
Can somebody provide me with some guidance as to:
why the first script copies the content multiple times
why I can create a new message but not inside an on perform mail action
If you google AppleScript Language Guide, then you should find the reference for AppleScript.
I have no clue what so ever with regards to Mail, but maybe you should try to search this forum, as problems concerning perform mail action, have been coming up from time to time, -many which have been solved.
Hi. The on perform mail action handler is either broken or buggy for at least the last two versions of Mac OS; the handler’s theMessages variable is a filter for a message set, but you don’t really need it, as you can roll your own. You also don’t need using terms from, since you are directly addressing the app.
My example assumes that your rule triggers a script upon receipt from a particular sender. More than one message may arrive simultaneously with any rule evaluation, so I’d search recent messages for a match.
set senderTarget to "JoeBlow@wherever.com" --adjust to a real address or fail !
set counter to 1
tell application "Mail"
repeat until (extract address from (inbox's message counter)'s sender) = senderTarget
set counter to counter + 1
end repeat
--do other actions with inbox's message counter's properties
end tell
It is very buggy. I think the reference to the sent message is virtual (on the internet). For some reason that might cause the two references in the inbox. If you want to get a good reference to the message, then move the message to another mailbox.
Still need to read your post more.
Edited: and btw, you can use the id of the incoming message to identify it after you move it.
What I did was create a new mailbox. Here’s the script that looks at the messages in the new mailbox:
using terms from application "Mail"
on perform mail action with messages these_msgs
tell application "Mail"
set the_mailbox to first mailbox whose name is "IncomingPopMessages"
set the_msgs to every message of the_mailbox
repeat with this_msg in the_msgs
set t to content of this_msg
try
display dialog t
-- continue processing
delete this_msg
on error
delete this_msg
end try
end repeat
end tell
end perform mail action with messages
end using terms from
Basically, what you do is make a rule to send all incoming messages to the new mailbox “IncomingPopMessages”. From there, the messages will have valid references and you can identify your messages by its internet id.
You need to create a rule to copy the messages from the inbox to this new mailbox. I think that’s how it went.
That was a bad script. I was cutting it down from testing.
The hard part about Mail scripting is you have to send yourself mail when testing! :roll eyes: Maybe something like this:
using terms from application "Mail"
on perform mail action with messages these_msgs
tell application "Mail"
set the_mailbox to first mailbox whose name is "IncomingPopMessages"
set the_msgs to every message of the_mailbox
repeat with this_msg in the_msgs
set t to content of this_msg
try
display dialog t
-- continue processing
on error err_msg
display dialog err_msg
end try
delete this_msg
end repeat
end tell
end perform mail action with messages
end using terms from