Wednesday, November 22, 2017

#1 2017-10-17 03:47:54 pm

paddirn
Member
Registered: 2017-10-17
Posts: 2

Script to create a Set of Folders within Currently Selected folder(s)

I have a script that I've been using for years that will create a set of folders within whatever selected folders I have (based on a script called 'NewFoldersSelect'). However, I'd like to modify the script to then create sub-folders within a few folders that were just created. How would I go about that?

So, for example, after the top layer of folders are created (folderNames), I'd like to create another set of folders titled "1", "2", "3", and "4" inside of the "RENDERS" folder (or any other folders from that initial set).

Sorry, I've looked through the forum and didn't see anything that I recognized that would apply to this code.


Applescript:

property folderNames : {"ANI & PATENT", "ARCHIVE", "IMAGES", "ONLINE INFO", "PQM", "PROOFREADING", "PROOFS", "RENDERS", "SOURCE DOCS"}

tell application "Finder"
   set selectedFolders to selection
   if (count of selectedFolders) > 0 then
       repeat with aFolder in selectedFolders
           set theLocation to aFolder as string
           repeat with newFolder in folderNames
               try
                   make new folder at theLocation with properties {name:"" & newFolder & ""}
               end try
           end repeat
       end repeat
   else
       set theLocation to (target of front window) as string
       repeat with newFolder in folderNames
           try
               make new folder at theLocation with properties {name:"" & newFolder & ""}
           end try
       end repeat
   end if
end tell

Offline

 

#2 2017-10-17 05:40:39 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4424

Re: Script to create a Set of Folders within Currently Selected folder(s)

Hi. Welcome to MacScripter.

It's possible to write a recursive routine using the Finder, but "mkdir" shell scripts work faster and are easier to write — once you're used to them!

Applescript:

-- To get subfolders "1", "2", "3", and "4" in the "RENDERS" folder only:
property hierarchy : "{'ANI & PATENT',ARCHIVE,IMAGES,'ONLINE INFO',PQM,PROOFREADING,PROOFS,RENDERS/{1,2,3,4},'SOURCE DOCS'}"
-- To get them in, say, both "PROOFS" and "RENDERS", use this instead:
-- property hierarchy : "{'ANI & PATENT',ARCHIVE,IMAGES,'ONLINE INFO',PQM,PROOFREADING,{PROOFS,RENDERS}/{1,2,3,4},'SOURCE DOCS'}"
-- Or to get them in all the folders:
-- property hierarchy : "{'ANI & PATENT',ARCHIVE,IMAGES,'ONLINE INFO',PQM,PROOFREADING,PROOFS,RENDERS,'SOURCE DOCS'}/{1,2,3,4}"

tell application "Finder"
   set selectedFolders to selection
   if (count selectedFolders) > 0 then
       repeat with aFolder in selectedFolders
           if (aFolder's class is folder) then set aFolder's contents to aFolder as alias
       end repeat
       set theLocations to selectedFolders's aliases
       if ((count theLocations) is 0) then error "No folders in the selection!"
   else if ((count Finder windows) > 0) then
       set folderAlias to (target of front Finder window) as alias
       set theLocations to quoted form of POSIX path of folderAlias
   else
       error "No selection and no container windows open!"
   end if
end tell

repeat with thisLocation in theLocations
   do shell script "mkdir -p " & (quoted form of POSIX path of thisLocation) & hierarchy
end repeat

Edit: Here's a devious variation which only needs to call 'do shell script' once, even when multiple folders are selected. I don't know if it's necessarily any better. (Further edit: Script below debugged to allow for single-folder selection (!) and both scripts now give explanatory error messages when the running conditions aren't met.)

Applescript:

-- To get subfolders "1", "2", "3", and "4" in "RENDERS" only:
property hierarchy : "{'ANI & PATENT',ARCHIVE,IMAGES,'ONLINE INFO',PQM,PROOFREADING,PROOFS,RENDERS/{1,2,3,4},'SOURCE DOCS'}"
-- To get them in, say, both "PROOFS" and "RENDERS", use this instead:
-- property hierarchy : "{'ANI & PATENT',ARCHIVE,IMAGES,'ONLINE INFO',PQM,PROOFREADING,{PROOFS,RENDERS}/{1,2,3,4},'SOURCE DOCS'}"
-- Or to get them in all the folders:
-- property hierarchy : "{'ANI & PATENT',ARCHIVE,IMAGES,'ONLINE INFO',PQM,PROOFREADING,PROOFS,RENDERS,'SOURCE DOCS'}/{1,2,3,4}"

tell application "Finder"
   set selectedFolders to selection
   
   if (count selectedFolders) > 0 then
       repeat with aFolder in selectedFolders
           if (aFolder's class is folder) then
               set folderAlias to aFolder as alias
               set aFolder's contents to quoted form of POSIX path of folderAlias
           end if
       end repeat
       set theLocations to selectedFolders's text
       set locationCount to (count theLocations)
       if (locationCount is 1) then
           set theLocations to beginning of theLocations
       else if (locationCount > 1) then
           -- Bracket the obtained folder paths together like the folder names in 'hierarchy'.
           set astid to AppleScript's text item delimiters
           set AppleScript's text item delimiters to ","
           set theLocations to "{" & (selectedFolders's text) & "}/"
           set AppleScript's text item delimiters to astid
       else
           error "No folders in the selection!"
       end if
   else if ((count Finder windows) > 0) then
       set folderAlias to (target of front Finder window) as alias
       set theLocations to quoted form of POSIX path of folderAlias
   else
       error "No selection and no container windows open!"
   end if
end tell

do shell script "mkdir -p " & theLocations & hierarchy

Last edited by Nigel Garvey (2017-10-17 11:36:59 pm)


NG

Offline

 

#3 2017-10-18 07:31:24 am

paddirn
Member
Registered: 2017-10-17
Posts: 2

Re: Script to create a Set of Folders within Currently Selected folder(s)

Works beautifully! Thank you.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)