Error when exporting from Apple Photos with AppleScript: Could not write file to destination

903 views Asked by At

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: screenshot of 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.

1

There are 1 answers

0
Robert Kniazidis On

First, the Photos.app dictionary says to export using its own file specifier with the addition of a compound HFS path (2 parts). Moreover, the 1-st part of the HFS path must be a folder that already exists in the file system. An example of such a folder is the temporary items folder from user domain. It definitely exists on any Mac (unlike the tmp of home folder).

Here we come to a little mystery. The dictionary does not say anything about what should be specified as the 2nd part of the HFS path. File or folder? Applying Sherlock Holmes's method of deductive logical thinking, I conclude: The export Photos.app command is for exporting a list of media items (that is, several items). Since it will not be possible to cram several media items into a file, then the 2nd part of the HFS path must be a folder:

-- script: Export media item(s) to local Folder
-- written: by me, right now

-- 1st part of compound HFS path, should be already existing folder
set tmp to (path to temporary items folder from user domain) as text

tell application "Photos"
    activate
    set mediaItem to last media item
    -- we use here Photos.app's flie specifier with compound HFS path
    -- 2nd part of HFS path became container folder for exported media items
    export {mediaItem} to file (tmp & "ApplePhotosExport")
end tell

NOTE: Of course, the HFS path of a folder can also be non-compound if the folder already exists. For example, in the example above, we could only specify file tmp, and then the media items would be exported to the temporary items folder itself, and not to its ApplePhotosExport subfolder.