mailbox question

Don’t wanna cross post, but following no reply + thinking I may have posted in the wrong place, I’m posting this here:

I’ve been looking for a way to do this for years. Recently got a mac, and wondering if applescript is the answer!

I’ve seen smart mailboxs in mail, but they don’t appear to do exactly what I want…

I have my own domain, and for everything I sign up for, i give a different email address, so for this place, i used “applescript@example.com
For amazon: “amazon@example.com” etc. Now I can create a smart mailbox for each of these addresses but only if I manually create one for each address (from what I can tell…)

What I want, is for a smart mailbox to be created automagically for any email which comes to my domain, so then I’ll have boxes for everyone who emails me, depending on the address they use! A nice filing system.

I guess, if it makes it much easier, it would be fine to do this with actual folders instead of smart mailboxes…although it would be quite nice to still have all the mail stored centrally…

Is this possible? Is it stupidly simple or is it ridiculously complicated? I’ve tried searching to see if this has been done before, but I couldn’t see much

Thanks in advance!

Hi tanc,

it is not possible to create smart mailboxes with AppleScript.

This script creates a new mailbox (if doesn’t exist) with the name of the first part of the recipient
and moves each message to this mailbox. You can define a rule which executes the script.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set theMailboxes to name of mailboxes
		repeat with eachMessage in theMessages
			tell eachMessage to set theRecipient to address of to recipient 1
			set theRecipient to text 1 thru ((offset of "@" in theRecipient) - 1) of theRecipient
			if theRecipient is not in theMailboxes then make new mailbox with properties {name:theRecipient}
			move eachMessage to mailbox theRecipient
		end repeat
	end perform mail action with messages
end using terms from

Ok, this is what i put together…

tell application "Mail"
	
	repeat with eachMessage in (every message in mailbox "test")
		
		
		set blah to (address in recipient in eachMessage) as text
		log "items:"
		log (count of characters in blah)
		set i to 1
		set teststr to ""
		repeat with i from 1 to count of blah
			
			if item i of blah is equal to "@" then
				
				set teststr to item i of blah as text
				exit repeat
			else
				set i to i + 1
			end if
		end repeat
		set blah to characters 1 thru (i - 1) of blah as text
		log blah
		try
			set mbox to mailbox blah
			set mName to name of mbox
		on error
			if blah is not "" then
				
				make new mailbox with properties {name:blah}
				set mbox to mailbox named blah
			end if
		end try
		move eachMessage to mailbox blah
	end repeat
end tell

It almost works, but gives an error when moving the message, after having created the mailbox
Mail got an error: NSInternalScriptError

I looked at your script, but I didn’t really understand what it was doing, or what I was supposed to do with it!!

Thanks

My script does what you requested, but with “normal” mailboxes.
If you define a rule with condition To > ends with @example.com and perform Run AppleScript with this script attached
it moves every incoming message in a mailbox named with the part before @ (a new one will be created if doesn’t exist)

Ok, that works…

One further question: Is there anyway to specify where the mailbox is created?

I’m using imap, and it would be good if the folders were created there, rather than locally. Is that possibe?

Thanks for your help!

Yes, change the “make mailbox line” to this:


if theRecipient is not in theMailboxes then make new mailbox at end of imap account "myAccount" with properties {name:theRecipient}

It only works the first time. If I have two messages to blah@example.com, it’ll look for mailbox “blah”, not find it, create it, and move that message, but that’s it. Try and apply the rule to any other message with blah@ and it will just ignore it…

with my limited debugging, it is never getting to the moving the message part, if the mailbox already exists…

Of course, it can’t work, my fault, sorry
If the created mailboxes are all within this imap-account,
you also have to change the line, which gets the names of the mailboxes


set theMailboxes to name of mailboxes of imap account "myAccount"

cool

I had to also change the move message line to include the "of imap mailbox “myaccount” "but it now works great!

Thanks for that!

I can’t leave it alone…

is there anyway to use the “envelope-to” field (in full header) instead of the recipient? …just works better for mailing lists etc.

Thanks…

try this:


tell eachMessage to set theRecipient to content of header "envelope-to"

here is how I got it to work.


