Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Should I use Automator or Applescript to get a list of sent emails by date?

Should I use Automator or Applescript to get a list of sent emails by date?


I have Smart Mailboxes set up that filter by email type, was wondering if there were a somewhat simple way to gather the number of emails by date in a given Smart Mailbox without having to manually count them. There are a large number of emails, isn't extremely huge to count but would be nice to have it done with one click.


thanks in advance!

Mac mini, OS X Mavericks (10.9.5), 2012 Model, i7 QC, 16GB RAM

Posted on Jul 6, 2015 2:00 PM

Reply
15 replies

Jul 6, 2015 2:18 PM in response to Kappy

Kappy wrote:


Why not just select the smart folder and look on the upper part of the messages window where the folder count is displayed?


- The Smart Mailboxes do not give the option to filter by the sent date, only date received and date last viewed.


As for what to use, well that's determined by which tool can solve the problem, and which one you know how to use.


- The whole point of this post is to find out which tool can solve the problem best.

Jul 6, 2015 2:48 PM in response to LuxLuther

LuxLuther wrote:



- The Smart Mailboxes do not give the option to filter by the sent date, only date received and date last viewed.



On the assumption with emails that 'Date received' is the same as the date sent, I filter sent emails by date with two conditions in the Smart Mailbox.


From contains [my name or something in my email address]

Date received [today, is between, etc]


The count appears at the top of the window.


Will that suit your needs?


SG

Jul 6, 2015 2:59 PM in response to Kappy

A. Wouldn't work for what I am trying to do.


B. The whole point of a discussion forum is to ask questions and get answers. Besides a few official Apple Employees on staff, it isn't anyone's 'job' or obligation to answer questions. People like sharing knowledge out of kindness and generosity, and I do as well if it is something that I am experienced in.


You were rude to this person as well, yet someone had the good nature to help, so not everyone has your point of view:


Can I sort a list of items based on another list of items?

Jul 6, 2015 3:16 PM in response to LuxLuther

LuxLuther wrote:


simple way to gather the number of emails by date in a given Smart Mailbox without having to manually count them.


If there isn't enough information here on how to solve the problem, perhaps you could explain specifics on where the information falls short for your purposes rather than worrying more about the tone?🙂


Smart folders work well here for filtering sent messages. Which aspect doesn't fit your needs?


SG

Jul 6, 2015 3:16 PM in response to SGIII

SGIII wrote:


LuxLuther wrote:



- The Smart Mailboxes do not give the option to filter by the sent date, only date received and date last viewed.



On the assumption with emails that 'Date received' is the same as the date sent, I filter sent emails by date with two conditions in the Smart Mailbox.


From contains [my name or something in my email address]

Date received [today, is between, etc]


The count appears at the top of the window.


Will that suit your needs?


SG


Hi,


My end goal is to get the number of sent emails on a specific date from a Smart Mailbox, from a specific person (multiple people use the same mail account but have different signatures so that I can track them), into a specific cell on a numbers document. The original post was definitely lacking in clarity, but let me explain a bit further:


This is a work-related thing for tracking emails, and I fill out a weekly report to send out that has various email categories for each person. I cannot post a screenshot due to sensitive information and names, unfortunately.


I was unable to get mail to do this sort of thing with rules, search, filters, etc, so was looking to get into the direction of learning automator and/or Applescript to solve this problem and eventually others. Maybe even an internal app to do all of these but starting out slow with specific tasks to learn.


I hope that clarifies what I am asking for, thanks for the reply!

Jul 6, 2015 3:57 PM in response to LuxLuther

I'm familiar with scripting Numbers but not that familiar with scripting Mail. It does look to me as if the Mail-related Automator actions may save you some work compared with doing a plain AppleScript (unless others here post scripts). To some extent you'll be replicating filtering you already have in your existing Smart Mailboxes. In each case I think the basic approach will be to collect a list of filtered messages (via Find Mail Items or Filter Mail Items action), and then do a count of that list (which you may have to put in a short script in a Run AppleScript action).


SG

Jul 6, 2015 4:25 PM in response to LuxLuther

Should I use Automator or Applescript to get a list of sent emails by date?

The short answer to your question is: neither


The long answer is that AppleScript is your only possible/practical option.


My reasoning is based on the fact that Mail.app provides NO access to smart mailboxes. You can't create them, see them, query them, or do anything else with them. This severely limits your options.

For most people that will be the end of the story.


