简体   繁体   English

无法通过 Applescript 或 JXA 删除照片中的子文件夹?

[英]Delete Subfolders in Photos via Applescript or JXA not possible?

I'm stuck, since I found no way to delete a subfolder in Apple's Photos.我被卡住了,因为我找不到删除 Apple 照片中的子文件夹的方法。 Neiter via Applescript nor with JXA.既不通过 Applescript 也不使用 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.删除顶层文件夹没有问题,但尝试删除子文件夹(文件夹中的文件夹)总是会导致错误,无法找到 object。

I spend hours on this, now.我现在花几个小时在这上面。

None of these JXA commands work:这些 JXA 命令都不起作用:

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?任何人都可以帮助我并告诉我通过 Applescript 或 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.至少在 Monterey 下,无法通过 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.似乎有一个delete子文件夹失败的错误,但不是顶级文件夹,也不是任何级别的相册。

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.我们可以验证folderToDelete是否包含一个文件夹,以及这个删除语法是否正确。

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;这将返回该文件夹的 ID; 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 .您也可以尝试id of parent of folderToDelete或什至(在此示例中,因为“测试文件夹删除”处于第三级) id of parent of parent of folderToDelete Clearly, folderToDelete is an actual item.显然, folderToDelete是一个实际项目。

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.如果仅针对子文件夹更改语法,那将是非常奇怪的(尽管并非超出 realm 的可能性)。

The same is true for JXA. 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”.这将删除名为“顶级文件夹”的顶级文件夹。

Note that there is an interesting twist in JXA in which the result (using the syntax I've used) is always a list.请注意,JXA 中有一个有趣的变化,其中结果(使用我使用的语法)始终是一个列表。 This is probably because this is the equivalent of AppleScript's get folders of folders of folders whose name is "Testing Folder Deletion" .这可能是因为这相当于 AppleScript 的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.) (遗憾的是, get folder of folders of folders whose name is "Testing Folder Deletion"虽然不是语法错误,但会返回一个空列表列表。它不仅不返回请求的文件夹,还会继续返回一个列表。 )

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:这将失败,就像在 AppleScript 中一样。您可以通过获取文件夹的属性或父文件夹的属性,以类似于我在 AppleScript 中使用的测试的方式来测试它是否确实具有该文件夹:

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

Similarly, deleting sub-sub- albums does work in JXA:同样,删除子相册在 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.您可能希望在问题中指定您的 macOS 版本。 There is some evidence online that previous to Monterey this syntax did successfully delete subfolders.网上有一些证据表明,在 Monterey 之前,此语法确实成功删除了子文件夹。 It is also possible that a post-Monterey OS will fix this, as it seems very likely to be a bug.后 Monterey 操作系统也有可能解决这个问题,因为它看起来很可能是一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM