building a path [folders; mkdir]

I’m sure I can figure out how do this but I bet there some clever way that I won’t think of… so here it is

say you have script like this


set userFolder to ("" & (path to current user folder) & "Desktop:")
set mypath to userFolder & "test:test2:test3"

but the path doesn’t really exist yet or maybe the folder test does but not the rest. How would you test to see if each part of the path exists and build it

I have a snippet that check to see if a folder exists in path so that I believe will help me.

thanks
mm

So, if you started with an empty desktop, you would need folder “test” and “test2” created?

correct … if the folder text does not exist then would have create the folder test and the folder test2 inside of test and and test3 inside of test 2

mm

If “test3” is a folder, then the string should end with a colon.

thats correct as well sorry

Hi,

Probably the shell script is best here. Otherwise you would need to check each item in the path starting from desktop.


set dp to (path to desktop as string)
set as_path to (dp & "test1:test2:test3")
set u_path to POSIX path of as_path
do shell script "mkdir -p " & quoted form of u_path

I didn’t test timing or anything.

gl,

Kel beat me, but here’s a handler version:

(* With AppleScript 1.10 or later (Mac OS X 10.4 or later), you can safely pass a POSIX path to this handler. On earlier versions, you should only pass a colon-delimited path. *)

on makePath(somePath)
	set somePath to POSIX path of somePath
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set somePath to text 1 thru text item -2 of somePath
	set AppleScript's text item delimiters to ASTID
	
	do shell script "/bin/mkdir -p " & quoted form of somePath
end makePath

makePath((path to desktop as Unicode text) & "test:test2:filename")

Kei,

thats sweet thank you :smiley:

mm

Bruce thank you for your help as well:)

I tested it out and AppleScript won again! This probably can be made even faster:


--set t1 to the ticks -- uncomment to use Jon's Commands 'the ticks'
set dp to (path to desktop as string)
set build_path to dp
set folder_tree to "test1
test2
test3"
repeat with this_folder in paragraphs of folder_tree
	try
		set build_path to ((build_path & this_folder) as alias) as string
	on error
		tell application "Finder" to ¬
			set build_path to (make new folder at folder build_path with properties {name:this_folder}) as string
	end try
end repeat
--set t2 to the ticks
--display dialog (t2 - t1)

gl,

I ran it a bit more and now ‘mkdir’ is faster. Should have tested it more.

gl,

kel,
what was the difference in time ?

mm

Not too much. About two ticks or 1/30 th of a second difference. I really should test it better, but at first I got 16 ticks for mkdir and 4 ticks for AS. You’ll lose more time with the AS version as you add more folders to the path also.