As an excuse to learn Applescipt, I writing a script to be attached to a Mail rule to filter messages. I scrounged around and put together this much code:
on perform_mail_action(theData)
tell application "Mail"
set theSelectedMessages to |SelectedMessages| of theData
set theRule to |Rule| of theData
repeat with a from 1 to count theSelectedMessages
set theMessages to selection
set theMessage to item 1 of theMessages
end repeat
end tell
end perform_mail_action
I would have thought the rule would only pass one message, but you never know so the repeat makes sense. I presume "selection" is a pointer to an item in theSelectedMessages. What seems strange is "set theMessage to item 1 of theMessages". I would have thought you would code "set theMessage to selection". I want to get to the body, "content" of the message to test for certain words.
Thanks for any help,
Curt
There is an example rule script at /Library/Scripts/Mail Scripts/Rule Actions/Sample Rule Action Script.scpt. You will need to use the handler declaration that is set in Mail's scripting dictionary, i.e.
on perform mail action with messages theMessages for rule theRule
user866649s answer only works for me (macOS 10.13) when adding another line of code: using terms from application "Mail"
Following snippet displays the senders names of all messages that triggered the script:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with theMessage in theMessages
display dialog (sender of theMessage as string)
end repeat
end perform mail action with messages
end using terms from
Related
How to read the subject of an email using AppleScript?
I'm on Mail 16, AppleScript 2.8 .
This script is triggered by a rule in Mail app.
It DOES get triggered and it SHOULD get the subject of an email, and just display it.
I get my first dialog displaying "here" , but then nothing else and no errors giving me a hint as to why not.
using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
display dialog length of these_messages as text
(* displays a 1, as there is 1 message *)
repeat with an_email in these_messages
display dialog "here"
(* displays 'here', so we know we are inside the loop *)
set subs to subject of an_email
display dialog subs as rich text
(* nothing is displyed, no error shows *)
end repeat
end tell
end perform mail action with messages
end using terms from
All exmaples Ive seen for this code should work, but most of them have been from 2015 and earlier. Maybe this isn't supposed to work anymore?
I'm setting up a rule in my Mail app to execute the AppleScript below when an email from a specific sender arrives. My goal is to copy the incoming email's subject line, however, this script is copying the subject of the currently selected email in the main inbox and not the arriving email's.
Is there a better way to achieve my goal?
tell application "Mail"
set _msgs to selected messages of message viewer 0
if (_msgs is not equal to missing value) then
set _msg to first item of _msgs
set the clipboard to (subject of _msg) as string
end if
end tell
You need to use the built-in handlers that Mail exposes to access the message associated with a rule activation; selected messages of message viewer 0 just accesses the active Mail window (as you describe). Instead, use Mail's on perform mail action handler to correctly access the message. See this question (and this answer) for more information.
Here is your code, re-written to correctly copy the subject line of incoming emails that satisfy the provided rule criteria. It's working for me on macOS Catalina 10.15.5:
using terms from application "Mail"
on perform mail action with messages _msgs for rule _rule
if (_msgs is not equal to missing value) then
set _msg to first item of _msgs
set the clipboard to (subject of _msg) as string
end if
end perform mail action with messages
end using terms from
I have been trying to write some Applescript to forward a message and to edit the subject. Currently I am working on the forwarding and the populating of the recipient and the subject line.
When I run this it gives me the error:
"Mail got an error: Can't set subject of item to any."
Here is the code:
tell application "Mail"
set theSelection to selection
set theForwardedMessage to forward (item 1 of theSelection) with opening window
tell theForwardedMessage
make new to recipient with properties {address:"address#blahblah.com"}
set subject to "blahblahblah"
end tell
end tell
I can't figure out why it's telling me it can't edit the subject line. Any pointers?
i hope this will help a little bit, but i don't fully understand your script and question.
set recipientName to "ZWEKKERBOY"
set recipientAddress to "john#doe.com"
set theSubject to "Subject here"
set theContent to "Content"
--Mail Tell Block
tell application "Mail"
--Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
--Set a recipient
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
--Send the Message
send
end tell
end tell
-- credit to maker!
You can change the
set theSubject to "Subject here"
line
to
set theSubject to subject
-- that's "blahblahblah"
Is there a way to delete mails by the subject, i'm trying to make an applescript that deletes mails with a certain subject from my 'Sent' mailbox. I have tried these scripts this with a test mail:
tell application "Mail"
set theMails to every message of (every mailbox whose name is "Sent") whose subject is "Test Mail"
delete theMails
end tell
and:
tell application "Mail"
set theMails to {every message of inbox "Sent" whose subject is "Test Mail"}
delete theMails
end tell
Both scripts didn't work, and I didn't understand the returned errors so I could not find a solution. Help appriciated!
the code bellow works. better to use word "contains" instead of trying exact match of the subject
tell application "Mail"
set theMails to {every message of sent mailbox whose subject contains "Test Mail"}
delete theMails
end tell
I've had a script that used to work perfectly to check if I've received an email with a certain subject (below):
tell application "Mail"
check for new mail
repeat until (background activity count) = 0
delay 0.5
end repeat
try
return subject of (first message whose read status is false and subject contains "New newsletter ordered by")
end try
end tell
Now AppleScript refuses to read emails' subjects. Any ideas why? I'm assuming there's been an update that's broken it but I can't find any information on that.
Try
return subject of (first message of inbox whose read status is false and subject contains "New newsletter ordered by")
I have a 10.7.4 computer and get an error when there is no message with those properties are found. If there is an unread message containing that subject, the script (the one that says "first message of inbox") works fine.
This seems to work for me, I first had to select the first message and then I could get the subject:
tell application "Mail"
set theInbox to inbox
set theMessage to first message of theInbox whose read status is false
tell front message viewer to set selected messages to {theMessage}
set theMessages to (get selection)
repeat with theMessage in theMessages
get subject of theMessage as string
end repeat
end tell