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

AppleScript for Mail

Is it possible to make an applescript that will check for unread messages through the whole mailbox, then tell me how many there are, then say who each unread mail is from. Ask me to have it read the subject then open, mark as read, or leave for later?

MacBook Air, OS X Mavericks (10.9.2)

Posted on Apr 10, 2014 1:31 AM

Reply
9 replies

Apr 10, 2014 5:30 AM in response to hunterh731

How about this:


tell application "Mail"

set unread_messages to (messages of mailbox "INBOX" of account "ICloud" whose read status is false)

set unread_count to countunread_messages


display dialog "Total unread messages: " & unread_count



repeat with each_message in unread_messages

set the_subject to subject of each_message

set the_sender to sender of each_message

set the_action to (button returned of (display dialog "Unread message:" & return & "Subject: " & the_subject & return & "Sender: " & the_sender & return buttons {"Open", "Mark as read", "Leave for later"}))

if the_action is "Open" then open each_message

if the_action is "Mark as read" then set read status of each_message to true


end repeat



end tell


You may need to change the account name (and possibly the name of the Inbox) in line 2 to match your own settings.


HTH


H

Apr 10, 2014 6:42 AM in response to HD

That's a nice piece of code 🙂, but it lacks one thing - a 'cancel' operation. Once the script runs, the user will have to go through the entire list of unread messages. There's no way out except a force quit.


Also, the first dialog box could do without the cancel button since both 'cancel' and 'OK' do the same thing (i.e., dismiss the dialog).


display dialog "Total unread messages: " & unread_count buttons {"OK"} default button "OK"

Apr 10, 2014 8:12 AM in response to softwater

Ah, many thanks. That gives us



tell application "Mail"

activate

tell (messages of mailbox "INBOX" of account "ICloud" whose read status is false)

set unread_messages to it

set unread_count to count

end tell

display dialog "Total unread messages: " & unread_count buttons {"OK"} default button 1

repeat with each_message in unread_messages

set the_subject to subject of each_message

set the_sender to sender of each_message

set the_action to choose from list {"Open", "Mark as read", "Keep for later"} default items {} with prompt "Subject: " & the_subject & return & return & "Sender: " & the_sender & return & return & "What do you want to do with this message?" & return with title "Unread message" without multiple selections allowed

if the_action is false then return

if item 1 of the_action is "Open" then open each_message

if item 1 of the_action is "Mark as read" then set read status of each_message to true

end repeat

end tell

Apr 10, 2014 12:03 PM in response to HD

That was a great code, I would like to add a "delete option, and I'm not quite sure how to do that? I made a change so it will speak the number of unread messages.


tell application "Mail"


activate

tell (messages of mailbox "INBOX" of account "GMAIL" whose read status is false)

set unread_messages to it

set unread_count to count

end tell

say "Total unread messages: " & unread_count

repeat with each_message in unread_messages

set the_subject to subject of each_message

set the_sender to sender of each_message

set the_action to choose from list {"Open", "Mark as read", "Keep for later"} default items {} with prompt "Subject: " & the_subject & return & return & "Sender: " & the_sender & return & return & "What do you want to do with this message?" & return with title "Unread message" without multiple selections allowed

if the_action is false then return

if item 1 of the_action is "Open" then open each_message

if item 1 of the_action is "Mark as read" then set read status of each_message to true

end repeat

end tell

Apr 10, 2014 2:30 PM in response to hunterh731

tell application "Mail"

Here you go. It also marks the deleted messages are read.


activate

tell (messages of mailbox "INBOX" of account "ICloud" whose read status is false) to set {unread_count, unread_messages} to {count, it}



say "Total unread messages: " & unread_count

repeat with each_message in unread_messages

set the_subject to subject of each_message

set the_sender to sender of each_message

set the_action to choose from list {"Open", "Mark as read", "Keep for later", "Delete"} default items {} with prompt "Subject: " & the_subject & return & return & "Sender: " & the_sender & return & return & "What do you want to do with this message?" & return with title "Unread message" without multiple selections allowed

if the_action is false then return

if item 1 of the_action is "Open" then open each_message

if item 1 of the_action is "Mark as read" then set read status of each_message to true

if item 1 of the_action is "Delete" then

set read status of each_message to true


deleteeach_message

end if

end repeat

end tell

May 21, 2014 2:03 PM in response to HD

just like to add in my script based on the above

i've been looking for something similar to this for a while and tried allsorts of snipets from many of the posts so heres my attempt



this actually uses the mailbox or folder that is selected in mail.app so you dont have to explicitly tell it which folder you want to use (had that part of the code from another post somewhere so credit to them not me

🙂



display alert "Please note!" message "This script may take a while if you have a lot of mailboxes selected. Do you want to continue?" buttons {"Cancel", "Yes"} cancel button "Cancel"

tell application "Mail"

tell front message viewer

set theSelectedMailboxes to selected mailboxes

repeat with a from 1 to length of theSelectedMailboxes

set theCurrentMailbox to itema of theSelectedMailboxes


processMailbox(theCurrentMailbox) of me

end repeat

end tell

end tell

on processMailbox(theMailbox)



tell application "Mail"

set unread_messages to (messages of theMailbox whose read status is false)

set unread_count to countunread_messages


--display dialog "Total unread messages: " & unread_count

repeat with each_message in unread_messages

set the_subject to subject of each_message

set the_sender to sender of each_message

set the_action to choose from list {"Open", "Mark as read", "Keep for later", "Delete"} default items {} with prompt "Subject: " & the_subject & return & return & "Sender: " & the_sender & return & return & "What do you want to do with this message?" & return with title "Unread message" without multiple selections allowed

if the_action is false then return

if item 1 of the_action is "Open" then open each_message

if item 1 of the_action is "Mark as read" then set read status of each_message to true

if item 1 of the_action is "Delete" then

set read status of each_message to true

set junk mail status of each_message to true

end if

end repeat

end tell


tell application "AppleScript Editor"


activate

display dialog "Do you want to delete the email?" buttons {"yes", "no"} default button 2

if result = {button returned:"yes"} then

tell application "Mail"


# deletes every message that is Read and marked as Junk.


# Any that are not read AND junk will remain.

delete (every message of theMailbox whose junk mail status is true and read status is true)

end tell

else

if result = {button returned:"no"} then


end if

end if

end tell

end processMailbox

AppleScript for Mail

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