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
Under Monterey, at least, deletion of subfolders in Photos is not possible via AppleScript/JXA. There appears to be a bug in
deletethat fails for subfolders, but not for top-level folders and not for any-level albums.We can verify both that
folderToDeletecontains a folder, and that this syntax for deletion is correct.This returns the id of that folder; you can also try
id of parent of folderToDeleteor even (in this example, since "Testing Folder Deletion" is at the third level)id of parent of parent of folderToDelete. Clearly,folderToDeleteis an actual item.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.
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 folderToDeleteas noted above, you cannotget every folder whose id is…orget every folder whose name is…. It will only return top-level folders that match, even on anidsearch.This will return the empty string. But:
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.
"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:This deletes the fifth top-level folder rather than either erroring out or deleting the fifth subfolder of “Workshop”. Fortunately,
Edit:Undo Delete Foldersuccessfully 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.
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:
Similarly, deleting sub-sub-albums does work in JXA:
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.