Mail and Applescript making new mailboxes under El Capitan failing

G'day


I'm trying to make nested Mail folders under El Capitan.


However, Applescript with Mail only allows ONE level of folder to be created. It refuses to make nested folders, UNLESS the parent folder is one created under Yosemite, and then Mail ONLY creates ONE level of nested folders.


I've lodged a bug report, and heard nothing so far.


Does anyone know of a workaround, please?


Regards


Santa




tell application "Mail"

activate

if not (exists mailbox "Bar") then make new mailbox with properties {name:("Bar" as rich text)} # Works

do shell script ("sleep 0.2")

set selected mailboxes of message viewer 1 to mailbox "Bar"

tell application "System Events" to tell process "Mail"

click menu bar item "Mailbox" of menu bar 1

do shell script ("sleep 0.1")

click menu item "Rebuild" of menu 1 of menu bar item "Mailbox" of menu bar 1

end tell

do shell script ("sleep 0.1") # Tried delays up to 4 seconds

quit

end tell

do shell script ("sleep 2") # Tried delays up to 4 seconds

tell application "Mail"

activate

#

# Try creating within pre-existing mailbox created with Yosemite ('Year 2015')-- WORKS at ONE level ONLY

#

if not (exists mailbox "Dandy" of mailbox "Year 2015") then

beep

set x to 0

repeat until exists mailbox "Dandy" of mailbox "Year 2015"

set x to x + 1

if x > 3 then exit repeat

try

make new mailbox with properties {name:("Year 2015/Dandy" as rich text)} #display dialog errmsg

end try

do shell script ("sleep 0.1")

end repeat

end if

#

# FAILS

#

if not (exists mailbox "Jim" of mailbox "Dandy" of mailbox "Year 2015") then

beep

try

make new mailbox with properties {name:"Year 2015/Dandy/Jim"}

on error errmsg

display dialog errmsg

end try

end if

end tell

tell application "Mail"

activate

if not (exists mailbox "Dandy" of mailbox "Bar") then

beep

set x to 0

repeat until exists mailbox "Dandy" of mailbox "Bar"

set x to x + 1

if x > 3 then exit repeat

try

make new mailbox with properties {name:("Bar/Dandy" as rich text)} # Errors with 'Mail got an error: AppleEvent handler failed.'

on error errmsg

#display dialog errmsg

end try

do shell script ("sleep 0.1")

end repeat

end if

do shell script ("sleep 0.2")

if not (exists mailbox "Jim" of mailbox "Dandy" of mailbox "Bar") then

beep

try

make new mailbox with properties {name:"Bar/Dandy/Jim"}

on error errmsg

display dialog errmsg

end try

end if

end tell

late 2014 27" i7 iMac-OTHER, OS X El Capitan (10.11.1), null

Posted on Nov 9, 2015 7:31 PM

Reply
2 replies

Nov 11, 2015 1:47 AM in response to Brian Christmas

Hi,


On mailbox created with Yosemite :

To create nested mailboxes, the script doesn't need to verify the existence of an mailbox, Mail will create missing mailboxes, use this script

tell application "Mail"
     make new mailbox with properties {name:"Year 2015/Dandy/Jim/"} -- no error, even if all mailbox exists, or no mailbox or some mailbox exists
     make new mailbox with properties {name:"Bar/Dandy/Jim/"}
     -- example to create a master mailbox without nested folder, but a nested mailbox will be added later. -->  make new mailbox with properties {name:"Some name/"}
end



But this script doesn't work on mailbox created on El Capitan, but works when the master mailbox is not created.

Notice the slash character at the end of the last name, this is necessary if you want to add another mailbox at the last level (when you run another script or the same script), Mail skip this character in the name.

  • Examples:
  • tell application "Mail"
         make new mailbox with properties {name:"test/test1/test2"} -- no error, even if all mailbox exists, or no mailbox or some mailbox exists
         make new mailbox with properties {name:"test/test1/test2/test3/"} -- doesn't work, error because no slash character at the end of the "test2" in the previous line
    end


tell application "Mail"
     make new mailbox with properties {name:"wwww/test1/test2/"}
     make new mailbox with properties {name:"wwww/test1/test2/test3/"} --  work
end



---

On mailbox created with El Capitan, solution :

You must create manually a temporary mailbox with a slash character at the end of the name, move it to the last level of a master mailbox, wait one second, and moves that mailbox out of the master mailbox.

Do this for all the necessary mailboxes (mailboxes that will be modified by a script)


After that, you can run my first script without error.

Nov 11, 2015 1:50 AM in response to Jacques Rioux

Thanks Jacques, works a treat.


tell application "Mail"

activate

#

# 'Year 2015' is pre-existing Yosemite mailbox

#

try

make new mailbox with properties {name:("Year 2015/11/2015 11 11/") as rich text}

on error errmsg

display dialog errmsg

end try

#

# 'Year 2016' is new El Capitan mailbox

#

try

make new mailbox with properties {name:("Year 2016/11/2016 11 11/") as rich text}

on error errmsg

display dialog errmsg

end try

end tell


I've made them 'as rich text' even though it does not seem to be necessary any more.


Regards


Santa

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Mail and Applescript making new mailboxes under El Capitan failing

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