create folders from list II

the original create folders from list post prompted a question from Jocely. basically the question is… it’s all very good being able to create folders through applescript, but what about subfolders and whatnot? it’s such a great question — it needs its own post as a reply.

to run either of these scripts — copy it into script editor, save it out as an application, then double-click to run it. you’ll be prompted to select your csv file — then the folders will be created in the same location as the csv file.

ok, scripts build up over time — first you try something and, once you get it working, you add, refine, stabilise, improve, etc. that’s how this development progressed — the first stab at this challenge looks like this :

set mgCSVfile to choose file
open for access mgCSVfile
set mgList to (read mgCSVfile)
close access mgCSVfile

tell application "Finder"
  set mgFolder to container of mgCSVfile as string
  repeat with x from 1 to count paragraphs of mgList
    set text item delimiters of AppleScript to ","
    set mgThisList to text items of paragraph x of mgList as list
    set text item delimiters of AppleScript to ""
    
    set mgTopFolder to item 1 of mgThisList
    if (exists folder mgTopFolder of folder mgFolder) is false then
      make new folder at mgFolder with properties {name:mgTopFolder}
    end if
    set mgNewFolder to (folder mgTopFolder of folder mgFolder) as alias
    
    repeat with i from 2 to count mgThisList
      if item i of mgThisList is not "" then
        set mgSubFolder to item i of mgThisList
        if (exists folder mgSubFolder of folder mgNewFolder) is false then
          make new folder at mgNewFolder with properties {name:mgSubFolder}
        end if
      end if
    end repeat
    
  end repeat
end tell

… and it will render a csv file like this :
screen grab of basic csv file
… into a folder structure like this :
screen grab of first folder structure

well, that’s not a bad start. but what about a folder hierarchy a little more complex than that? we need to go deeper with the nesting. so here’s the second stab. and you’ll notice it’s actually a simpler script :

set mgCSVfile to choose file

open for access mgCSVfile
set mgList to (read mgCSVfile)
close access mgCSVfile

tell application "Finder"
  repeat with x from 1 to count paragraphs of mgList
    set mgFolder to container of mgCSVfile as string
    set text item delimiters of AppleScript to ","
    set mgThisList to text items of paragraph x of mgList as list
    set text item delimiters of AppleScript to ""
    
    repeat with i from 1 to count mgThisList
      if item i of mgThisList is not "" then
        set mgSubFolder to item i of mgThisList
        if (exists folder mgSubFolder of folder mgFolder) is false then
          make new folder at mgFolder with properties {name:mgSubFolder}
        end if
        set mgFolder to (folder mgSubFolder of folder mgFolder) as alias
      end if
    end repeat
    
  end repeat
end tell

… and the results using the same csv file :
screen grab of second folder structure

now we see that each line (paragraph) in the csv file renders a single nested folder structure. this is more like what we’re looking for. what do you reckon you’ll get if you run this csv file through that second script? :

screen grab of more complex csv file

you can get a copy of the CreateFoldersII app here

update : if you are in the german or dutch regions you may find that your CSV files export as semi-colon separated (rather than comma separated). in which case you would replace this line :

    set text item delimiters of AppleScript to ","

with this :

    set text item delimiters of AppleScript to ";"

macgrunt icon

Advertisements

create folders from list

a recent digital publishing workflow required the creation of over 90 folders (one for each chapter of the book) and each of these also had to include a ‘links’ folder. imagine trying to do that manually — over 90 folders.

applescript droplet icon this is a perfect job for an applescript droplet — drop a text file on the app and watch the folders appear — as if by magic :

screen grab of finder before and after text file is dropped on droplet

this workflow starts with a comma delimited list of folder names. you could also use a tab delimited list. note: this version of the script does not handle returns (line breaks) in the list. the demo list looks like this :

screen grab of comma delimited list

you can get a copy of the CreateFolders app here

screen grab of create folders script compiled in script editor

for those interested in this sort of thing…
this script shows one way to strip leading and trailing spaces from chunks of text. it uses two repeat while loops, each of which keep repeating for as long as the associated statement is true. the repeat loop ceases as soon as the statement is false, and the script moves on.

you could use the repeat until form instead :
repeat until mgThisItem does not start with ” ”

the last part of the script shows a simple, pure applescript way to create folders in the finder. the syntax is tortured and long-winded, but it works a treat. to learn how to create folders and folder hierarchies by scripting the shell, see nigel garvey’s excellent explanation at macscripter

update : if you are in the german or dutch regions you may find that your CSV files export as semi-colon separated (rather than comma separated). in which case you would replace this line :

    set text item delimiters of AppleScript to ","

with this :

    set text item delimiters of AppleScript to ";"

macgrunt icon