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

问题描述 投票:0回答:1

所以我一直在寻找答案,没有运气。是否可以使用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已被锁定。

javascript macos automation jxa
1个回答
0
投票

我认为您需要通过Standard Additions的pathTo命令引用垃圾文件夹

例如,要将Finder中当前选定的文件发送到“废纸篓”,这样的操作可能会起作用。

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

        app.delete(seln[0])
})();
© www.soinside.com 2019 - 2024. All rights reserved.