title: Applescript: Recursive Folders of Selected Folder
body: <p>Using applescript, I'm trying to create a .patch file in a set of parallel folders for every .recipe file in a target folder and its sub-folders. This also includes creating a parallel folder structure as the script does it's thing. I've got the steps right, but some part of the detailed grammar is wrong and I can't figure out what and how to fix it. Debugging information I've read so far has not helped me, though I'm sure this is a simple matter of incorrect applescript grammar. Here is the code:</p>
  <pre><code>use AppleScript version &quot;2.4&quot; -- Yosemite (10.10) or later
  use scripting additions
  
  on run
      set patchFile to (choose file with prompt &quot;Select patch file...&quot;)
      set assetsFolder to (choose folder with prompt &quot;Select recipe assets folder...&quot;)
      set dumpFolder to (choose folder with prompt &quot;Select dump folder...&quot;)
      
      makePatches(patchFile, assetsFolder, dumpFolder)
  end run
  
  on makePatches(patchFile, targetFolder, dumpFolder)
      tell application &quot;Finder&quot;
          set recipeCount to count (files in folder (targetFolder) whose name ends with &quot;.recipe&quot;)
          if (recipeCount &gt; 0) then
              set recipeFiles to files in folder (targetFolder) whose name ends with &quot;.recipe&quot;
              repeat with eachFile in recipeFiles
                  set recipePatch to duplicate patchFile to folder dumpFolder
                  set recipePatch's name to eachFile's name &amp; &quot;.patch&quot;
              end repeat
          end if
          
          set folderCount to (folders in folder (targetFolder) whose visible is true)
          if (folderCount &gt; 0) then
              set subFolders to folders in folder (targetFolder)
              repeat with eachFolder in subFolders
                  set newName to folder eachFolder's name
                  set newFolder to make new folder at folder dumpFolder
                  set newFolder's name to newName
                  my makePatches(patchFile, eachFolder, newFolder)
              end repeat
          end if
      end tell
      return
  end makePatches
  </code></pre>
  <p>Note that if I remove the code for iterating through the folders and simply have it create patches for each .recipe file in a folder, then that code works. Or at least, it does as far as I can tell. So the problem should be somewhere at or after folderCount. The recursion is needed, as is the ability to use choose with prompt. Hopefully that isn't a problem. What is the correct grammar for the iteration section?</p>