public
Last active

Simple Inbox Archiving Script for Apple Mail

  • Download Gist
gistfile1.scpt
AppleScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
-- See article here: http://daringfireball.net/2007/07/simple_inbox_sweeper
 
-- The following should be one long line:
set _description to "All unflagged, read messages in each IMAP account
inbox will be moved to the “Archive” mailbox corresponding to that
account. This action is not undoable."
 
tell application "Mail"
display alert "Archive read messages from IMAP inboxes?" buttons ¬
{"Cancel", "Archive"} cancel button 1 message _description
repeat with _acct in imap accounts
set _acct_name to name of _acct
set _inbox to _acct's mailbox "INBOX"
try
set _archive_box to _acct's mailbox "Archive"
on error
display alert "No “Archive” mailbox found for account “" & ¬
_acct_name & "”."
return -- Stop the script
end try
-- Begin update for OS X 10.7.0
set _msgs_to_move to (a reference to ¬
(every message of _inbox ¬
whose flagged status is false and read status is true))
set _msg_list to contents of _msgs_to_move
if (_msg_list's length > 0) then
move _msgs_to_move to _archive_box
end if
-- End update for 10.7.0
end repeat
end tell

Apple Mail in OS X 10.7.0 has a type coercion bug which breaks the original version of this script. The following should work, and prior to 10.7.0 did work, and probably will work again in a future 10.7.x software update:

    set _msg_list to (every message of _inbox ¬
        whose flagged status is false and read status is true)
    if (_msg_list's length > 0) then
        move _msg_list to _archive_box
    end if

But it doesn't work in 10.7.0. Hence the rather convoluted workaround wherein we start with a reference to the list of read unflagged messages, then create our list of messages to move by asking for the contents of the reference. AppleScript, baby.

If you want to go full integration you can add this as a "Service" from Automator - and attach a keyboard shortcut - since apple removed the scripts menu from Mail.app a few versions ago.

http://culturedcode.com/things/wiki/index.php/Creating_a_service_for_Mail_in_Snow_Leopard

Process is more or less here - just choose "run applescript" as the sole action and use the snippet above.

@jerrod Assuming it still exists in Lion, you can use the system script menu (in Snow Leopard, it's a preference in AppleScript Editor). You can also use FastScripts.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.