Create folder structure and rename

I’m trying to create an workflow that will generate a folder structure (see below) where “ShowName” is a variable based on user input. I’ve managed to do it without any subfolders, but can’t manage to get it any further. Does it need to be done with Applescript?

ShowName
ShowName Folder 1
YYYY-MM-DD
ShowName Folder 2
ShowName Folder 3
zOLD
ShowName Folder 4
zOLD
ShowName Folder 5
ShowName Folder 6
ShowName Folder 7
YYYY-MM-DD
ShowName Folder 8
ShowName Folder 9
Folder A
Folder B
Folder C
ShowName Folder 10

Any ideas? Thanks for the help,

-adf

Hi,

in Automator

¢ add a Ask for Finder Items action, set the prompt to Choose Destination Folder (or whatever you like) and the type to Folders.
¢ add a Run AppleScript action and replace the code with


property prefix : ""

on run {input, parameters}
	set folderpath to POSIX path of (item 1 of input)
	
	set prefix to text returned of (display dialog "Enter Prefix:" default answer "")
	set timeStamp to do shell script "date +%Y-%m-%d"
	
	set folderHierarchy to "/{" & folderName(1) & "/" & timeStamp & ¬
		"," & folderName(2) & "," & folderName(3) & "/zOld" & ¬
		"," & folderName(4) & "/zOld" & ¬
		"," & folderName(5) & ¬
		"," & folderName(6) & "," & folderName(7) & "/" & timeStamp & ¬
		"," & folderName(8) & "," & folderName(9) & "/{'Folder A','Folder B','Folder C'}" & ¬
		"," & folderName(10) & "}"
	do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of prefix & "/" & folderHierarchy
	return input
end run

on folderName(v)
	return quoted form of (prefix & space & "Folder" & space & v)
end folderName

Hi Stefan,

Thanks for the quick reply.

That almost does it, but I’m afraid I misled you with my original post. The folder structure actually has the names below, which generally don’t change, but might some day in the future. The date stamp for the YYYY-MM-DD folders is handy, and will be useful for the future, but I actually just need the folders to literally be “YYYY-MM-DD” (it’s just meant to remind the user of the proper date format). One last thing, is it possible to tag the ShowName Drafting folder with a green label?

ShowName
ShowName Client Info & Drawings
YYYY-MM-DD
ShowName COI
ShowName Drafting
zOLD
ShowName LW
zOLD
ShowName Power
ShowName Prod. Management
ShowName Release
YYYY-MM-DD
ShowName Rig Photos
ShowName Show Order & Pricing
Folder A
Folder B
Folder C
ShowName Venue Info

Thanks again for all your help,

-adf

here we go


property prefix : ""

on run {input, parameters}
	set folderpath to item 1 of input
	
	set prefix to text returned of (display dialog "Enter Prefix:" default answer "")
	set folderHierarchy to "/{" & folderName("Client Info & Drawings") & "/YYYY-MM-DD," & folderName("COI") & ¬
		"," & folderName("Drafting") & "/zOld" & ¬
		"," & folderName("LW") & "/zOld" & ¬
		"," & folderName("Power") & ¬
		"," & folderName("Prod. Management") & ¬
		"," & folderName("Release") & ¬
		"/YYYY-MM-DD," & folderName("Rig Photos") & ¬
		"," & folderName("Show Order & Pricing") & "/{'Folder A','Folder B','Folder C'}" & ¬
		"," & folderName("Venue Info") & "}"
	do shell script "/bin/mkdir -p " & quoted form of POSIX path of folderpath & "/" & quoted form of prefix & "/" & folderHierarchy
	tell application "Finder" to set label index of folder (prefix & space & "Drafting") of folder prefix of folderpath to 6
	return input
end run

on folderName(v)
	return quoted form of (prefix & space & v)
end folderName

Amazingly perfect. Much appreciated.

-adf

Hey StarchyFind,

This job could be done simply enough with the Finder, but the shell makes it a bit easier still.

