0

I'm stuck, since I found no way to delete a subfolder in Apple's Photos. Neiter via Applescript nor with JXA.

The deletition of folders on top level is no problem, but trying to delete subfolders (folders within folders) always results in an error, that the object cannot be found.

I spend hours on this, now.

None of these JXA commands work:

Application("Photos").delete (sfolder);
sfolder.delete ();
sfolder.parent.delete (sfolder);
sfolder.parent.containers.byName (sfolder.name ()).delete ();

Can anybody out there please help me and tell me the correct command to delete a SUBfolder in Photos via Applescript or JXA?

Thank you

This is some of all the code I tried:

tell application "Photos"

    -- In Photos is a folder "a" only containing an empty folder "b"

    -- -- -- -- -- -- -- -- -- -- -- -- --
    -- The subfolder "b" should be deleted
    -- -- -- -- -- -- -- -- -- -- -- -- --
        
    -- Photos
    -- -- My Albums
    -- -- -- a (as a folder)
    -- -- -- -- b (as an empty folder)

    set f1 to folder "a"

    set f2 to f1's folder "b"

    -- all of the following won't work

    -- delete f2 won't work
    -- delete folder f2 won't work
    -- delete folder id f2 won't work
    -- delete f1's folder f2
    -- delete f2 of f1
    -- delete f2 in f1
    -- delete container f2

end tell

1 Answers1

0

Under Monterey, at least, deletion of subfolders in Photos is not possible via AppleScript/JXA. There appears to be a bug in delete that fails for subfolders, but not for top-level folders and not for any-level albums.

tell application "Photos"
    --Subfolder deletion fails
    set folderToDelete to folder "Testing Folder Deletion" of folder "Posting Possibilities" of folder "Workshop"
    delete folderToDelete
end tell

We can verify both that folderToDelete contains a folder, and that this syntax for deletion is correct.

tell application "Photos"
    --It is getting the subfolder; we can test by getting the folder's name, parent, or id
    set folderToDelete to folder "Testing Folder Deletion" of folder "Posting Possibilities" of folder "Workshop"
    get id of folderToDelete
end tell

This returns the id of that folder; you can also try id of parent of folderToDelete or even (in this example, since "Testing Folder Deletion" is at the third level) id of parent of parent of folderToDelete. Clearly, folderToDelete is an actual item.

tell application "Photos"
    --Top-level folder deletions work
    set folderToDelete to folder "Top-Level Folder"
    delete folderToDelete
        
    --Album deletions work regardless of location
    set albumToDelete to album "Testing Album Deletion" of folder "Posting Possibilities" of folder "Workshop"
    delete albumToDelete
end tell

This will delete the top-level folder whose name is “Top-Level Folder”. It will also delete the sub-sub-album “Testing Album Deletion”. Clearly, the syntax is correct both for deleting folders and for deleting albums, including sub-albums. It would be exceedingly strange (though not out of the realm of possibility) for the syntax to change only for subfolders.

The same is true for JXA.

photos = Application("Photos")
folderToDelete = photos.folders.whose({name: "Top-Level Folder"})
folderToDelete = folderToDelete()[0]
photos.delete(folderToDelete)

This will delete the top-level folder named “Top-Level Folder”.

I did the previous testing under macOS Monterey. While testing to see if this behavior remains the same under Ventura, I noticed a possibly related behavior. While you can get id of folderToDelete as noted above, you cannot get every folder whose id is… or get every folder whose name is…. It will only return top-level folders that match, even on an id search.

tell application "Photos"
    set folderToDelete to folder "Testing Folder Deletion" of folder "Posting Possibilities" of folder "Workshop"
    set folderId to the id of folderToDelete
    get the name of every folder whose id is folderId
end tell

This will return the empty string. But:

tell application "Photos"
    set folderToDelete to folder "Workshop"
    set folderId to the id of folderToDelete
    get the name of every folder whose id is folderId
end tell

This will return a list of folder names, which is of course a list of one since ids are unique. The same occurs with names.

tell application "Photos"
    get the id of every folder whose name is "Workshop"
end tell

"Workshop" is a top-level folder, and so this returns a list of ids. But whose name is "Testing Folder Deletion" will return an empty list.

I suspect that this behavior is related to AppleScript’s inability to reference folders by id.

There’s another bug, too. Noticing that the error is about “Can’t make folder id "xxx" of folder id "yyy" into type integer”, I tried deleting the folder by number. For example, get name of folder 5 of folder "Workshop" gave me the correct name of the example folder I’ve been trying to delete in my latest tests. But:

tell application "Photos"
    --deletion by index DELETES THE WRONG FOLDER
    get name of folder 5 of folder "Workshop"
    delete folder 5 of folder "Workshop"
end tell

This deletes the fifth top-level folder rather than either erroring out or deleting the fifth subfolder of “Workshop”. Fortunately, Edit:Undo Delete Folder successfully restores it.

Note that there is an interesting twist in JXA in which the result (using the syntax I’ve used) is always a list. This is probably because this is the equivalent of AppleScript’s get folders of folders of folders whose name is "Testing Folder Deletion". (Sadly, get folder of folders of folders whose name is "Testing Folder Deletion", while not a syntax error, returns a list of empty lists. It not only doesn’t return the requested folder, it also continues to return a list.)

This is more obvious when getting subfolders or subalbums. A subscript is required for each level down from the application.

folderToDelete = photos.folders.whose({name: "Workshop"}).folders.whose({name: "Posting Possibilities"}).folders.whose({name: "Testing Folder Deletion"})
folderToDelete = folderToDelete()[0][0][0]
photos.delete(folderToDelete)

Notice that three subscripts are required to get the actual folder, because this folder is at the third level (second sublevel). This will fail, just as it does in AppleScript. You can test that it really does have the folder in a manner similar to the test I used in AppleScript, by getting the folder’s properties, or the parent folder’s properties:

folderToDelete.id()
folderToDelete.parent.id()

Similarly, deleting sub-sub-albums does work in JXA:

albumToDelete = photos.folders.whose({name: "Workshop"}).folders.whose({name: "Posting Possibilities"}).albums.whose({name: "Testing Album Deletion"})
albumToDelete = albumToDelete()[0][0][0]
photos.delete(albumToDelete)

This uses the same syntax as the syntax that fails to delete a sub-subfolder but it successfully deletes an album at the same sub-level and with the same parent.

You may wish to specify your macOS version in the question. There is some evidence online that previous to Monterey this syntax did successfully delete subfolders. It is also possible that a post-Monterey OS will fix this, as it seems very likely to be a bug.

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
  • 1
    Thank you so much for this detailed search and answer, which exactly reflects my analysis: This is a bug. I'm always astonished that there's so little to be found online regarding Applescript or JXA. As if only 10 peeps on the planet are using this. – the_last_voice Jan 21 '23 at 13:03