2017-07-25 38 views
0

我正尝试在Apple Mail中使用JavaScript创建子邮箱。JXA:在Apple Mail中创建邮箱

我有下面的代码片段(parent是一个参考,我希望新邮箱的邮箱):

var mb = mail.Mailbox({name: "SubFolder"}); 
parent.mailboxes.push(mb); 

事件日志显示:

app = Application("Mail") 
app.mailboxes.byName("Local").mailboxes.byName("Archive").mailboxes.push(app.Mailbox({"name":"SubFolder"})) 

    --> Error -10000: AppleEvent handler failed. 

我在做什么错误?

谢谢, 克雷格。现在

代码:

var mb = mail.Mailbox({name: "Local/Archive/Test Archive/SubFolder"}) 
logger.logDebug("mb = '" + Automation.getDisplayString(mb) + "'."); 
mail.mailboxes.push(mb)      // create the subfolder 

这个工作,只要有路径中没有空格。 我试图强制使用\\在它前面的空间,但是然后你得到“测试\存档”作为名称。

那么,如何让名称中的空间起作用?

谢谢。

回答

1

要创建子文件夹,您需要一个名称,如posix路径 - >"/theMasterMailbox/subMailBox1/subMailBox2/subMailBox3"


所以,你需要:

  • 一个循环,把每个父文件夹的名称到一个数组。
  • 使用join('/')将数组的元素连接到字符串中。
  • 使用mail.mailboxes.push(mb)而不是parent.mailboxes.push(mb)

这里是它创建了一个选择的文件夹(邮箱)在名为 “” 邮箱的示例脚本:

mail = Application('com.apple.Mail') 
parent = mail.messageViewers()[0].selectedMailboxes()[0] 

mboxNames = [parent.name()] 
thisFolder = parent 
try { 
    while (true) { // loop while exists the parent folder 
     mboxNames.unshift(thisFolder.container().name()) // add the name of the parent folder to the beginning of an array 
     thisFolder = thisFolder.container() // get the parent of thisFolder 
    } 
} catch (e) {} // do nothing on error, because thisFolder is the top folder 

mboxNames.push("SubFolder") // append the name of the new subFolder to the array 

mBoxPath = mboxNames.join('/') // get a string (the names separated by "/") 
mb = mail.Mailbox({name:mBoxPath}) 
mail.mailboxes.push(mb) // create the subfolder 
+0

感谢。我刚刚看到这个,因为我在发布时未收到通知。我会在沙盒中试试这个,看看我能否为本地盒子工作。 – Crashmeister

+0

请参阅我上面的文章的扩展。如果其中一个邮箱名称中有空格,我无法使其工作。 – Crashmeister

+0

您的代码可以在我的电脑上正常工作('macOS Sierra',**版本10.12.6 **)。 对不起,我帮不了你。 – jackjr300

相关问题