问题
I have an Applescript that is working on my computer, but not on my colleague's. I get two errors when manipulating paths: -10004
and -10000
. I have an idea on how to solve this, but first I'd like to understand those error codes.
Here is the script (I removed useless part, the full version is on 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
Here is the log:
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
Thanks!
I updated the script but I still get error -10000
. Here are the modified lines:
save theDocument in file exportFilename
and
-- 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
回答1:
Errors -10000 - -10015 are event registry errors.
Error -10000 is not a target error per se, because it will throw an -1708 in those cases mostly. most of the time it is not a target error but an incomplete command or wrong usage of brackets. What if you use:
save theDocument in file export_filename
Error -10004 is a privilege violation error, which mean you're doing something with the file that isn't allowed. Probably you're not allowed to remove the file and do shell script command should always be used outside tell application blocks. The problem is that the target application can run as another user than the script. I'm not saying it is the error but there is a chance that it this is the problem. Otherwise you simply heve not enough privileges and you need to ask the user for administrator privileges.
do shell script "do something" with administrator privileges.
回答2:
I haven't found where those error codes are documented, but they mainly deal with events that the targeted application isn't able to do. The first two errors -10004 are from using a Standard Additions command inside an application tell statement (offset and do shell script) - the application doesn't know what those commands are, passes the error up the chain to AppleScript, but AppleScript knows what they are and does it.
I don't have OmniGraffle, but the last error is telling you that the save command couldn't be performed, probably due to a problem with the destination not being a file specifier - it is just a text string, so you will probably have to coerce it into something that the command wants.
来源:https://stackoverflow.com/questions/9204320/what-are-error-10004-and-error-10000-in-applescript