Since the folder structure might be changed in future I’d make it easy to read and maintain.

Personally I’d run this from FastScripts or Keyboard Maestro, so I’m not going to cover the Automator bit (and Stefan already has).

The script will look more orderly in the Applescript Editor or in an Automator AppleScript action, as the tabs will expand out properly.


set myShowName to text returned of (display dialog "Enter Show Name:" default answer "Your Show Name")

set shCMD to text 1 thru -1 of "
showName=" & quoted form of myShowName & ";
baseFolderPath=~/\"test_directory/TEST_FOLDER/\"
dtStr=\"YYYY-MM-DD\";

if [ -e \"$baseFolderPath$showName\" ]; then
	echo \"Error: The showName folder already exists!\";
else
	mkdir -p \"$baseFolderPath$showName\"
	cd \"$baseFolderPath$showName\";
	mkdir 												\\
		\"$showName Client Info & Drawings\"			\\
		\"$showName Client Info & Drawings/$dtStr\"		\\
		\"$showName COI\"								\\
		\"$showName Drafting\"							\\
		\"$showName Drafting/zOLD\"						\\
		\"$showName LW\"								\\
		\"$showName LW/zOLD\"							\\
		\"$showName Power\"								\\
		\"$showName Prod. Management\"					\\
		\"$showName Release\"							\\
		\"$showName Release/$dtStr\"					\\
		\"$showName Rig Photos\"						\\
		\"$showName Show Order & Pricing\"				\\
		\"$showName Show Order & Pricing/Folder A\"		\\
		\"$showName Show Order & Pricing/Folder B\"		\\
		\"$showName Show Order & Pricing/Folder C\"		\\
		\"$showName Venue Info\";

		echo \"$baseFolderPath$showName/$showName Drafting/\";
fi
"
set shResult to do shell script shCMD

if shResult starts with "Error" then
	error shResult
else
	set fldrToTag to alias POSIX file shResult
	tell application "Finder" to set label index of fldrToTag to 6
end if

Chris,

That’s fantastic. Your way makes a lot of sense. I like that it can be saved as an Applescript application, so the user need only to double-click it to run the script. This is going to be used in an office where users’ skill levels vary greatly, so the simpler, the better.

One thing I like about the Automator workflow is that it brought up a window that let you choose the directory for the script output. Is that something that could be added to your script? And/or could it use the directory in which the script resides as the output directory? Knowing both ways would be useful.

Many thanks,

-Andy

any compiled AppleScript can be saved as an application.

This is my approach without the Automator stuff and with a check whether the entered prefix already exists


property prefix : ""

set folderpath to choose folder with prompt "Choose Destination Folder"
repeat
	set prefix to text returned of (display dialog "Enter Prefix:" default answer "")
	tell application "System Events" to exists folder prefix of folderpath
	if result then
		display dialog "The folder '" & prefix & "' already exists. Please enter a different one" buttons {"OK"} default button 1
	else
		exit repeat
	end if
end repeat

set folderHierarchy to "/{" & folderName("Client Info & Drawings") & "/YYYY-MM-DD," & folderName("COI") & ¬
	"," & folderName("Drafting") & "/zOld" & ¬
	"," & folderName("LW") & "/zOld" & ¬
	"," & folderName("Power") & ¬
	"," & folderName("Prod. Management") & ¬
	"," & folderName("Release") & ¬
	"/YYYY-MM-DD," & folderName("Rig Photos") & ¬
	"," & folderName("Show Order & Pricing") & "/{'Folder A','Folder B','Folder C'}" & ¬
	"," & folderName("Venue Info") & "}"
do shell script "/bin/mkdir -p " & quoted form of POSIX path of folderpath & "/" & quoted form of prefix & "/" & folderHierarchy
tell application "Finder" to set label index of folder (prefix & space & "Drafting") of folder prefix of folderpath to 6

on folderName(v)
	return quoted form of (prefix & space & v)
end folderName

Thanks Stefan. Is there a way to have the script output to the directory in which the script is located so there’s no Choose Destination window?

Thanks,

-Andy

replace

set folderpath to choose folder with prompt "Choose Destination Folder"

with


set myPath to path to me
tell application "System Events" to set folderpath to path of container of myPath as alias

Hey Andy,

Sure, that’s easy enough.

I’ve added an error-handler.

The default location is the Desktop from whence the user can choose whatever location they want as the base folder.


try
	
	set baseFolderPath to POSIX path of (choose folder with prompt "Choose Root Folder:" default location (path to desktop))
	set myShowName to text returned of (display dialog "Enter Show Name:" default answer "Your Show Name")
	
	set shCMD to text 1 thru -1 of "
	showName=" & quoted form of myShowName & ";
	baseFolderPath=" & quoted form of baseFolderPath & ";
	dtStr=\"YYYY-MM-DD\";
	
	if [ -e \"$baseFolderPath$showName\" ]; then
		echo \"Error: The showName folder already exists!\";
	else
		mkdir -p \"$baseFolderPath$showName\"
		cd \"$baseFolderPath$showName\";
		mkdir 												\\
			\"$showName Client Info & Drawings\"			\\
			\"$showName Client Info & Drawings/$dtStr\"		\\
			\"$showName COI\"								\\
			\"$showName Drafting\"							\\
			\"$showName Drafting/zOLD\"						\\
			\"$showName LW\"								\\
			\"$showName LW/zOLD\"							\\
			\"$showName Power\"								\\
			\"$showName Prod. Management\"					\\
			\"$showName Release\"							\\
			\"$showName Release/$dtStr\"					\\
			\"$showName Rig Photos\"						\\
			\"$showName Show Order & Pricing\"				\\
			\"$showName Show Order & Pricing/Folder A\"		\\
			\"$showName Show Order & Pricing/Folder B\"		\\
			\"$showName Show Order & Pricing/Folder C\"		\\
			\"$showName Venue Info\";
	
			echo \"$baseFolderPath$showName/$showName Drafting/\";
	fi
	"
	set shResult to do shell script shCMD
	
	if shResult starts with "Error" then
		error shResult
	else
		set fldrToTag to alias POSIX file shResult
		tell application "Finder" to set label index of fldrToTag to 6
	end if
	
on error e number n
	set e to e & return & return & "Num: " & n
	if n ≠ -128 then
		try
			tell current application to button returned of ¬
				(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
					default button "OK" giving up after 30)
			if ddButton = "Copy" then set the clipboard to e
		end try
	end if
end try

That too is easy enough. Change the top line of the script to these two lines:


set AppleScript's text item delimiters to ":"
set baseFolderPath to POSIX path of (choose folder with prompt "Choose Root Folder:" default location alias (((text items 1 thru -3 of (path to me as text)) as text) & ":"))

Save the script as an AppleScript application, and you should be good to go.

Hi Chris,

I actually intended for this to automatically output to the same directory and not bring up a Choose Folder window, like it does in one of Stefan’s scripts.

-adf

Hey Andy,

That too is easy enough. Change that top line to this instead.


set AppleScript's text item delimiters to ":"
set baseFolderPath to POSIX path of (((text items 1 thru -3 of (path to me as text)) as text) & ":")

Chris and Stefan, thanks for all your help.

I had one thought for a future enhancement. Is it possible for the script, either before or after you choose the destination (after probably makes more sense), to have an ability to select between two (or possibly more) unique folder structures within the script?

For example, a dialog box with a button for “Theater” and “Corporate” and a button to Cancel.

I can easily make two different scripts, but it would just be cleaner and slicker to have it one script. If it’s a pain, no worries.

-adf

Hey Andy,

Of course. It’s a script. :cool:


set folderStructureTypeList to {"Theater", "Corporate", "How", "Many", "Choices", "Do", "You", "Want?"}
set fldrStrChoice to choose from list folderStructureTypeList default items (item 1 of folderStructureTypeList)