applescript - JXA: Create a mailbox in Apple Mail -
i trying create sub-mailbox in apple mail using javascript.
i have following code snippet (parent reference mailbox in want new mailbox):
var mb = mail.mailbox({name: "subfolder"}); parent.mailboxes.push(mb);
the events log shows:
app = application("mail") app.mailboxes.byname("local").mailboxes.byname("archive").mailboxes.push(app.mailbox({"name":"subfolder"})) --> error -10000: appleevent handler failed.
what doing wrong?
thanks, craig.
code now:
var mb = mail.mailbox({name: "local/archive/test archive/subfolder"}) logger.logdebug("mb = '" + automation.getdisplaystring(mb) + "'."); mail.mailboxes.push(mb) // create subfolder
this works long there no spaces in path. tried force space using \\ in front of it, "test\ archive" name.
so how space in name work?
thanks.
to create sub-folder, need name posix path --> "/themastermailbox/submailbox1/submailbox2/submailbox3"
.
so, need:
- a loop put name of each parent folder array.
- use
join('/')
join elements of array string. - use
mail.mailboxes.push(mb)
instead ofparent.mailboxes.push(mb)
here's sample script creates mailbox named "subfolder" in selected folder (the mailbox):
mail = application('com.apple.mail') parent = mail.messageviewers()[0].selectedmailboxes()[0] mboxnames = [parent.name()] thisfolder = parent try { while (true) { // loop while exists parent folder mboxnames.unshift(thisfolder.container().name()) // add name of parent folder beginning of array thisfolder = thisfolder.container() // parent of thisfolder } } catch (e) {} // nothing on error, because thisfolder top folder mboxnames.push("subfolder") // append name of new subfolder array mboxpath = mboxnames.join('/') // string (the names separated "/") mb = mail.mailbox({name:mboxpath}) mail.mailboxes.push(mb) // create subfolder
Comments
Post a Comment