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 :
… into a folder structure like this :
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 :
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? :
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 ";"