2016-12-20 115 views
3

我正在嘗試編寫一個腳本,它將.numbers文檔轉換爲.csv文檔。它需要從命令行執行,所以我可以在預先提交的git鉤子中使用它。AppleScript沒有保存文件的權限

我已經編寫了一個AppleScript來獲取數字文件的路徑並將其導出爲CSV,但實際的導出將不起作用,因爲「您沒有權限。(6)」。我認爲這與沙箱有關,但我不能使用AppleScript來彈出文件選擇器,因爲這需要完全自動化。

如何給我的AppleScript權限導出到此文件?

on run argv 
    set input_file to item 1 of argv 

    set output_file to input_file 
    --strip off the .numbers extention 
    set delims to AppleScript's text item delimiters 
    set AppleScript's text item delimiters to "." 
    if output_file contains "." then set output_file to (text items 1 thru -2 of output_file) as text 
    -- set the extension to be csv 
    set output_file to output_file & ".csv" 
    set output_file to POSIX file output_file 

    tell application "Numbers" 
     activate 
     tell (open input_file) 
      set activeDocument to it 
       with timeout of 3 seconds 
        export activeDocument as CSV to output_file 
       end timeout 
      close activeDocument saving yes 
     end tell 
    end tell 
end run 

完整的錯誤消息是 export_numbers_to_csv.scpt:604:676: execution error: Numbers got an error: The document 「DisplayPlusButtonTestScripts.numbers」 could not be exported as 「DisplayPlusButtonTestScripts」. You don’t have permission. (6)

我的調用是從工作目錄/Users/me/osascript export_numbers_to_csv.scpt /Users/me/Test\ Scripts/MyTests.numbers

我有權寫入我要求腳本寫入的目錄。我也嘗試導出到臨時目錄(通過path to temporary items from user domain),但得到了相同的錯誤信息。

+0

AppleScript類是什麼'input_file'?該錯誤可能與目標路徑的組成有關。 – vadian

+0

@vadian我認爲這只是一個字符串。它從命令行作爲帶斜槓的路徑字符串傳入。 – MattL

+0

我想知道爲什麼在錯誤消息中沒有提到目標的文件擴展名。無論如何嘗試'將output_file設置爲POSIX文件output_file作爲文本' – vadian

回答

2

如果一個.csv文件已存在於同一個目錄中,並且名稱與您試圖導出的名稱相同,則權限可能會變得混亂。如果您創建.csv文件或者至少在某個時候編輯/打開它,那麼您將有權導出到該名稱,但是如果它之前從未打開過寫入訪問權限(例如,如果您下載了它) ),那麼必要的權限將不存在。

要解決這個問題,你可以在之前以下行添加到您的腳本「告訴應用程序‘數字’」塊:

open for access file output_file 
close access file output_file 

這告訴腳本打開寫訪問該文件,因爲它只需要打開以獲得必要的權限,然後關閉它。