tell application "Mail"
	activate
	set theMessage to every message in inbox as list
	repeat with amessage in theMessage
		if amessage is not "" then
			
			set SenderName to extract name from sender of amessage
			set theDomain to extract address from sender of amessage
			set tid to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "@"
			set theDomain to text item 2 of theDomain
			set AppleScript's text item delimiters to "."
			set theDomain to text item 1 of theDomain
			set AppleScript's text item delimiters to tid
			
					
			if not (exists mailbox theDomain) then
				make new mailbox with properties {name:(theDomain)}
			end if
			
			if not (exists mailbox SenderName of mailbox theDomain) then
				
				make new mailbox with properties {name:(theDomain & "/" & SenderName)}
			end if
			move amessage to mailbox SenderName of mailbox theDomain
		end if
		
	end repeat
end tell

Hello StefanK, are you sure about this? :expressionless:

Please, don’t take me wrong as you appear very knowledgeable on these matters but I would love to create a few Smart boxes with AppleScript and I just started my quest for information when I came accross this post of yours.

The option of course would be creating a standard mailbox and “move” the messages I am interested in but that would be not as elegant.

Any other ideas?

Ciao

/Pieter

Hi Pieter,

Yes, I am.
A smart mailbox is a virtual folder which displays dynamically the search result of given search criteria.
It is also not possible to create a smart folder in Finder with AppleScript.

PS: Of course you can try to create a smart mailbox using GUI scripting,
but you cannot access it with applescript

Hello Stefan, thank you for quick reply.

I know what smart folders are as I use them all the time.
Being “virtual folders” is exactly what makes them so interesting to me.

I have been exploring my options with Automator but it seems like there even are less available there, which of course, was to be expected.

I wonder what the GUI scripting, you are referring to, is.

But, to have a better understanding of what I want to do, here is the problem:

I am running a small Mailing list for an Apple user group, (approx 25/30 people).

I noticed that unexperienced users have troubles in setting-up filtering rules that can highlight mail coming from this particular sender (the Mailing List in this case).
What makes it even more difficult for them is that the mailing list address is in the to: field instead of the from:

Setting up a Smart Box in Mail.app with the appropriate filtering rules is trivial and I was hoping to be able to script this and distribute the script among the users.

Any further idea?

Ciao

/Pieter

PS btw should we start a new topic on this issue?

This is GUI scripting, it creates a new smart mailbox simulating choosing menu items and pressing buttons.

activate application "Mail"
tell application "System Events"
	tell process "Mail"
		click menu item 14 of menu 1 of menu bar item 6 of menu bar 1
		repeat until exists checkbox 1 of window 1
			delay 0.5
		end repeat
		tell window 1
			set value of text field 1 to "MyNewSmartMailBox"
			tell pop up button 1
				click it
				delay 0.5
				click menu item 1 of menu 1
			end tell
			delay 0.5
			tell scroll area 1
				tell pop up button 1
					perform action "AXPress"
					delay 0.5
					click menu item 6 of menu 1
				end tell
			end tell
		end tell
	end tell
end tell

I don’t think so

Thanks a ton Stefan, it seems like you have done the hard work for me already… :slight_smile:

At a first run however, the script fails with the following message error:

Sorry for asking but, any clues as for the reasons?

Grazie

/P

Scelgi Preference di Sistema > Accesso Universale
e poi attiva Abilita l’acceso s dispositivi d’assistenza

This enables UI scriping capabilities

Thanks for the hint Stefan, I am impressed. :slight_smile:

Doing so however, still doesn’t work as expected.

The script starts apparently hanging for ever and no result is produced.
Does it perhaps need some input from my side?
and, btw what is this section doing?


tell pop up button 1
					perform action "AXPress"
					delay 0.5
					click menu item 6 of menu 1
				end tell

Sorry to ask once more but, how to further proceed from here? :stuck_out_tongue:
Is there perhaps a “trace” feature in AppleScript I can enable for debugging?

Thank you

/Pieter

GUI scripting is time-critical. You should play with the delays, especially just before you snippet above.
The snippet opens the pop up menu with the criteria and chooses menu item 6 of it.
There is no further input needed.
To specify the UI elements, you can use Apple’s UIElementInspector or the commercial application PreFab UI Browser
There is no trace mode in Script Editor. LateNight’s ScriptDebugger has one, but it doesn’t work reliably in UI scripts

PS: I tried the script with “sistema italiano”, it works on my machine