0
votes

I'am trying to create a Service which allow user to choose a folder, then show a list with options where he can choose from and based on the selected items, automator should create a folder for each chosen item in the list.

What I got so far:

1) Asked for Finder item => this allows user to choose a folder

2) JS where I create the options for the list

function run(input, parameters) {

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

 return array;
}

This gives me the following output when already when I run:

enter image description here

3) Added another JS-script Where I return the input (this returns only the selected items)

enter image description here

4) ... This is where I'am stuck. How do I create a folder now for each selected option from within the list?

Complete overview in next atachment

enter image description here

1

1 Answers

1
votes

You can use the chooseFromList command in your JavaScript instead of the "Choose from List" action and another "Run JavaScript" action.

You an use .map() to loop from the selected items.

Here's the script:

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;
    }); 
 }

Update

To create some subfolders within the new folder, use conditions (if name equal some name), like this:

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;
    }); 
 }