Applescript delete mail by subject - email

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

Related

AppleScript for sending emails from a list

I have an AppleScript which starts a new email in Mail, and fills in the Body with the contents of a user-selected file.
The problem is that the first line of the body in the new email is always blank -- i.e. there's a line-break before the body from the user-selected file. No matter what I do, even if I hardcode the body into the script (e.g. "Hello world!") it starts on the second line of the email.
Can anyone please suggest a way to fix this?
property theToAddress : "john#example.com"
property theSubject : "Example subject"
set theTo to theToAddress
set theSubjectLine to theSubject
set theBody to choose file with prompt "Please select a file for the body of the email:"
set theMessageBody to read theBody as «class utf8»
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubjectLine, content:theMessageBody, visible:true}
tell theNewMessage
make new to recipient with properties {address:theTo}
end tell
end tell

AppleScript code for copying new email subject

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

Edit the subject of a forwarded message using Applescript - encountering error when editing subject line

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"

Mavericks Mail Applescript Changes

Mavericks has been pretty good so far, but one truststy Applescript I'd been using for years decided it didn't wan to work anymore. The script is here:
tell application "Mail"
set the clipboard to (content of first message of ¬
inbox whose subject contains "2013-11-05") as string
end tell
The job of the script is to get the content from an email with a subject "J-List Reports 2013-11-05") (obviously this changes each day). If I change the script to look for "J-List reports" with no date it works fine, but it gets the wrong email since I can't specify the date (it looks for the first message that happens to have this string in the subject). If any numbers are in the Applescript, trying to force it to find the correct email, I get
"Mail got an error: can't get message 1 of inbox whose subject
contains 2013-11-05"
even though nothing else has changed.
Can anyone suggest a way to specify the correct mail, perhaps any mail whose subject contains "J-List reports" and whose month is 11 and whose day is 5? I spent a couple hours but couldn't get it to work for me.
This worked for me:
tell application "Mail"
content of message 1 of mailbox "[Gmail]/All Mail" of account "Gmail" where subject contains "2013-11-05"
end tell
set the clipboard to result
The messages are sorted from newest to oldest, so message 1 should be the newest message.
tell application "Mail" to messages of inbox and tell application "Mail" to messages of mailbox "INBOX" of account "Gmail" returned an empty list for some reason.
this works fine for me
tell application "Mail"
try
set the clipboard to (content of first message of ¬
inbox whose subject contains "2013-11-05") as string
on error errMsg
display dialog "an error: " & errMsg
end try
end tell
Try changing the string to something you know exists , I get the same error when I search for an email message where there are no matches. I suspect that you are not including the full error message here either
rror "Mail got an error: Can’t get message 1 of inbox whose subject contains \"2013-11-05\". Invalid index." number -1719
invalid index is important to a diagnosis because it tells us exactly what the problem is. There is no message that can be identified where the subject contains 2013-11-05
so you should use a try statement and then decided what to do from there.
Mail Version 7.0 (1816)
OS 10.9 build 13a603

AppleScript can't read Mail.app's messages

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