The extension of this is that you might be able to write an AppleScript to re-implement the Smart Mailbox rules to filter your inbox to apply the same rules (with the additional filter of your date range). This kind of logic is several orders of magnitude beyond what Automator can do, and may depend on the complexity of your smart mailbox rules.

Jul 6, 2015 7:37 PM in response to LuxLuther

To get you started, here's how you might approach this in Automator. It seems to be pretty easy.


User uploaded file


After you "Add.." and choose the desired outbox, the pane in the 'Get Specified Mail Items' action (the first one, collapsed in the screenshot) will display something like:


mailbox "Sent Messages" of account "name@yourdomain.com"


You can add conditions as needed to the 'Filter Mail Messages' action, to filter by subject, etc. Here you're essentially replicating the Smart Mailboxes.


The AppleScript in the 'Run AppleScript' action is of course bare bones and simply places the count in cell B2 of the frontmost document. You will want to adapt it to your setup, something like:


set document "MyDocumentName"'s sheet "MySheetName"'s table "MyTableName"'s cell "B2"'s value to theCount


The 'input' variable is an AppleScript list received from the previous action. The 'count' just counts the number of items that passed through the filters.


If you don't have too many separate signatures to tally, you could simply set up a separate workflow for each one, with a different value in the "contains" in Filter Mail Messages and designating a different destination cell in Numbers.


SG

Jul 7, 2015 10:20 AM in response to LuxLuther

LuxLuther wrote:


My end goal is to get the number of sent emails on a specific date from a Smart Mailbox, from a specific person (multiple people use the same mail account but have different signatures so that I can track them), into a specific cell on a numbers document. The


And here's a pure AppleScript (i.e., without Automator) approach that counts emails sent in the last 7 days by "signature" (which appears in a message's content) and places the counts on the system clipboard so you can click a cell in Numbers once and command-v to paste to get a two-column table of the counts.


Copy-paste into Script Editor (in Applications > Utilities), change the properties as needed, and click run.


It's easier in AppleScript to loop through different signatures. On the other hand, it's easier in Automator to replicate the filtering you already have in Mail's Smart Mailboxes.


SG


property numDays : 7

property theSigs : {"Signature1", "Signature2", "Signature3", "Signature4"}

property myMailbox : "Sent Messages"

property myAccount : "myusername@mydomain.com"

set sinceDate to (current date) - (numDays * days)

set pasteStr to ""

repeat with i from 1 to count theSigs

tell application "Mail"

set tgtBox to mailboxmyMailbox of accountmyAccount

set foundMsgs to (tgtBox's messages whose ¬

date sent > sinceDate and ¬

content contains theSigs's item i)


--add more filters after the 'whose' between the () above

set pasteStr to pasteStr & ¬


theSigs'sitemi & tab & (countfoundMsgs) & return

end tell

end repeat

set the clipboard topasteStr

--return pasteStr --uncomment for testing

Jul 7, 2015 6:57 PM in response to LuxLuther

One of the messier (at least for me) aspects of the AppleScript approach is dealing with the time string part of the date-time string that AppleScript stores when you think you are dealing with "just a date." Here's one way to get the the messages sent on a specific date (as opposed to in the last x days before the current date). It also works for multi-day periods (where beginDate and endDate are specified as different dates).


SG


property beginDate : date ("July 6, 2015") --> "Monday, July 6, 2015 at 12:00:00 AM"

property endDate : date ("July 6, 2015") --> "Monday, July 6, 2015 at 12:00:00 AM"

property theSigs : {"Signature1", "Signature2", "Signature3", "Signature4"}

property myMailbox : "Sent Messages"

property myAccount : "myusername@mydomain.com"

set endDate to endDate + (1 * days) --> "Tuesday, July 7, 2015 at 12:00:00 AM"

set pasteStr to ""

repeat with i from 1 to count theSigs

tell application "Mail"

set tgtBox to mailboxmyMailbox of accountmyAccount

set foundMsgs to (tgtBox's messages whose ¬

date sent > beginDate and ¬

date sent < endDate and ¬

content contains theSigs's item i)


--add more filters after the 'whose' between the () above

set pasteStr to pasteStr & ¬


theSigs'sitemi & tab & (countfoundMsgs) & return

end tell

end repeat

set the clipboard topasteStr

--pasteStr -- uncomment to debug

Should I use Automator or Applescript to get a list of sent emails by date?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.