2012-09-29 49 views
0

該腳本一直在使用10.7及更高版本,但在10.8中,似乎已損壞。該行:AppleScript不能在Mountain Lion中工作的路徑

set theFilePath to ((path to application support from user domain) as rich text) & "AppFolderName:" & UniqueName as string 
set theFileReference to open for access theFilePath with write permission 

在以前的版本上工作得很好,但蘋果顯然阻止它在Mountain Lion上正常工作。有沒有其他方式通過Mountain Lion中的Apple腳本訪問該文件夾?

編輯:我已經包括了整個腳本代碼將,郵件規則內的整個信息導出到我的程序可以導入的文本文件。將文本文件發送到〜/ Library/Application Support/MyProgram/MailImport/

請確保您的機器上已存在該目錄,就像它在我的這裏一樣,並且Apple腳本不會對它進行任何檢查。

path to application support位於代碼中,但將其更改爲path to desktop時,此腳本不起作用,表示存在寫入應用程序支持文件夾的問題,但代碼有效。

要測試,您可以在Mail中創建新規則,並讓Every Message運行該腳本。您必須將腳本放入〜/ Library/Application Scripts/com.apple.mail/

然後它將作爲規則窗口中的一個選項出現。您可以右鍵單擊郵件並選擇應用規則以在單個郵件上測試腳本。

using terms from application "Mail" 
    on perform mail action with messages theMessages for rule theRule 
     tell application "Mail" 
      repeat with eachMessage in theMessages 
       set sub to subject of eachMessage 
       set mid to message id of eachMessage 
       set sen to sender of eachMessage 
       set recp to "" 
       repeat with thisRecpt in recipients of eachMessage 
        set recp to recp & address of thisRecpt & "," 
       end repeat 
       set {year:y, month:m, day:d, hours:hh, minutes:mm} to (date sent of eachMessage) 
       set dat to (y * 10000 + m * 100 + d) as string 
       set tim to (hh * 100 + mm) as string 
       set con to content of eachMessage 

       set TotalString to "<!STDMessageSubject>" & sub & "<!STDMessageSubject>" & "<!STDMessageID>" & mid & "<!STDMessageID>" & "<!STDMessageSender>" & sen & "<!STDMessageSender>" & "<!STDMessageRecipient>" & recp & "<!STDMessageRecipient>" & "<!STDMessageDate>" & dat & "<!STDMessageDate>" & "<!STDMessageTime>" & tim & "<!STDMessageTime>" & "<!STDMessageContent>" & con & "<!STDMessageContent>" 

       set UniqueName to do shell script "uuidgen" 

       set theFilePath to ((path to application support from user domain) as rich text) & "MyApplication:MailImport:" & UniqueName as string 
       set theFileReference to open for access theFilePath with write permission 


       write TotalString to theFileReference 
       close access theFileReference 
      end repeat 
     end tell 
    end perform mail action with messages 
end using terms from 
+0

這很可能與授權/沙盒的樂趣有關,但希望有一些解決方法可以與之前的系統具有相同的功能。 – mjdth

回答

0

所以事實證明這是一個沙盒問題。蘋果郵件在10.8使用一般的沙盒應用程序支持文件夾的位置,無論你怎麼努力只得到~/Library/Application Support/,所以從內部郵件一個AppleScript 10.8

path to application support from user domain 

返回路徑

~/Library/Containers/com.apple.mail/Data/Library/Application Support/ 

從那裏可以創建和訪問MyApplication:MailImport:文件夾。由於我們正在嘗試讀取輸出的實際程序不是沙箱,所以我們現在可以從該位置讀取和訪問數據,因爲它似乎工作正常。

1

有沒有這樣的事情在蘋果作爲「富文本」。它應該只是作爲「文本」。另外,FilePath是一個字符串,所以在下一行中,您需要像這樣引用它...打開以訪問文件theFilePath。注意單詞「文件」。你需要那個單詞來將字符串轉換成文件引用,這就是該命令所要求的。

