Newbie Script Help - Apple Mail

Hi everyone,

I am currently writing my first piece of Apple Script - so please be patient.

I want a script which activates Mail and then displays (within the message viewer) the most recently delivered mail messages. I plan to use this with a quicksilver trigger.

So far I have written:

tell application “Mail” to activate
tell application “System Events”
click menu item “Message Viewer” of ((process “Mail”)'s (menu bar 1)'s ¬
(menu bar item “Window”)'s (menu “Window”))
click menu item “Drafts” of ((process “Mail”)'s (menu bar 1)'s ¬
(menu bar item “Mailbox”)'s (menu “Mailbox”)'s ¬
(menu item “Go To”)'s (menu “Go To”))
click menu item “Inbox” of ((process “Mail”)'s (menu bar 1)'s ¬
(menu bar item “Mailbox”)'s (menu “Mailbox”)'s ¬
(menu item “Go To”)'s (menu “Go To”))
end tell

Which does the trick in a clunky way. The reason I go to Draft before going to Inbox is that this was the only way I could find to clear the search bar, since if the search bar is filled the jump to Inbox often fails.

Another problem with the script is that it only really takes you to the Inbox, and the new messages will only be displayed if Inbox has the correct sort order.

I would like a more elegant solution. Any tips?

Very grateful for any advice you guys can give.

Cheers,

Dan

Model: MacBook
Browser: Firefox 3.0
Operating System: Mac OS X (10.5)

Hi Dan,

Most of the time GUI scripting (click menu item “Message Viewer” of….) in Applescript will not be the best way to do things.

You should get into the habit of reading the Applescript dictionaries of scriptable applications. In Script Editor go to the Window menu and select Library (for starters).

One way to do what you ask:

tell application "Mail"
	-- Bring the app to the front
	activate
	-- communicate with its main window
	tell front message viewer
	-- Select the inbox
		set selected mailboxes to {inbox}
		-- Sort by date
		set sort column to date received column
		-- With the dates descending (for latest messages at the top.)
		set sorted ascending to false
	end tell
end tell

Best wishes

John M
www.nhoj.co.uk

Thanks John. That’s a great tip! I was unaware of the word “dictionary” in this context - I have now been able to google lots of helpful stuff. Thanks a lot.