Creating all folders in a path en bloc with AppleScript

Hello.

This is for those who dislikes the *nix way to create every missing folder in a path:


set hfsPath to path to desktop
set neededPath to POSIX path of  hfsPath
do shell script "mkdir -p " &  neededPath

The foundation for the *nix way for creating a predetermined path is described thoroughly here. If what you want is to create a whole subtree somewhere that post is absolutely mandatory, and a very easy going good read.

I made this snippet in order to achieve the same with AppleScript, being a bit puristic at moment but I want the concepts to stick with me.
Well here it is, I used the text items intrinsic to AppleScript as a queue while traversing the path.
And it can be useful if you want to set other properties than name when creating the missing folders.


set theHfsPath to path to desktop as text
verifyThatFoldersExistEn_Bloc at theHfsPath 
-- creates folders in a path en bloc if they don't exist.
-- we check that the subtree exists as well.
to verifyThatFoldersExistEn_Bloc at hfsPath
	local tids
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	-- uses a current and a previous as one would in a linked list.
	set curPath to ""
	set prevPath to ""
	set folderList to text items of hfsPath
	repeat with aFolder in folderList
		set curFoldername to contents of aFolder
		set curPath to curPath & curFoldername
		try
			curPath as alias
		on error -- Oops curPath doesn't exist!
			tell application "Finder"
				try
					make new folder at folder prevPath with properties {name:curFoldername}
				on error
					display alert "verifyThatFoldersExistEn_Bloc: couldn't make the last folder at " & curPath & "Exits"
					set AppleScript's text item delimiters to tids
					error number -128
				end try
			end tell
		end try
		set curPath to curPath & ":"
		copy curPath to prevPath
	end repeat
	set AppleScript's text item delimiters to tids
end verifyThatFoldersExistEn_Bloc

Best Regards

McUsr