Applescript to archive email in Mail.app - email

I need to write an Applescript for Mail.app that will take all messages in my Inbox and Sent Messages that are older than a certain # of days and move them into respective folders "On My Mac", or local folders.
The reason for this being my IMAP account has a 120 day quota limit, and I'd rather automate "archiving" my email to a local folder rather than doing it manually.

What have you tried so far? Your question is very broad. The following should get you started:
property secondsIn120Days : 10368000
tell application "Mail"
set theInbox to inbox
set dateToday to current date
set firstMessage to 1
set lastMessage to (get count of messages in theInbox)
repeat with thisMessage from lastMessage to firstMessage by -1
set currentMessage to message thisMessage of theInbox
set messageDate to date received of currentMessage
set timeDifference to dateToday - messageDate
if timeDifference ≥ secondsIn120Days then
(* In answer to your comment, any folder you create to archive
messages is going to be in the "On My Mac" directory. But say you
create a Smart Mailbox called "Mail Archive" then all you should
need are these lines... *)
set archiveMailbox to (mailbox ("Mail Archive" as string))
move currentMessage to archiveMailbox
end if
end repeat
end tell
UPDATE: Added response to comments in the code.

Related

Applescript to rule "sent mailbox zero"?

According to GTD and "inbox zero" I also wanted a corresponding "sent mailbox zero" by applying a rule in Apple Mail. (Why? Please, see the end of this question.)
Now, here is a simple Applescript solution: sortOut. How could it be more efficient?
The workflow is simply this
Loop through all sent messages that was a reply to some other message.
Get the message id of the message the sent message was a reply to.
Locate the mailbox for this (prior) message.
Any suggestions how to solve step 3 more efficiently will be appreciated, e.g. locating a mailbox directly based on message id, without having to use the message: protocol and then open the message and preferably not to code something like ... (in every mailbox) whose message id is mID which turns out to be rather slow.
tell application "Mail"
set i to 0
set mb1 to sent mailbox
repeat with m in ((messages of mb1) whose all headers contains "In-Reply-To")
set mID to content of header "In-Reply-To" of m
set cmd to "open 'message:" & mID & "'"
try
do shell script cmd
delay 1
set s to the selection
set m2 to item 1 of s
set mb2 to mailbox of m2
set mailbox of m to mb2
close front window
set i to i + 1
end try
end repeat
if i ≠ 1 then
set s to "s"
else
set s to ""
end if
set myMsg to (i & " message" & s & " moved successfully") as rich text
display notification myMsg with title "SortOut" subtitle "Sorting complete"
end tell
Some earlier discussions elsewhere indicate that some people may even wonder why I want this. My reason is simply that I like to have have all threads and conversations, concerning every project and deadline, collected together based on context.
When a project is finished I archive it as a completed entity for easy reference if I need to go back or follow up some things (e.g. when planning a yearly event, “Remember what I replied to this question last year?” That also means that all those conversations will have the same label in gmail.
So, instead of having almost 15000 sent emails in one mailbox, I now have them sorted into context based mailboxes—and the mailbox for sent messages is empty!
BTW, the Alfred workflow Mail Actions by Pedro Lobo is simply amazing for filing mail. My script sortOut does the rest ;-)
Sorry, Pedro, I didn't have enough reputation to post a link to your brilliant work.
I just updated the code but still could not locate the mailbox corresponding to a message id from the In-Reply-To header using Applescript (without the message:protocol).
tell application "Mail"
set i to 0
set mb1 to sent mailbox
repeat with m in ((messages of mb1) whose all headers contains "In-Reply-To")
set mID to content of header "In-Reply-To" of m
set cmd to "open 'message:" & mID & "'"
try
do shell script cmd
delay 1
set s to the selection
set m2 to last item of s
set mb2 to mailbox of m2
if name of mb2 does not contain "INBOX" then
set mailbox of m to mb2
close front window
set i to i + 1
end if
end try
end repeat
if i ≠ 1 then
set s to "s"
else
set s to ""
end if
set myMsg to (i & " message" & s & " moved successfully") as rich text
display notification myMsg with title "SortOut" subtitle "Sorting complete"
mb2
end tell

applescript to save email-attachments in a folder doesn't work in Yosemite

