No internet connection
  1. Home
  2. How to

sf.file.directoryGetDirectories for Multiple Selections in Finder

By Nathan Salefski @nathansalefski
    2023-09-04 19:20:27.511Z

    I have a larger script that uses the sf.file.directoryGetDirectories to the directories of the currently selected folder in Finder. Currently it only works for one selection. I've tried to modify it to work regardless of how many folders are selected in Finder, but I can't seem to get it to work.

    function getDirectoryPaths([path]) {
        if (path.length > 1) {
    
            let allDirectoryPaths = []
    
            for (let i = 0; i < path.length; i++) {
                path = path[i]
    
                let directoryPaths = sf.file.directoryGetDirectories({ path, isRecursive: true, }).paths;
    
                allDirectoryPaths.push(directoryPaths);
            }
    
            return allDirectoryPaths;
        }
        else {
            path = path[0]
    
            let directoryPaths = sf.file.directoryGetDirectories({ path, isRecursive: true, }).paths;
    
            return directoryPaths;
        }
    }
    let selectedFinderFolder = sf.ui.finder.selectedPaths;
    
    let selectedFinderFolderDirectories = getDirectoryPaths([selectedFinderFolder]);
    
    log(selectedFinderFolderDirectories);
    
    Any help would be amazing! Thanks!
    Solved in post #4, click to view
    • 6 replies
    1. Kitch Membery @Kitch2023-09-04 19:53:58.593Z

      Try this.

      function getDirectoryPaths(directoryPath) {
          let directoryPaths = sf.file.directoryGetDirectories({
              path: directoryPath,
              isRecursive: true,
          }).paths;
          return directoryPaths;
      }
      
      function flattenArray(arrays) {
          const flatArray = [];
          for (let i = 0; i < arrays.length; i++) {
              const subArray = arrays[i];
              for (let j = 0; j < subArray.length; j++) {
                  flatArray.push(subArray[j]);
              }
          }
          return flatArray;
      }
      
      function main() {
          let selectedFinderPaths = sf.ui.finder.selectedPaths;
      
          let directoryPathArrays = selectedFinderPaths.map(path => getDirectoryPaths(path));
      
          let flattenedDirectoryPaths = flattenArray(directoryPathArrays)
      
          log(flattenedDirectoryPaths);
      }
      
      main();
      
      1. Nathan Salefski @nathansalefski
          2023-09-04 20:20:04.383Z

          AS expected because you're a genius this works. Now I do have a small issue. When trying to apply it as the original getDirectoryPaths was applied in this post (How to use looseMatch in Popup Menu #post-30), it isn't working. It's expecting a string and of course an Array is an object. Could we pop over there to see how it can be applied?

          1. Kitch Membery @Kitch2023-09-04 20:27:21.605Z

            Maybe this...

            function flattenArray(arrays) {
                const flatArray = [];
                for (let i = 0; i < arrays.length; i++) {
                    const subArray = arrays[i];
                    for (let j = 0; j < subArray.length; j++) {
                        flatArray.push(subArray[j]);
                    }
                }
                return flatArray;
            }
            
            function getDirectoryPaths(paths = []) {
                const getSingleDirectoryPaths = (directoryPath) => {
                    let directoryPaths = sf.file.directoryGetDirectories({
                        path: directoryPath,
                        isRecursive: true,
                    }).paths;
                    return directoryPaths;
                }
            
                let directoryPathArrays = paths.map(path => getSingleDirectoryPaths(path));
            
                let flattenedDirectoryPaths = flattenArray(directoryPathArrays)
            
                return flattenedDirectoryPaths;
            }
            
            let selectedFinderPaths = sf.ui.finder.selectedPaths;
            
            log(getDirectoryPaths(selectedFinderPaths));
            
            Reply1 LikeSolution
            1. Nathan Salefski @nathansalefski
                2023-09-04 21:54:55.772Z

                You're an actual GENIUS! I had to rework two parts of the code in the other post but now it's working for both single selected folders and multiple.

                Thank you!

                1. Kitch Membery @Kitch2023-09-04 21:56:01.978Z

                  Glad it worked for you, Nathan. :-)

                  1. Nathan Salefski @nathansalefski
                      2023-09-04 21:58:17.121Z

                      It's so awesome. As you know this is a part of a much larger idea but little by little it's becoming more universally usable.