I have an AppleScript that exports images from Apple Photos. In some circumstances, it fails with error "Could not write file to destination (1,001)". I've been banging my head on this and could use help figuring out why it's happening.
Here's a simplified AppleScript that reproduces the problem:
tell application "Photos"
activate
set mediaItem to last media item
export {mediaItem} to "/Users/spather/tmp/ApplePhotosExport"
end tell
If I run this right after a fresh start of the Photos app, it will fail. If manually export (using the Apple Photos UI) a photo to the same folder the AppleScript exports to, thereafter, the script works every time. But if I restart the Photos app, the script will fail again until I do a manual export from the UI at least once.
When it fails, I see this error dialog:
This is what the permissions look like on the destination directory:
$ ls -ld@ /Users/spather/tmp/ApplePhotosExport
drwxr-xr-x 10 spather staff 320 Dec 10 15:56 /Users/spather/tmp/ApplePhotosExport
But I suspect it's not a permissions issue as the same directory will not exhibit the issue for a manual export or the script run after a manual export and will exhibit the issue again after a cold restart of the Photos app.
EDIT: Perhaps it is a permission thing after all. After I wrote the above I traced filesystem activity with Instruments and saw this:
Photos (54936) failed performing open_nocancel on ( /Users/spather/local-storage/tmp/ApplePhotosExport/IMG_4037 (4).jpg.sb-ec7cccde-OaN2DK ) with: Operation not permitted. 1
EDIT 2: In an attempt to rule out permissions, I tried fulling opening permissions on the directory. Sadly, it still fails in the cases it failed before.
$ chmod 777 ~/tmp/ApplePhotosExport
$ ls -ld@ ~/tmp/ApplePhotosExport
drwxrwxrwx 11 spather staff 352 Dec 10 16:40 /Users/spather/tmp/ApplePhotosExport
Thanks in advance for any pointers to a solution or avenues to investigate.