Archive messages in Mail.app using a single keystroke

Mar 24, '11 07:30:00AM

Contributed by: Anonymous

Here's an AppleScript to archive messages in Mail.app using a single keystroke.

Unlike related hints, this script is designed to be launched with a single keystroke using a tool like FastScripts.

The basic logic flow: if a mailbox view is in the foreground, it will archive the selection; if not, it will skip archiving and (optionally) enter the keystroke you typed in the first place.

Steps:

More info is available here in my blog about it.

Here's the script:

(*
  # DESCRIPTION #
  
  This script files the selected Mail messages to the specified folder (Archive, by default).
  
  
  # LICENSE #

  Created by Dan Byler (contact: dbyler@gmail.com); released as public domain.
  
  
  # CHANGE HISTORY #
  
  0.1: Original release.

  
  # INSTALLATION #
  
  -  Copy to ~/Library/Scripts/Applications/Mail
   -  If desired, activate with Fastscripts
*)


--name of the mailbox you're filing to
property fileMailbox : "Archive"

--if composing a message, paste the character you typed?
property doPasteText : true
property escText : "\\" --if using backslash, remember to escape it

tell application "Mail"
  repeat with thisMessageViewer in message viewers
    
    -- Is the message viewer in front? Avoid accidentally triggering
    if window of thisMessageViewer is first window then
      
      set theSelectedMessages to selection --todo just use "selection"
      if theSelectedMessages is {} then
        return
      end if
      
      set visibleMessages to visible messages of thisMessageViewer
      set firstID to id of first item of theSelectedMessages
      set lastID to id of last item of theSelectedMessages
      
      -- Change selection
      
      repeat with i from 1 to (count visibleMessages)
        if id of item i of visibleMessages is firstID then
          try
            -- Make sure we're navigating to a valid message
            if class of item (i - 1) of visibleMessages is message then
              set thisMessageViewer's selected messages to {item (i - 1) of visibleMessages}
            else -- If not, it's probably a message thread; skip back another
              set thisMessageViewer's selected messages to {item (i - 2) of visibleMessages}
            end if
          on error --likely at top of viewer
            try
              set thisMessageViewer's selected messages to {item (i + 1) of visibleMessages}
            end try
          end try
        else if id of item i of visibleMessages is lastID then
          try
            set thisMessageViewer's selected messages to {item (i + 1) of visibleMessages}
            exit repeat
          end try
        end if
      end repeat
      
      -- Move messages
      repeat with theMessage in theSelectedMessages
        set theAccount to name of account of mailbox of theMessage
        move theMessage to mailbox fileMailbox of account theAccount
      end repeat
      
      return
    else
      if doPasteText is true then
        -- Let the original keystroke pass through.
        -- Uses paste to avoid an infinite script-triggering loop.
        tell application "System Events"
          set currentClipboard to the clipboard
          set the clipboard to escText
          keystroke "v" using {command down}
          delay 1
          set the clipboard to currentClipboard
        end tell
      end if
    end if
  end repeat
end tell

[crarko adds: I haven't tested this one. You should be able to bind this to a keyboard shortcut in Mail using the Keyboard Shortcuts System Preference.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110320181919317