create new folder with unique name - what am i missing??

I know this should have taken me all of 30 seconds to write; obviously I am missing some key concept here…

All I want to do is create a new folder in a designated location; the requirements are simply that a name has to be supplied and that name can not already be used.

Here’s where am I right now:



--set names for dummy text files
set textAnimation to "AFX, Motion, 3D, Etc"
set textDocs to "Any text based documents associated with the project"
set textDVDbuild to "DVD Studio Pro Project Files, and Builds"
set textDVDrip to "Source Ftg from DVD"
set textFinalExport to "Stand Alone Quicktime for Mastering"
set textSourceMovs to "Jumpbacks, Stock Ftg, Etc Go Here"
global theAssets
global nameProject

--eventually make this a global variable

set theAssets to choose folder with prompt "Please select your Assets folder"

repeat
	try
		set nameProject to name_Project()
		
		
		tell application "Finder"
			if (exists folder nameProject of theAssets) is false then
				exit repeat
			end if
		end tell
		
	end try
end repeat




tell application "Finder"
	make new folder at theAssets with properties {name:nameProject}
end tell



--subroutine to set name for new project; blank entry is not an option
on name_Project()
	
	repeat
		display dialog "Enter a name for the new project:" default answer ""
		try
			set tempName to the text returned of result
			if tempName is not "" then
				exit repeat
			else
				display dialog "Please enter a name for your project." buttons {"OK"}
			end if
		end try
	end repeat
	
	
	
	return tempName
	
end name_Project


This works, but there is no graceful way to exit. If the name is taken, it goes back to request a name, as I would like, but if the user decides to bail, pushing the “cancel” button simply goes back to request a name, because the script sees that as an empty name. I realize this is exactly what I have told it to do, but I’m just lost…

Thanks in advance.

Hi,

this should do it


.
--eventually make this a global variable

set theAssets to choose folder with prompt "Please select your Assets folder"

if name_Project() then
	tell application "Finder"
		make new folder at theAssets with properties {name:nameProject}
	end tell
end if

-- subroutine to set name for new project; blank entry is not an option
-- returns true on success or false, if the user presses Cancel. 
-- the folder name is stored in the global nameProject
on name_Project()
	repeat
		try
			set {text returned:tempName} to display dialog "Enter a name for the new project:" default answer ""
		on error number n
			if n = -128 then return false
		end try
		if tempName is not "" then
			tell application "Finder" to set folderExists to exists folder tempName of theAssets
			if folderExists then
				display dialog "The folder '" & tempName & "' exists already." & return & "Please choose another name" buttons {"Retry"} default button 1 giving up after 2
			else
				set nameProject to tempName
				return true
			end if
		else
			display dialog "Please don't leave the field blank." buttons {"Retry"} default button 1 giving up after 2
		end if
	end repeat
end name_Project

Perfect! Thanks very much. I see where I was going wrong in my process. I was trying to do the two tests (not blank and not used) as two separate “if” blocks as opposed to putting the second test within the “then” portion of the first test.

As I knew it would, it makes perfect sense when I see it laid out.

Thanks again.