2017-07-25 76 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