Creating multiply folders with five levels of folders

Hi
i’m a novice to apple scripting and i have got lost trying to create an app for making multiple levels of folders with set names when i drop the named folder on the scrip icon from the finder. I’m want to create 5 levels of folders. Can anyone please advise. thanks

property level1 : {“AW”, “Ads”, “Pics”, “Vis”, “FOH”, “Video”, “Logos”, “3D”, “Gifts”, “Misc”, “Pch”}
property level2 : {{“AW”, “-AW-2011”, “-AW-2012”}, {“Ads”, “-Ads-2011”, “-Ads-2012”, “-AdPicLog”}, {“Vis”, “-Vis-2011”, “-Vis-property temp : {”"}
global thetopLevel, showName, abbName

on open of theFiles
repeat with theFolder in theFiles
tell application “Finder” to set theFlag to exists of folder (theFolder as text)
if theFlag then
makeFolders(theFolder as text)
else
activate
beep
display dialog (theFolder as text) & " is not a folder. Cannot be processed!" buttons {“OK”} default button 1 with icon caution giving up after 4
end if
end repeat
activate
display dialog “Completed”
end open
on makeFolders(thetopLevel)
set AppleScript’s text item delimiters to {“:”}
set showName to (text item ((count of text items of thetopLevel) - 1) of thetopLevel) as text
set AppleScript’s text item delimiters to temp
tell application “Finder”
activate
repeat
set theR to display dialog “Enter the abbreviated Name” default answer showName buttons {“Cancel”, “OK”} default button 2
set theR to (text returned of theR) as text
if ((length of theR) + 9) ≥ 31 then
beep
display dialog “Name is too long! Please abbreviate the name (maximum of 21 characters)” buttons {“Cancel”, “OK”} default button 2 with icon caution
else
set abbName to theR
exit repeat
end if
end repeat
open folder thetopLevel
repeat with l1 in level1
set thel1Folder to thetopLevel & abbName & “-” & (l1 as text) & “:”
if (not (exists of folder thel1Folder)) then
make new folder at folder thetopLevel with properties {name:(abbName & “-” & (l1 as text))}
end if
repeat with addFolders from 1 to count of items of level2
if (item addFolders of level2) contains (l1 as text) then
copy (item addFolders of level2) to theAFolders
repeat with i from 2 to count of items of theAFolders
copy (item addFolders of level2) to theAFolders
repeat with i from 2 to count of items of theAFolders
if (not (exists of folder (thel1Folder & abbName & ((item i of theAFolders) as text)))) then
make new folder at folder thel1Folder with properties {name:(abbName & ((item i of theAFolders) as text))}
end if
end repeat
end if
end repeat
end repeat
end tell
end makeFolders

Please describe the task and your problem more fully. How should that folder tree look?
How & where did you get lost?

Hi thanks for your reply.

The applescript is an application. A user make a folder and gives it a name, for example ‘A Very Big Show’. He then drops the folder over the applescript application. The user is presented with a window requesting if you want to abbreviate the the folders to be created inside the main ‘A Very Big Show’ folder and lets say we type AVBS.

The script then makes a series of folders with the abbreviate AVBS-Ads, AVBS-AW, AVBS-FOH, AVBS-Vis, AVBS_Gifts, AVBS-Pics, AVBS-Mis on the first level

Also there are more folder created inside the folders of the first level. So inside AVBS-Ads there are AVBS-AdsPics, AVBS-Ads-2011, AVBS-Ads-2012 and so on with some of the other folders on the fist level. This makes the second level

I want to make more folders inside the second level and again making up to five levels

hope this help example below

thanks:o

A Very Big Show
AVBS-Ads
AVBS-Ads-Pics
AVBS-Ads-2011
AVBS-Ads-2012
AVBS-AW
AVBS-AW-2011
AVBS-AW-2012
AVBS-Video
AVBS-Fcp
AVBS-FcpAssets
AVBS-FCP-Mov
AVBS-FcpMov1080p
AVBS-FcpMov720p
AVBS-Aep
AVBS-Aep
AVBS-AepAssets
AVBS-AepMov
AVBS-Aep1080p
AVBS-Aep720p

Hi,

try this, you can extend the variable folderStructure with additional values.
I hope the syntax is understandable


property abbreviatedName : ""

on open theItems
	repeat with anItem in theItems
		set {name:folderName, folder:isFolder, package folder:isPackage} to info for anItem
		if isFolder and not isPackage then
			repeat
				set {text returned:abbreviatedName} to display dialog "Enter the abbreviated Name" default answer folderName buttons {"Cancel", "OK"} default button 2
				if ((length of abbreviatedName) + 9) ≥ 31 then
					beep
					display dialog "Name is too long! Please abbreviate the name (maximum of 21 characters)" buttons {"Cancel", "OK"} default button 2 with icon caution
				else
					exit repeat
				end if
			end repeat
			set folderStructure to "/{" & addPrefix("Ads") & "/{" & addPrefix("Ads-Pics") & "," & addPrefix("Ads-2011") & "," & addPrefix("Ads-2012") & "}," & ¬
				addPrefix("AW") & "/{" & addPrefix("AW-2011") & "," & addPrefix("AW-2012") & "}," & ¬
				addPrefix("Video") & "/{" & addPrefix("FcpAssets") & "," & addPrefix("FCP-Mov") & "/{" & addPrefix("FcpMov1080p") & "," & addPrefix("FcpMov720p") & "},}," & ¬
				addPrefix("Aep") & "/{" & addPrefix("Aep") & "," & addPrefix("AepAssets") & "," & addPrefix("AepMov") & "/{" & addPrefix("Aep1080p") & "," & addPrefix("Aep720p") & "},}," & ¬
				addPrefix("Vis") & "," & ¬
				addPrefix("FOH") & "," & ¬
				addPrefix("Video") & "}"
			do shell script "/bin/mkdir -p " & quoted form of POSIX path of anItem & folderStructure
		end if
	end repeat
end open


on addPrefix(value)
	return abbreviatedName & "-" & value
end addPrefix


I must say I much prefer the look of Stefan’s script, but this one reproduces the folder structure given in post #3, takes user input for the year folders, and offers suggestions in the input dialogs:

-- A list of parts for the eventual "mkdir" path parameter.
property bits : {missing value, "-{Ads/", missing value, "-Ads-{pics,", missing value, "},AW/", missing value, "-AW-{", missing value, "},Video/", missing value, "-{Fcp/", missing value, "-Fcp{Assets,Mov/", missing value, "-FcpMov{1080,720}p},Aep/", missing value, "-Aep{,Assets,Mov/", missing value, "-Aep{1080,720}p}}}"}

on open theItems
	repeat with thisItem in theItems
		-- Get relevant details for this dropped item.
		tell application "System Events"
			set {class:itemClass, package folder:isPackage, name:folderName} to disk item (thisItem as text)
			set isFolder to (itemClass is folder)
		end tell
		-- Process if it's not a file or a package folder.
		if ((isFolder) and not (isPackage)) then
			-- Ask the user for a suitable abbreviation for the folder name.
			set abbr to getAbbreviation(folderName)
			-- Insert the abbreviation into the bits list. This will also fill in the gaps for the years, but we'll overwrite these again in a moment.
			repeat with i from 1 to (count bits) by 2
				set item i of bits to abbr
			end repeat
			
			-- Ask the user for the years to use in the year-folder names.
			set theYears to getYears(folderName)
			-- Insert the years at the appropriate points in the bits list.
			set item 5 of bits to theYears
			set item 9 of bits to theYears
			
			-- Use what we've got to build a "mkdir" shell script command which will create the entire heirarchy.
			set astid to AppleScript's text item delimiters
			set AppleScript's text item delimiters to ""
			set shellCommand to "/bin/mkdir -p " & (quoted form of POSIX path of thisItem) & bits
			set AppleScript's text item delimiters to astid
			
			-- Execute the shell script.
			do shell script shellCommand
		else
			display dialog "The item "" & rootPath & "" isn't a folder!" buttons {"OK"} default button 1 with icon caution
		end if
	end repeat
end open

-- Get user input for the folder name abbreviation.
on getAbbreviation(folderName)
	-- Create an abbreviation to offer the user.
	set suggestedAbbr to ""
	repeat with thisWord in folderName's words
		set suggestedAbbr to suggestedAbbr & character 1 of thisWord
	end repeat
	
	-- Present it and get the response.
	set abbr to getInput(""" & folderName & "": What abbreviation should be used in subfolder names?", suggestedAbbr)
	repeat while ((count abbr) > 21)
		set abbr to getInput("The abbreviation is too long! Please abbreviate it further. (Maximum of 21 characters.)", suggestedAbbr)
	end repeat
	
	return abbr
end getAbbreviation

-- Get user input for the years in the year-folder names.
on getYears(folderName)
	-- Get this year and next to offer as suggestions.
	set year1 to year of (current date)
	set suggestedYears to (year1 as text) & ", " & (year1 + 1)
	
	-- Present them and get the response. Any number of years entered by the user (except none) will work.
	set theYears to getInput(""" & folderName & "": For what years are subfolders needed in the "Ads" and "AW" subfolders?", suggestedYears)
	
	-- Format the response for insertion into the bits list.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ","
	set theYears to theYears's words as text
	set AppleScript's text item delimiters to astid
	
	return theYears
end getYears

-- Get user input.
on getInput(prompt, suggestion)
	return text returned of (display dialog prompt default answer suggestion buttons {"OK"} default button 1 with icon note)
end getInput

Gentlemen, thank you very much, both work very well and i am indebted to you.

I wish i had your skills, but for someone like me this is a great community and resource

again thanks

Paul

I suppose rather than just being clever, we should explain that both scripts construct path parameters for the Unix “mkdir” command, whose job is to make a directory at a specified location. With the “-p” option, it’ll create every folder which doesn’t already exist in the given path. It can also take advantage of a kind of shorthand provided by UNIX which allows an entire heirarchy like yours to be specified, which saves having to call “mkdir” separately for each individual folder.

The shell script command eventually issued by Stefan’s script is:

do shell script "/bin/mkdir -p '/path/to/A Very Big Show/'/{AVBS-Ads/{AVBS-Ads-Pics,AVBS-Ads-2011,AVBS-Ads-2012},AVBS-AW/{AVBS-AW-2011,AVBS-AW-2012},AVBS-Video/{AVBS-FcpAssets,AVBS-FCP-Mov/{AVBS-FcpMov1080p,AVBS-FcpMov720p},},AVBS-Aep/{AVBS-Aep,AVBS-AepAssets,AVBS-AepMov/{AVBS-Aep1080p,AVBS-Aep720p},},AVBS-Vis,AVBS-FOH,AVBS-Video}"

And by mine:

do shell script "/bin/mkdir -p '/path/to/A Very Big Show/'AVBS-{Ads/AVBS-Ads-{pics,2011,2012},AW/AVBS-AW-{2011,2012},Video/AVBS-{Fcp/AVBS-Fcp{Assets,Mov/AVBS-FcpMov{1080,720}p},Aep/AVBS-Aep{,Assets,Mov/AVBS-Aep{1080,720}p}}}"

As you can see, the shorthand can be arranged in various ways. I’ve tried to give a brief overview here.