Saturday, November 27, 2021

#1 2021-11-16 12:34:58 pm

Rocadero
Member
Registered: 2010-02-20
Posts: 8

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).

Applescript:


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

Offline

 

#2 2021-11-16 01:11:27 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Creating Nested Nested Folders

Rocky. You just needed a few repeat loops.

Applescript:

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


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#3 2021-11-16 01:19:18 pm

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 587

Re: Creating Nested Nested Folders

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

Offline

 

#4 2021-11-16 01:56:25 pm

Rocadero
Member
Registered: 2010-02-20
Posts: 8

Re: Creating Nested Nested Folders

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

Offline

 

#5 2021-11-16 03:11:13 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Creating Nested Nested Folders

Rocadero wrote:

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?



The following should do that.

Applescript:

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


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#6 2021-11-16 04:20:29 pm

Rocadero
Member
Registered: 2010-02-20
Posts: 8

Re: Creating Nested Nested Folders

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)