在 Mac OS 上使用 JXA 发送到垃圾箱

Send to Trash with JXA on Mac OS

所以我一直在寻找这个问题的答案,但一直没有成功。是否可以在 Mac Automation 中使用 JXA 将文件发送到回收站?我的简单测试代码如下所示:

// set startup applications that this script will use
var app = Application.currentApplication()
var finder = Application("Finder");
app.includeStandardAdditions = true 


function openDocuments(droppedItems)
{
    // Variables
    var AllFiles = [] // array to store all files in.

    for (var item of droppedItems)
    {
        AllFiles.push(item) // load each file into array
    }

    // go through each file in the list
    for (var i = 0; i < AllFiles.length; i ++)
    {
        // move to the trash
        finder.move(Path(AllFiles[i]), {
                to: Path("/Users/usr/.trash"),
                replacing: true
            })
    }
}

这只是我正在构建的一个测试,它应该将我放在其中的任何文件发送到垃圾箱,但它不会将 .trash 识别为有效的文件夹位置。我已经用其他文件夹对其进行了测试,它确实有效,所以我假设 .trash 已被锁定。

我认为您需要通过 Standard Additions

pathTo 命令引用垃圾文件夹

例如,要将 Finder 中当前选定的文件发送到废纸篓,类似这样的方法就可以了。

(() => {
    const
        ca = Application.currentApplication(),
        sa = (ca.includeStandardAdditions = true, ca),
        app = Application('Finder'),
        seln = app.selection();

        app.delete(seln[0])
})();