編輯:現在,我看到你的整個代碼我會寫這樣的。您的問題仍然可能是沙盒問題,但至少應該消除腳本中可能出現的編碼錯誤的任何來源。這會給你一個成功的腳本的最佳機會。如果它仍然不起作用,那麼它可能是一個沙盒問題。

我看到的基本編碼問題是您告訴Mail執行所有命令。郵件不知道諸如「應用程序支持路徑」,「執行shell腳本」或如何寫入文件等命令。他們是applescript命令,所以你不應該讓Mail去執行它們。它們不在Mail的applescript字典中,因此當Mail嘗試執行它們時可能會感到困惑。正如你所說,這當然是「文本」不斷變爲「富文本」的原因。

所以試試這個吧。如果你仍然有問題,那麼至少你知道你已經盡全力去消除代碼中的錯誤來源。

using terms from application "Mail" 
    on perform mail action with messages theMessages for rule theRule 
     tell application "Mail" 
      repeat with eachMessage in theMessages 
       set sub to subject of eachMessage 
       set mid to message id of eachMessage 
       set sen to sender of eachMessage 
       set recp to "" 
       repeat with thisRecpt in recipients of eachMessage 
        set recp to recp & address of thisRecpt & "," 
       end repeat 
       set {year:y, month:m, day:d, hours:hh, minutes:mm} to (date sent of eachMessage) 
       set dat to (y * 10000 + m * 100 + d) as string 
       set tim to (hh * 100 + mm) as string 
       set con to content of eachMessage 

       set TotalString to "<!STDMessageSubject>" & sub & "<!STDMessageSubject>" & "<!STDMessageID>" & mid & "<!STDMessageID>" & "<!STDMessageSender>" & sen & "<!STDMessageSender>" & "<!STDMessageRecipient>" & recp & "<!STDMessageRecipient>" & "<!STDMessageDate>" & dat & "<!STDMessageDate>" & "<!STDMessageTime>" & tim & "<!STDMessageTime>" & "<!STDMessageContent>" & con & "<!STDMessageContent>" 
       my writeToFile(TotalString) 
      end repeat 
     end tell 
    end perform mail action with messages 
end using terms from 

on writeToFile(TotalString) 
    set UniqueName to do shell script "uuidgen" 
    set theFilePath to ((path to application support from user domain) as text) & "MyApplication:MailImport:" & UniqueName 
    set theFileReference to open for access file theFilePath with write permission 
    write TotalString to theFileReference 
    close access theFileReference 
end writeToFile 

EDIT2:嘗試在地方一個在上面的代碼中的這個處理程序。這可能是使writeToFile處理程序工作的一種方法,因爲寫入部分將發生在與applescript不同的進程中。值得一試!

on writeToFile(TotalString) 
    set UniqueName to do shell script "uuidgen" 
    set theFilePath to ((path to application support from user domain) as text) & "MyApplication:MailImport:" & UniqueName 
    set theResult to do shell script "echo " & quoted form of TotalString & " > " & quoted form of POSIX path of theFilePath 
end writeToFile 

EDIT3:如果EDIT2不工作,然後看看here。看起來其他人在郵件寫入某些位置時遇到了問題,並通過向Mail添加密鑰來解決此問題,以授予其權限。

+0

當在腳本編輯器中保存時,它會自動用'用戶域規則類型富文本'替換'用戶域作爲文本'提供應用程序支持的路徑' – mjdth

+0

另外,作爲單獨的註釋,更改'應用程序支持的路徑'從原始代碼到'桌面路徑'的路徑導致腳本執行正常,所以它必須是權限問題而不是語法問題。 – mjdth

+0

第三條評論:我已經包含了整個腳本。當簡單地將「路徑到應用程序支持」替換爲「桌面路徑」時,此腳本編譯和運行正常 – mjdth