(* Open Mailbox Rule Action Script This script opens a message viewer window for a mailbox if a rule moves messages to that mailbox. Can be used on as many rules as you want. Save as say: "/Library/Scripts/Mail Scripts/Rule Actions/Open Mbox.scpt" Copyright (c) 2004 Kim Holburn kim@holburn.net Released under the GPL v2: http://www.gnu.org/copyleft/gpl.html I got some ideas from the Apple Sample scripts and help when I was despairing of getting applescript to do what I wanted from bjterry@adelphia.net on the applescript-users mailing list: http://www.lists.apple.com/mailman/listinfo/applescript-users *) using terms from application "Mail" on perform mail action with messages theMessages for rule theRule tell application "Mail" set theText to "This AppleScript is an AppleScript rule action" & return & return & "To view this script, hold down the option key and select it again from the Scripts menu." set madeWindow to 0 set theMboxName to "" try -- If this is not being executed as a rule action, -- getting the name of theRule variable will fail. set theRuleName to name of theRule if should move message of theRule then set mbox to move message of theRule set theMboxName to name of mbox end if set theText to "" end try if theText is not equal to "" then display dialog theText buttons {"OK"} default button 1 else if theMboxName is not equal to "" and theMboxName is not equal to "Junk" then -- try to determine if the mailbox is already open -- for some reason you have to do it this way in mail -- other ways that look like equivalents to me -- simply didn't work. Bugs in Mail.app? set selectedMailboxes to selected mailboxes of (every message viewer where it's visible is true) -- it's a list of lists repeat with currentMessageViewMailboxes in selectedMailboxes repeat with currentMailbox in currentMessageViewMailboxes set nmb to "" set namb to "" set nambox to "" set ncmb to "" set ncmbox to "" -- if any of these properties aren't there you get an error --which kill script execution. -- So you have to try to set these separately. -- otherwise you might bomb out on the first one. try set nmb to name of currentMailbox end try try -- have to set these separately. --So you really try each one and don't bomb out on the first one. set amb to account of currentMailbox set namb to name of amb end try try set ambox to account of mbox set nambox to name of ambox end try try set cmb to container of currentMailbox set ncmb to name of cmb end try try set cmbox to container of mbox set ncmbox to name of cmbox end try --set dt to "nmb:(" & nmb & ") nmbox:(" & theMboxName & ")" & return --set dt to dt & "namb:(" & namb & ") nambox:(" & nambox & ")" & return --set dt to dt & "ncmb:(" & ncmb & ") ncmbox:(" & ncmbox & ")" --display dialog dt if nmb is theMboxName and namb is nambox and ncmb is ncmb then set madeWindow to 2 exit repeat end if end repeat if madeWindow is not 0 then exit repeat end if end repeat if madeWindow = 0 then set theWindow to make new message viewer with properties {visible:true} -- can't set selected mailboxes here -- have to wait until the window is created. -- why can't the command remember when the time is right? tell theWindow set visible to true set ngmbox to "" set mydelay to 1 set mylast to 2 repeat with mycount from 0 to mylast --do shell script "sleep " & mydelay -- if you use shell script sleep you can never -- set the selected mailbox for the window delay mydelay try set selected mailboxes to {mbox} --set selected mailboxes to {move message of theRule} --on error errText --display dialog "error was: " & errText end try try set gmboxs to selected mailboxes set gmbox to mailbox 1 of gmboxs set ngmbox to name of gmbox end try if ngmbox is theMboxName then exit repeat else if mycount = mylast then --set dtt to "Can't set message viewer to mbox '" & theMboxName & "'" --display dialog dtt else set mydelay to mydelay + mydelay end if end repeat --3 times try set selected messages to {message 1 of theMessages} end try end tell --activate end if --if madeWindow = 0 then end if --if theText is not equal to "" then end tell end perform mail action with messages end using terms from