Script to bring a mailbox to the front [newbie, non-geek]

Hi,

I managed to create a script a few weeks ago for deleting a message I’ve just sent if I click a certain button in a dialog. It works very well except for one thing. I use the script in conjunction with a set of rules in Hazel for periodically copying .emlx files from ~Library/Mail/myAccount/Sent Messages.mbox/Messages to a folder in ~/Documents. The purpose of the script is to trash sent messages I don’t want to keep so they don’t then get copied to the other folder.

The problem I’ve discovered is that, even after my script has run and the message has disappeared from the “Sent” mailbox (and now appears in the “Trash” mailbox), the .emlx file is still in the “…/Sent Messages.mbox/Messages” folder (with a duplicate in the “…/Deleted Messages/…” folder; the duplicate’s filename is one number after the original’s filename). If the original .emlx stays in the “Sent” box, then in due course the Hazel rule will run and copy this unwanted file to Documents.

After a bit of investigation I’ve discovered that the original .emlx file disappears from “Sent” the moment I select “Trash” in the left panel of the Mail.app viewer window. So I’m assuming what I need is an add-on to my existing script, that will do something under the hood equivalent to selecting the “Trash” box for the relevant account. I’m hoping this could be scripted such that Mail.app wouldn’t have to become the front application; but I guess I could live with the latter if necessary.

As always when I try to figure out an applescript I get to a point where no amount of googling and experimenting is going to produce what I want. That point has again been reached, so I’m turning to you guys for hope and consolation …

Here’s my current script (including the Hazel handlers - first & last lines):

on hazelProcessFile(infile)
	tell application "Mail"
		activate
		set theMessage to message 1 of mailbox "Sent Messages" of account "myAccount" of application "Mail"
		set theSubject to subject of theMessage
		display dialog theSubject & "...  Save copy?" buttons {"No", "Yes"} default button 1
		if (button returned of the result) = "No" then
			set mailbox of theMessage to mailbox "Deleted Messages" of account "myAccount" of application "Mail"
		end if
	end tell
end hazelProcessFile

TIA …

Model: macbook2.16
AppleScript: 2.2
Browser: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en; rv:1.8.1.12) Gecko/20080206 Camino/1.5.5
Operating System: Mac OS X (10.5)

Hi,

Try adding this to your script (at the end of the Mail tell block):

set selected mailboxes of front message viewer to {trash mailbox}

Best wishes

John Maisey
www.nhoj.co.uk

Thanks for that, Oliver :slight_smile: Certainly got me on the right track. I actually discovered that it needs first the “sent” box and then the “trash” box to be selected. That bit now works perfectly.

I did some more enquiring of my own re having Mail hide after script is finished, and have come up with something. Whole script now looks like this:

on hazelProcessFile(infile)
	tell application "Mail"
		activate
		set theMessage to message 1 of mailbox "Sent Messages" of account "Mail.com" of application "Mail"
		set theSubject to subject of theMessage
		display dialog theSubject & "...  Save copy?" buttons {"No", "Yes"} default button 1
		if (button returned of the result) = "No" then
			set mailbox of theMessage to mailbox "Deleted Messages" of account "Mail.com" of application "Mail"
			set selected mailboxes of front message viewer to {sent mailbox}
			set selected mailboxes of front message viewer to {trash mailbox}
			close front window
		end if
		if (button returned of the result) = "Yes" then
			close front window
		end if
	end tell
end hazelProcessFile

It actually seems to work; but oddly Hazel then returns an error message via Growl saying there was error running the script :confused:

Should that concern me? Any suggested improvements to the script, anyone? thanks.