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
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
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!!
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)
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"
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
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.
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
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.
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
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?
Is there perhaps a “trace” feature in AppleScript I can enable for debugging?
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