How to read the subject of an email using AppleScript - email

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?

Related

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

Microsoft Outlook Message When Emailing in Progress Application

There is a below code, does not work
FOR EACH customer where customer.contactmethod = 'E':
MESSAGE "EMAILING TO" customer.customerCode customer.CustomerName.
RUN Emaling.p (INPUT parameter 1,INPUT pramater 2 ....)
END.
When this above loop executes it, it messages the first customer only. The reason I found is the outlook message.
The Outlook message is below
"A program is trying to send an email message on your behalf. if this is unexpected, clikc Deny and verify your antivirus software is upto date......................................."
if user click allow/deny in the outlook message, it does not message the second customer as it is stuck in first customer.
How can I suppress this outlook message using 4gl code?
The key to that is inside your "Emaling.p" procedure. Whatever technique that code is using to send email needs to be adjusted to bypass the security check.
Or you could disable whatever it is that is popping up that message. It looks like some over-zealous Windows security setting.
In any case it has nothing much to do with the 4gl code that you have shown.
Having said that -- your code is obviously incomplete and as shown would not be expected to work at all. Maybe it doesn't really look anything like that but a more workable version of the code above would be:
FOR EACH customer NO-LOCK where customer.contactmethod = 'E': /* without the NO-LOCK you might be aggravating a transaction scoping problem... */
MESSAGE "EMAILING TO" customer.customerCode customer.CustomerName.
do on error undo, leave: /* this may help to keep processing even if errors occur */
RUN Emaling.p (INPUT parameter 1,INPUT pramater 2 ....). /* you forgot the "." and knowing the rest of the parameters might help to suggest ways to get around the reported problem */
end.
END.

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

Getting at a Message from a Mail Rule in Applescript

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