I've installed OS x 10.10.1
Now this script doesn't work:
tell application "Mail"
set theSelectedMessages to selection
set this_message to item 1 of the theSelectedMessages
tell this_message
set these_attachments to every mail attachment
repeat with z from 1 to the count of these_attachments
set this_attachment to item z of these_attachments
set this_name to the name of this_attachment as text
.........and so on
I get the message : error in the apple event Routine. The error comes in this line: "set these_attachments to every mail attachment"
In 10.9 the script worked.
WoWo
woide#wwp-service.de
G'day from the land of Oz
Your code, slightly modified, works. I think your use of 'selection' might be faulty, but haven't test it. Try 'selected mailbox'.
Regards
Brian Christmas
tell application "Mail"
set theSelectedMessages to messages of mailbox "* items to shift"
set this_message to item 1 of the theSelectedMessages
tell this_message
set these_attachments to every mail attachment
set AttachmentNames to {}
repeat with z from 1 to the count of these_attachments
set this_attachment to item z of these_attachments
set this_name to the name of this_attachment as rich text
set end of AttachmentNames to this_name as rich text
end repeat
end tell
end tell
AttachmentNames & return & z
--> {"2014-11-10 233839Z.zip", "2014-11-10 233839Z.zip", "(20141110233839Z 2).pdf", "2014-11-10 233839Z", "
", 4}

Applescript mail flagging not working in maverick?

I am working on applescript and the following does not work :
I've put the file in the script folder, created a rule which find messages but the script does never happen.
I tried to flag, change the background color to no avail. The rule "test" exists.
What is wrong here?
Code :
using terms from application "Mail"
-- on perform mail action with messages selectedMessages
set today to current date
set deleteDate to today - (7 * days)
on perform mail action with messages selectedMessages for rule test
tell application "Mail"
repeat with eachMessage in selectedMessages
-- repeat with a from 1 to 5
--- Process the current message
-- set background color of eachMessage to white
-- if date received of eachMessage in selectedMessages is after deleteDate then
set background color of eachMessage to white
set flagged status to false
-- end if
-- display dialog theText
end repeat
end tell
end perform mail action with messages
end using terms from

AppleScript and Mail.app: checking if new messages contain a string

I'm writing a script to check if a particular web form has been submitted.
The script so far reads thusly:
tell application "Mail"
check for new mail
set newmail to get the unread count of inbox
repeat with msg in newmail
if msg's subject contains "New newsletter built by" then
return msg's subject
end if
end repeat
end tell
I've got an unread email in my inbox for the script to work with, but I still get an error:
error "Mail got an error: Can’t make 1 into type specifier." number -1700 from 1 to specifier
Any help at all will be appreciated.
Cheers!
tell application "Mail"
check for new mail
repeat until (background activity count) = 0
delay 0.5 --wait until all new messages are in the box
end repeat
try
return subject of (first message of inbox whose read status is false and subject contains "New newsletter built by ")
end try
end tell
Applescript is a bit tricky. It looks like you're trying to parse the count of the inbox, not the actual inbox.
Try this script instead:
tell application "Mail"
check for new mail
-- instead of getting the unread count of inbox
-- let's set an Applescript variable to every message of the inbox
set myInbox to every message of inbox
repeat with msg in myInbox
-- and look at only the ones that are unread
if read status of msg is false then
-- and if the subject of the unread message is what we want
if msg's subject contains "New newsletter built by" then
-- return it
return msg's subject
end if
end if
end repeat
end tell
I've actually solved the issue, so I'm going to post it here should anyone else need help. Check it:
tell application "Mail"
check for new mail
set checker to (messages of inbox whose read status is false)
set neworder to number of items in checker
if neworder > 0 then
repeat with i from 1 to number of items in checker
set msg to item i of checker
if msg's subject contains "New newsletter built by " then
return msg's subject
end if
end repeat
end if
end tell
adding to what jackjr300 said....
tell application "Mail"
set x to unread count of inbox
check for new mail
delay 3
set y to unread count of inbox
set z to y - x
end tell
if x = y then
say "There is nothing to report"
end if
if z = 1 then
say "You have one new message"
end if
if z = 2 then
say "You have two new messages"
end if
if z = 3 then
say "You have three new messages"
end if
if z = 4 then
say "You have four new messages"
end if
if z = 5 then
say "You have five new messages"
end if
if z = 6 then
say "You have six new messages"
end if
if z = 7 then
say "You have seven new messages"
end if
if z = 8 then
say "You have eight new messages"
end if
if z = 9 then
say "You have nine new messages"
end if
if z = 10 then
say "You have ten new messages"
else
say "You have more than ten new messages"
end if

How can i modify this applescript to apply to selected emails rather than selected mailboxes?

tho is my first post - glad to have found this community :)
I'm afraid I have limited applescript knowledge but I discovered a script that will create a new message with recipients based on senders of emails in a particular mailbox.
This is almost what I need...
What I really need is a script that will create a new message with recipients based on senders of all selected emails.
I am actually astonished that this is not an option in more mail clients. It's not an option in Mail, nor Mailmate, nor Entourage.
The code I am working with is...
tell application "Mail"
set theSenderList to {}
set theMailboxes to the selected mailboxes of message viewer 0
repeat with aMailbox in theMailboxes
repeat with aMessage in messages of aMailbox
set end of theSenderList to sender of aMessage
end repeat
end repeat
set newMessage to make new outgoing message with properties {visible:true, subject:"Answers to ", content:"Here is the solution to "}
repeat with aSender in theSenderList
tell newMessage
make new bcc recipient at end of bcc recipients with properties {address:aSender}
end tell
end repeat
end tell
To access the selected messages, you can simply use selected messages instead of selected mailboxes. Once you've made this change, the loop over the selected mailboxes no longer applies. Instead, keep just the inner loop over messages.
...
set theMessages to the selected messages of message viewer 0
repeat with aMessage in theMessages
set end of theSenderList to sender of aMessage
end repeat
...
To find this sort of thing yourself:
open the AppleScript editor,
open the library window (Window → Library, or ⇧⌘L)
add the application to the library (click the plus symbol in the library window),
open the scripting dictionary for the application (double click the application in the library window)
Select the Mail suite, then the message viewer class in the browser.
You can also open a scripting dictionary via File → Open Dictionary..., or with ⇧⌘O.
In the properties for the message viewer, you'll see both "selected messages" and "selected mailbox".
The dictionary viewer uses a number of icons in the browser to indicate the type for a term
Suite: an "S" in an orange square.
Class: a "C" in a purple square.
Command: a "C" in a blue circle.
Element: an "E" in an orange square.
Property: a "P" in a purple square.
See Also
Where to find what commands/properties are available for AppleScript in Microsoft Outlook 2011?