2012-02-09 26 views
2

我有工作在我的電腦上,但不能在我同事的一個AppleScript。 -10004-10000:操縱路徑時,我得到兩個錯誤。我有一個idea關於如何解決這個問題,但首先我想了解這些錯誤代碼。什麼是錯誤-10004和-10000錯誤AppleScript中

這裏是腳本(我刪除無用的部分,完整版是github):

-- export all layers to image files 

-- Settings 
property exportFileExtension : "png" 
property ADD_CANVAS_NUMBER : true 
-- End of Settings 

on file_exists(FileOrFolderToCheckString) 
    try 
     alias FileOrFolderToCheckString 
     return true 
    on error 
     return false 
    end try 
end file_exists 

tell application "OmniGraffle Professional 5" 
    set theWindow to front window 
    set theDocument to document of theWindow 
    set theFilename to name of theDocument 
    -- remove .graffle 
    -- FIRST ERROR IS HERE -10004 
    set theFilename to text 1 thru ((offset of "." in theFilename) - 1) of theFilename 

    set export_folder to (choose folder with prompt "Pick the destination folder") as string 
    set export_folder to export_folder & theFilename & ":" 

    -- create folder 
    if file_exists(export_folder) of me then 
     try 
      display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1 
     on error errText number errNum 
      if (errNum is equal to -128) then 
       return 
      end if 
     end try 

     -- deletes the folder (necessary because some layers may have been renamed 
     do shell script "rm -rf " & quoted form of POSIX path of export_folder 

    else 
     -- creates the folder 
     do shell script "mkdir -p " & quoted form of POSIX path of export_folder 
    end if 

    set canvasCount to count of canvases of theDocument 

    set i to 0 
    repeat with canvasNumber from 1 to canvasCount 
     set theCanvas to canvas canvasNumber of theDocument 
     set canvas_name to name of theCanvas 
     set canvas of theWindow to theCanvas 
     set layerCount to count of layers of theCanvas 

     -- ... 

     set area type of current export settings to current canvas 
     set draws background of current export settings to false 
     set include border of current export settings to false 

     set canvas_filename to "" 
     -- ... 
     set canvas_filename to canvas_filename & canvas_name 

     repeat with layerNumber from 1 to layerCount 
      set theLayer to layer layerNumber of theCanvas 

      if (theLayer is prints) and (class of theLayer is not shared layer) then 
       set layer_name to name of theLayer as string 
       set filename to canvas_filename & " - " & layer_name & "." & exportFileExtension 
       set export_filename to export_folder & filename 

       -- show the layer, export, then hide the layer 
       if character 1 of layer_name is not "*" then 
        set visible of theLayer to true 
        -- SECOND ERROR IS HERE -1000 
        save theDocument in export_filename 
        set visible of theLayer to false 
       end if 

      end if 

     end repeat 

    end repeat 
end tell 

這裏是日誌:

tell application "OmniGraffle Professional 5" 
    get window 1 
     --> window id 5032 
    get document of window id 5032 
     --> document "MSD.graffle" 
    get name of document "MSD.graffle" 
     --> "MSD.graffle" 
    offset of "." in "MSD.graffle" 
     --> error number -10004 
end tell 
tell current application 
    offset of "." in "MSD.graffle" 
     --> 4 
end tell 
tell application "OmniGraffle Professional 5" 
    choose folder with prompt "Pick the destination folder" 
     --> alias "Macintosh HD:Users:Romain:Desktop:Temp:" 
    display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1 
     --> {button returned:"Erase"} 
    do shell script "rm -rf '/Users/Romain/Desktop/Temp/MSD/'" 
     --> error number -10004 
end tell 
tell current application 
    do shell script "rm -rf '/Users/Romain/Desktop/Temp/MSD/'" 
     --> "" 
end tell 
tell application "OmniGraffle Professional 5" 
... 
... 
    save document "MSD.graffle" in "Macintosh HD:Users:Romain:Desktop:Temp:MSD:1- Navigation - 1Layout.png" 
     --> error number -10000 
Result: 
error "OmniGraffle Professional 5 got an error: AppleEvent handler failed." number -10000 

謝謝!


我更新了腳本,但仍收到錯誤-10000。以下是經修改的線:

save theDocument in file exportFilename 

-- Create folder if does not exist, remove it otherwise 
-- Shell script should not be executed inside tell application block 
if file_exists(export_folder) of me then 
    try 
     display alert "The file already exists. Do you want to replace it?" buttons {"Cancel", "Erase"} cancel button 1 
    on error errText number errNum 
     if (errNum is equal to -128) then 
      return 
     end if 
    end try 

    tell me 
     -- Delete the folder 
     do shell script "rm -rf " & quoted form of POSIX path of export_folder 
    end tell 

else 
    tell me 
     -- Create the folder 
     do shell script "mkdir -p " & quoted form of POSIX path of export_folder 
    end tell 
end if 

回答

4

錯誤-10000 - -10015是事件註冊表錯誤。

錯誤-10000本身並不是目標錯誤,因爲它會拋出在這些案件中的主要-1708。大多數情況下,這不是目標錯誤,而是一個不完整的命令或括號的錯誤用法。如果你使用:

save theDocument in file export_filename 

錯誤-10004是一個特權違規錯誤,這意味着你正在做一些不允許的文件。可能您不允許刪除該文件,並且應該始終在告訴應用程序塊之外使用shell腳本命令。問題在於目標應用程序可以作爲另一個用戶而不是腳本運行。我不是說這是錯誤,但有可能是這是問題。否則,你只是沒有足夠的權限,你需要詢問用戶的管理員權限。

do shell script "do something" with administrator privileges. 
+0

感謝您的回答!我用我使用的新腳本更新了我的問題,它給了我相同的錯誤(-10000)。 – charlax 2012-02-09 21:56:15

2

我還沒有發現其中的錯誤代碼記錄,但他們主要對付的目標應用是無法做到的事件。前兩個錯誤-10004是在應用程序tell語句中使用標準增加命令(偏移量執行shell腳本) - 應用程序不知道這些命令是什麼,將錯誤傳遞給鏈AppleScript,但AppleScript知道他們是什麼,並做到這一點。

我沒有OmniGraffle,但最後一個錯誤是告訴你保存命令無法執行,可能是由於目標不是文件說明符的問題 - 它只是一個文本字符串,所以你可能不得不強迫它成爲命令所需的東西。

+0

謝謝!我強制作爲一個文件,但它不起作用。它在我的電腦上工作。 – charlax 2012-02-09 21:56:50

+0

它可能需要一個別名或POSIX路徑 - 您需要檢查腳本字典以查看保存命令的含義。 – 2012-02-09 22:11:14

+0

即使腳本字典告訴我它需要別名(它來自標準套件),我也不能將它作爲別名強制使用,因爲文件不存在。 – charlax 2012-02-11 01:15:19