This is for those folk in google land querying for “applescript make new mailbox Imap”
Today I finished my first applescript. Using components from several other scripts I set out on the following task.
- For all messages in a set of selected Imap folder
- Find messages that originate from a mailinglist
- Apply heuristics to determine the domain of the mailinglist and the name of the mailinglist
- File the message in an IMAP folder with name “ArchiveRepository/domain.of.list/listname/<year>/<Quarter>
One of the subtasks is to build the mailboxes in IMAP with these directory structures. Taking an existing example (The MailArchiveByDate scropt from Dough Hellman) I first took a left to right approach. Writing out what happens internally in a test script would look like:
— Emulating what would happen in the code loop:
make at the end of the mailboxes of account (name of theAccount) new mailbox with properties {name:(“ArchiveRepository”)}
make at the end of the mailboxes of account (name of theAccount) new mailbox with properties {name:(“ArchiveRepository/domain.of.list”)}
make at the end of the mailboxes of account (name of theAccount) new mailbox with properties {name:(“ArchiveRepository/domain.of.list/listname”)}
— etc etc
In Doug’s code functionality is achieved by:
— Find the month archive mailbox. If this does not
— exist, we create it.
try
set monthMailbox to (mailbox archiveMonth of yearMailbox)
on error
log “Creating ” & archiveMonth & ” mailbox”
if imapAccountName is “” then
make new mailbox with properties {name:mailboxName}
else
make at the end of the mailboxes of account imapAccountName new mailbox with properties {name:mailboxName}
end if
set monthMailbox to (mailbox archiveMonth of yearMailbox)
end try
Unfortunately this does not seem to be portable to all IMAP servers. My email server generates an error when I try to CREATE “ArchiveRepository/domain.of.list” while “ArchiveRepository” already exists and is not a container of other mailboxes.
The obvious solution is create the mailbox using the full path, all the way to the last mailbox that will not be a container of mailboxes but of messages. An example is below:
try
set rootMailbox to mailbox ArchiveRoot of theAccount
on error
display dialog “Creating ” & mailboxName & ” mailbox in ” & name of theAccount with icon 1
make at the end of the mailboxes of account (name of theAccount) new mailbox with properties {name:(mailboxName)}
set rootMailbox to mailbox ArchiveRoot of theAccount
end try
set ListDomainBox to my ReturnChildMailbox(rootMailbox, TheDomain, theAccount, mailboxName)
set ListnameBox to my ReturnChildMailbox(ListDomainBox, TheListName, theAccount, mailboxName)
set YearBox to my ReturnChildMailbox(ListnameBox, archiveYear, theAccount, mailboxName)
set QuarterBox to my ReturnChildMailbox(YearBox, Quarter, theAccount, mailboxName)
log “Box set to ” & name of QuarterBox as text move theMessage to QuarterBox
Where ReturnChildMailbox does the appropriate creation
on ReturnChildMailbox(ParentMailbox, childname, theAccount, FullPath)
tell application “Mail”
try
set ChildMailbox to (mailbox childname of ParentMailbox)
on error
display dialog “Creating ” & childname & ” mailbox in ” & name of ParentMailbox & return & FullPath with icon note
make at the end of the mailboxes of account (name of theAccount) new mailbox with properties {name:(FullPath)}
set ChildMailbox to (mailbox childname of ParentMailbox)
end try
end tellreturn ChildMailbox
end ReturnChildMailbox
Maybe I’ll post the script at some point 😉
Hi, I would be interested in the script. Did it work? I want to make a Folder script that creates a IMAP Folder when in Finer a new Subfolder is created.