3

私のコンピュータでは動作する Applescript がありますが、同僚のコンピュータでは動作しません。パスを操作すると、 と の 2 つのエラーが発生し-10004ます-10000。これを解決する方法については考えがありますが、まずこれらのエラー コードを理解したいと思います。

これがスクリプトです(役に立たない部分を削除しました。完全版は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

2 に答える 2

5

エラー -10000 ~ -10015 は、イベント レジストリ エラーです。

エラー -10000 は、ほとんどの場合に -1708 をスローするため、それ自体はターゲット エラーではありません。ほとんどの場合、それはターゲット エラーではなく、不完全なコマンドまたは間違ったブラケットの使用です。使用するとどうなりますか:

save theDocument in file export_filename

エラー -10004 は権限違反エラーです。これは、ファイルに対して許可されていない操作を行っていることを意味します。おそらく、ファイルを削除することは許可されておらず、do shell script コマンドは常に tell アプリケーション ブロックの外で使用する必要があります。問題は、ターゲット アプリケーションがスクリプトとは別のユーザーとして実行できることです。エラーとは言いませんが、これが問題である可能性があります。そうしないと、十分な権限が得られず、ユーザーに管理者権限を要求する必要があります。

do shell script "do something" with administrator privileges.
于 2012-02-09T09:01:31.997 に答える
3

これらのエラー コードが文書化されている場所は見つかりませんでしたが、主に、対象のアプリケーションが実行できないイベントを処理します。最初の 2 つのエラー -10004 は、アプリケーションの tell ステートメント ( offsetおよびdo shell script ) 内でStandard Additionsコマンドを使用したことによるものです。彼らはそうです、そしてそれをします。

私は OmniGraffle を持っていませんが、最後のエラーは、保存コマンドを実行できなかったことを示しています。これは、保存先がファイル指定子ではないという問題が原因である可能性があります。これは単なるテキスト文字列であるため、おそらくコマンドが必要とするものに強制します。

于 2012-02-09T02:40:03.673 に答える