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 ";"
Hi,
I recently tried to use this again on my new system which is running MacOS 12.
I now get the error: Not authorised to send Apple events to Finder. (-1743)
Do you know how to work around this? Going to System preferences > Privacy > Automation, it doesn’t seem possible to actively add an app here.
Would be great if I could get this working again.
Best
Joe
Hi Joe
I don’t have that OS here. I may have access to a 12 machine in a few days.
I think you may need to add the app under Security & Privacy > Accessibility.
m.
Hi,
thank you so much for the script, I don’t understand anything about programming ;-)
I’m very interested in the first script, it works but I get un error message:
Il est impossible d’obtenir item 1 of {} ( impossible to get item 1 of {} )
Also, I’m in France and we have special caracter (accent) like à or é which are replaced by strange signs…
Your help would be much appreciated!
Regards,
Jo
Hey many thanks for this, works great. I was just wondering is it possible to and how to amend the script to have main folders in column a and sub folders in column b sub sub folders in column c?
Hi MacGrunt,
I wanted to thank you for this post. We’ve been manually converting our folder structure outlines into actual folder structures, as each is as unique as the project requirements. With the information in this post, we can edit the folder structure up to an hour before going live thanks to this!
Many thanks for your feedback amelinamel. It’s great to know people are still using my stuff to improve their own workflows even after all these years. :-)
Hello,
Great work, as far as I understand. I am very interested in your script Create folders I and II to complete not a very complex hierarchy of folders from a list, but almost 300 lines (this is for a folder of a list of education training).
Thank you !
O.
Thanks O.
Coming your way
m.
I’m using this to create set of folder to create an organised applications folder in the dock.
I have over four hundred folders need to create and your script provide the foundation for me to create folders from a csv file. I managed to use mulitple myCSVfile to get what I exactly want. Thank you so much and you save me hours!
Bryan
That’s brilliant Bryan
Glad you were able to find something useful here.
Appreciate the feedback.
Have a good one mate.
m.
Could you use this to set the permissions (for the username to be the same as the top folder name) using the ‘owner’ and ‘owner permissions’ settings? I’m strungling to make this work within apple script editor (your code works brilliantly for what we want) and thinking that i might need to make another script to set this. Many thanks for the confirmation/pointers on this.
Oh yeah, that’s a great question George.
I can’t remember doing anything with permissions before, but I’d be interested to look into it.
Unfortunately I’m flat out for the next couple of days. I’ll get back to you as soon as I get a chance to give it a go.
m.
Hey! This is very useful. Thanks a lot. I have one more question. How do i input in the script to choose the destination folder for the folders to be created in?
Thanks agin
G’day Kai
By default the script creates the folders in the same location as the csv file.
If you want to have the script stop and ask you for a location then change this line :
set mgFolder to container of mgCSVfile as string
… to this :
set mgFolder to choose folder
m.
hey- this is awesome! any way to limit folder creation to a single column on the csv? i’m working from csv files with multiple columns of info and need to make folders from numbers is column A.
G’day Martin
If you just want Column A, then you don’t need all items of each paragraph, only item 1.
So, change this line :
set mgThisList to text items of paragraph x of mgList as list
to :
set mgThisList to text item 1 of paragraph x of mgList as list
I haven’t tested this in the context of the rest of that particular script.
I’d actually write the script differently for this functionality — but it may work with the simple line change.
See how you go and let us know.
You should also change this line :
set mgFolder to container of mgCSVfile as text
to :
set mgFolder to container of mgCSVfile as string
I’ll have to update that in the post — ‘as text’ has been deprecated I believe
m.
that seems to work just fine. thanks!!
I just found this page because I’m trying to do the opposite. Do you have an applescript that can look at a directory structure and output a CSV? Sorry I can’t figure it out on my own, I’m not an Applescript guru.
G’day
I haven’t done one yet but I’m happy to have a crack at it.
Send through a small example of a folder structure and how you’re hoping the resulting csv will look.
I’ll be able to have a look at in when I get back in a couple of days.
m.
AHH I love this post!! this for the reply!!! I will most def be using these scripts!!!