Creating Nested Nested Folders

I have created a clumsy AppleScript that creates a year folder and inside of it, 12 month folders (numbered 01–12). However, I want it to have 3 more sub folders inside each month folder for project specific items (EPS, PDF, Uploads), but after hours of trying, I can’t figure out how to make it happen. What would be the best way to create the three sub-folders inside each numbered month folder? Here’s what I have so far (I’m sure there must be a better way to write all of this).


tell application "Finder"
	activate
	try
		tell application "Finder" to set currentDirectory to target of Finder window 1
	on error
		display dialog "there is no open Finder window"
		return
	end try
	
	set folderYear to text returned of (display dialog "Please enter the year:" default answer "Folder Year")
	
	make new folder at currentDirectory with properties {name:folderYear}
	
	make new folder at folder folderYear of currentDirectory with properties {name:"01"}
	make new folder at folder folderYear of currentDirectory with properties {name:"02"}
	make new folder at folder folderYear of currentDirectory with properties {name:"03"}
	make new folder at folder folderYear of currentDirectory with properties {name:"04"}
	make new folder at folder folderYear of currentDirectory with properties {name:"05"}
	make new folder at folder folderYear of currentDirectory with properties {name:"06"}
	make new folder at folder folderYear of currentDirectory with properties {name:"07"}
	make new folder at folder folderYear of currentDirectory with properties {name:"08"}
	make new folder at folder folderYear of currentDirectory with properties {name:"09"}
	make new folder at folder folderYear of currentDirectory with properties {name:"10"}
	make new folder at folder folderYear of currentDirectory with properties {name:"11"}
	make new folder at folder folderYear of currentDirectory with properties {name:"12"}
		
end tell
[format][/format]

My objective is to make this a Finder window action that I can click to make the new year’s folder with all the appropriate folders inside. Any help is greatly appreciated.
⁓Rocky

Rocky. You just needed a few repeat loops.

tell application "Finder"
	activate
	try
		set currentDirectory to target of Finder window 1
	on error
		display dialog "there is no open Finder window"
		return
	end try
	
	set theYear to text returned of (display dialog "Please enter the year:" default answer "Folder Year")
	
	set theYearFolder to (make new folder at currentDirectory with properties {name:theYear})
	
	repeat with i from 1 to 12
		set theMonth to text -2 thru -1 of ("0" & i)
		set theMonthFolder to (make new folder at theYearFolder with properties {name:theMonth})
		repeat with anItem in {"EPS", "PDF", "Uploads"}
			make new folder at theMonthFolder with properties {name:anItem}
		end repeat
	end repeat
end tell

@peavine:
Potverdrie!!! You posted the exact same code I just wrote!

THANK YOU peavine and alastor933!

OMG! The hours I’ve spent on this is ridiculous! I am so over joyed and humbled.

Now… uhm, to add a little bit more complexity to this, is there a way to make it so the three sub-folders are ONLY in the ODD months (i.e., 1,3,5,7,9,11) and not in ALL of them?

Regardless, I can totally work with it the way you’ve scripted it (no biggie), but this slight alteration would truly be the ultimate goal of this script.

The generous sharing your (collective) brilliance is enormously appreciated.
Thank you, thank you, thank you!

⁓Rocky

The following should do that.

tell application "Finder"
	activate
	try
		set currentDirectory to target of Finder window 1
	on error
		display dialog "there is no open Finder window"
		return
	end try
	
	set theYear to text returned of (display dialog "Please enter the year:" default answer "Folder Year")
	
	set theYearFolder to (make new folder at currentDirectory with properties {name:theYear})
	
	repeat with i from 1 to 12
		set theMonth to text -2 thru -1 of ("0" & i)
		set theMonthFolder to (make new folder at theYearFolder with properties {name:theMonth})
		if i mod 2 is equal to 1 then
			repeat with anItem in {"EPS", "PDF", "Uploads"}
				make new folder at theMonthFolder with properties {name:anItem}
			end repeat
		end if
	end repeat
end tell
1 Like

WOW! I’m not going to pretend that I understand what you did here. As far as I can tell this is next level scripting. It worked absolutely perfectly.
Is there somewhere I can study some of what you did here? I really want to learn how to script better than I do. I’ve been playing with AS for decades and I still struggle with it.

Thank you profusely once again for completely solving this. You make it look simple but I know it’s really not.

⁓Rocky