Automator Javascript - "从列表中选择"返回值

时间:2017-03-30 11:16:31

标签: javascript macos applescript directory automator

我正在尝试创建一个允许用户选择文件夹的服务,然后显示一个列表,其中包含可供选择的选项,并根据所选项目,automator应为每个所选项目创建一个文件夹。列表。

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

1)要求Finder item =>这允许用户选择文件夹

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

function run(input, parameters) {

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

 return array;
}

当我跑步时,这给了我以下输出:

enter image description here

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

enter image description here

4)......这就是我被困的地方。如何从列表中为每个选定的选项创建文件夹?

  

下一个atachment的完整概述

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以在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;
    }); 
 }