You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Mail.app - Applescript - Looking for help with my 1st Applescript

Okay… so I am taking the plunge. I've finally found something that is bugging me enough that I want to make a script to resolve it.

*Here's the situation:*

I use Smart Mailboxes a lot. It's my primary window into dealing with email.
I use MailTags (an addon for Mail) to apply keywords to messages, via Mail Act-On rules (an addon by the same company).

I use Smart Mailboxes to show me messages with various keywords etc.

What I have found is that Smart Mailboxes do NOT automatically update themselves. For instance, when a message in the Smart Mailbox is changed in such a way that the filter on that mailbox should not display it, the message will still sit there until I either leave the mailbox and return, or hit Rebuil in the Mailbox menu.

The same applies for when new mail comes into my Inbox. If there is new mail that qualifies for inclusion in the smart mailbox I am currently viewing, it will not appear until I do one of the above two things.

What I discovered was this script:


tell application "Mail" to activate

tell application "System Events"
click menu item "Rebuild" in menu ¬
"Mailbox" in menu bar 1 of process "Mail"
end tell

This is what got me thinking about scripting a solution.
The other option is to assign a keyboard shortcut to the Rebuild command. But I would like the Rebuild to take place automatically when I apply certain rules to messages. I want my Mail Act-on rule to trigger the a Rebuild. I figure I can do this by having it trigger the Rebuild script shown above.

*HERE IS MY QUESTION:*
Is there a way to have the script check what the currently active/selected Mailbox is? Or, put another way, can I get it to NOT run if certain mailboxes are selected?

Here's what I am thinking.

Under no circumstances do I want this script to be triggered if the current mailbox is NOT a smart mailbox, and IS any of my IMAP mailboxes… most especially the Inboxes. That would trigger a complete cleaning out and rebuilding of the entire IMAP inbox, which has many gigs of mail in it.

What I am trying to figure out, and would love some help with, is how I can make sure this script ONLY runs when the current mailbox is either ANY smart mailbox, or NOT an IMAP mailbox, or some other logic that keeps it from running in an IMAP mailbox.

Any tips are greatly appreciated.

With thanks,

Jonathan

Message was edited by: InspiredLife

Message was edited by: InspiredLife

Macbook Pro 17" i7, Mac OS X (10.6.6), 500GB 7200RPM HDD, Matt screen, 8GB RAM

Posted on Mar 8, 2011 11:41 PM

Reply
3 replies

Mar 9, 2011 12:45 AM in response to InspiredLife

Unfortunately, there's no direct way to do what you want. Nothing in Mail.app's dictionary gives a hoot about smart mailboxes

Ironically, the closest hack I could find relies on that fact.

Each message viewer window has a selected mailboxes property which returns a list of currently-selected mailboxes. However, this only works if regular mailboxes are selected - it actually throws an error if you have a smart mailbox selected. You can use that to your advantage:

tell application "Mail"
try
set currentMailboxes to (selected mailboxes of message viewer 1)
-- that's all we care about. If the above line works, we have a regular mailbox selected
on error
-- however, if we get here, something went wrong
-- I'll assume that's because we have a smart mailbox selected,
-- but be aware there could be other failure states..
end try
end tell

Mar 9, 2011 5:10 PM in response to Camelot

Thanks Camalot,
I appreciate your help with this. I am also enjoying learning more about AppleScript. It seems much easier that other scripting languages, if for no other reason that it uses basic english logic and words.

When I run your script with an inbox selected, I get the following Result:

tell application "Mail"
get selected mailboxes of message viewer 1
--> {mailbox "INBOX" of account "JE Gmail"}
end tell
Result:
{mailbox "INBOX" of account "JE Gmail" of application "Mail"}


When I run it with a smart mailbox selected the following is produced:
tell application "Mail"
get selected mailboxes of message viewer 1
--> {current application}
end tell
Result:
{application "Mail"}


So get the impression it's not throwing up an error. Certainly, either way it does not run what I put into the On Error section, which was simply
display dialog "Success"


Is this useful? Can I test if the output contains the word "INBOX" ?
I notice that if I have, for instance, a SENT folder select, it gives the following result:
Result:
{mailbox "INBOX/Sent Messages" of account "email@je.com" of application "Mail"}


Which again has the word INBOX in there. Drafts comes up a little different.
Result:
{mailbox "Drafts (email@je.com)" of application "Mail"}

So if there was a way to parse the result through a test against "INBOX" "DRAFT" etc then this might work. What do you think?

I have no idea how to go about that though, so your input would be very helpful.

Thanks,

Jonathan

Mar 25, 2011 7:49 AM in response to InspiredLife

Try this:

tell application "Mail"
launch
if frontmost is false then
activate
end if

-- make new message viewer
end tell

tell application "System Events"
tell process "Mail"
click menu item "Inbox" of menu 1 of menu item "Go To" of menu "Mailbox" of menu bar item "Mailbox" of menu bar 1
click menu item "Rebuild" in menu bar item "Mailbox" of menu bar 1
end tell
end tell

return input

Mail.app - Applescript - Looking for help with my 1st Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.