【问题标题】:Send to Trash with JXA on Mac OS在 Mac OS 上使用 JXA 发送到废纸篓
【发布时间】:2017-12-30 03:35:21
【问题描述】:

所以我一直在寻找这个问题的答案,但没有运气。是否可以在 Mac 自动化中使用 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 javascript-automation


    【解决方案1】:

    我认为您需要通过 Standard Additions

    pathTo 命令引用垃圾文件夹

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

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

    【讨论】:

    • 所以我尝试了这个,但我收到“无法转换类型”的错误。我还尝试使用 .trash 以防万一,因为它是一个隐藏文件夹。 pathTo 可能是答案。我会看看我是否可以让它返回一个正确的文件路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多