I am getting the following error when attempting to access several files in an AppleScript. However, I can open these files manually without issue.
The document [filename] could not be opened. You don't have permission.
I have attempted the following:
- Manually modified permission via File → Get Info
- Used Disk Utility to "Verify" and "Repair Permissions"
- Rebooted in recovery mode to reset home directory permissions and acls
I continue to have the problem.
To add further frustration, the files do not consistently report the error. Sometimes I will get the error on a file when I run the script, but not the next time!
Why might I be receiving this permissions error, and how else might I address it?
AppleScript below, if it helps:
-- prompt for source directory
set srcDirectory to (choose folder)
-- get list of all files in source directory
set allFiles to (list folder srcDirectory without invisibles)
tell application "OmniGraffle"
-- create a new document
set newDocument to (make new document with properties {template:"Single Pixel Grid"})
-- added for debug purposes
delay 5
-- get a reference to the first layer
set destinationLayer to first layer in first canvas of newDocument
-- step through each of the file
repeat with currentFile in allFiles
-- get a reference to the next file
set srcFileString to (srcDirectory as string) & currentFile
set srcFileRef to (open srcFileString)
-- get a reference to the icon
set srcGraphic to first graphic in first layer in first canvas of srcFileRef
-- flip the icon (they're all upside down)
flip srcGraphic over vertically
-- copy the updated source to destination canvas
duplicate srcGraphic to destinationLayer
-- close the source file
close srcFileRef saving no
-- added for debug purposes
delay 5
end repeat
end tell
Best Answer
This problem is caused by Apple's sandboxing of applications (in this case, OmniGraffle). Preview does not receive an entitlement to access the PDF if you just pass it a string to open. From Apple's release notes:
Why does this sometimes work? When you open a file manually, the application retains the entitlement to open it (in Recents, for instance). Therefore, if you try any file you've opened already, odds are it will work, but it won't work on new files.
To fix, change your code to read: