Automator Javascript - "Choose From List" return 值

Automator Javascript - "Choose From List" return values

我正在尝试创建一个允许用户选择文件夹的服务,然后显示一个列表,其中包含用户可以从中进行选择的选项,并且基于所选项目,自动化程序应该为每个所选项目创建一个文件夹列表。

到目前为止我得到了什么:

1) 询问 Finder 项目 => 这允许用户选择一个文件夹

2) 我在其中为列表创建选项的 JS

function run(input, parameters) {

 var array = ["PDF", "LINKS", "PERSMAP", "SCHUIMKARTON", "INSPIRATIE"];
 var arrayLength = array.length;

 return array;
}

当我 运行:

时,这给了我以下输出

3) 在我 return 输入的地方添加了另一个 JS 脚本(这 return 只有选定的项目)

4) ... 这就是我卡住的地方。我现在如何为列表中的每个选定选项创建一个文件夹?

Complete overview in next atachment

您可以在 JavaScript 中使用 chooseFromList 命令代替“从列表中选择”操作和另一个“运行 JavaScript”动作。

您使用 .map() 从所选项目循环。

这是脚本:

function run(input, parameters) {
    thisApp = Application.currentApplication()
    thisApp.includeStandardAdditions = true
    var destFolder = input[0];// the destination folder
    var array = ["PDF", "LINKS", "PERSMAP", "SCHUIMKARTON", "INSPIRATIE"];

    theseNames = thisApp.chooseFromList(array, {withPrompt: 'Which folders to create?', multipleSelectionsAllowed:true});
    if (theseNames == false) {return}; // exit this script because user cancelled

    // (for each selected name then create the folder if this name does not already exists in the destination folder), return path of these sub-folders to the next action
    return theseNames.map(function(thisName) { 
        newFolder = destFolder + "/" + thisName + "/";// concatenation of the destination folder and an item in theseNames
        $.NSFileManager.defaultManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(newFolder,false,$(), $());
        return newFolder;
    }); 
 }

更新

要在新文件夹中创建一些子文件夹,请使用条件 (如果名称等于某个名称),如下所示:

function run(input, parameters) {
    thisApp = Application.currentApplication()
    thisApp.includeStandardAdditions = true
    var destFolder = input[0];// the destination folder
    var array = ["PDF", "LINKS", "PERSMAP", "SCHUIMKARTON", "INSPIRATIE"];

    theseNames = thisApp.chooseFromList(array, {withPrompt: 'Which folders to create?', multipleSelectionsAllowed:true});
    if (theseNames == false) {return}; // exit this script because user cancelled

    f_m = $.NSFileManager.defaultManager;
    // (for each selected name then create the folder if this name does not already exists in the destination folder), return path of these sub-folders to the next action
    return theseNames.map(function(thisName) { 
        newFolder = destFolder + "/" + thisName + "/";// concatenation of the destination folder and an item in theseNames
        f_m.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(newFolder,false,$(), $());
        if (thisName == "PDF") { // create folder1 and folder2 in the PDF folder
            f_m.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(newFolder + "folder1",false,$(), $());
            f_m.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(newFolder + "folder2",false,$(), $());
        }
        if (thisName == "INSPIRATIE") { // create folderX in the INSPIRATIE folder
            f_m.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(newFolder + "folderX",false,$(), $());
        }
        return newFolder;
    }); 
 }