how can I create variable sub folders within variable folders...

I am making my first attempt into applescript and think I have bitten off more than I can chew.

The intended purpose of my script is to do the following:

On inserting files to a folder user is promted for Street number of an apartment, the street name of an apartment and finally the unit number of an apartment.

The script has been set to start with a default location where i presently store my apartment pictures according to a set structure

I am seeking to have finder first see if there is a folder corresponding to street name first
if not create one with name equal to street name
then check to see if there is a folder within street name folder called streetnumber & streetname
if not create it if there is move on to
check if there is a folder named street number and street name and unit number
if there is not create it.

lastly put files intially dropped into folder into correctly named as street number & street name and apt number within street number & name within folder of street name

i.e ask input and receive 1 any street unit 4
place 1 any street unit 4 inside folder called 1 any street contained within folder any street.

after much searching I began adopting a script to fit my needs but think i am running into a problem when trying to determine the first sub folder.

running the script causes error “Can’t make {file "Flathound:pictures", "stName"} into type constant.” number -1700 from {file “Flathound:pictures”, “stName”} to constant

below is the code I have worked out so far.

Any suggestions on how to get on the correct path to solving this issue would be greatly appreciated…




set folderpath to "/Volumes/flathound/pictures" as POSIX file

set stNum to text returned of (display dialog "Enter Street Number:" default answer "")
set stName to text returned of (display dialog "Enter Street Name:" default answer "")
set aptNum to text returned of (display dialog "Enter the Apartment Number:" default answer "")
set address to {stNum & stName & aptNum}
set stnamenum to {stNum & stName}
set path_to_stName to (path to folderpath & text of "stName") as string
set path_to_stnamenum to (path to stNum & text of stName) as string
set path_to_address to (path_to_stnamenum & text of aptNum) as string
property new_folders : {"stName", "stNum", "aptNum"}
check_or_make(folderpath, "stname")
set path_to_user_folderpath to folderpath & "stname"

check_or_make(path_to_stnamenum, "stnum")
set path_to_stnamenum to path_to_address & "aptnum"


check_or_make(path_to_address, "aptnum")
set path_to_address to path_to_stnamenum & "aptnum"

tell application "Finder"
	if not (exists alias file (path_to_path_to_stnamenum & "aptnum")) then
		make new alias file at (path_to_path_to_address as alias) to folder path_to_address
	end if
end tell

repeat with a_folder_name in new_folders
	check_or_make(path_to_stnamenum, aptNum)
end repeat



to check_or_make(parent_folder, folder_name)
	tell application "Finder"
		if not (exists folder (parent_folder & folder_name & ":" as string)) then
			do shell script "cd " & quoted form of POSIX path of (parent_folder) & "; mkdir " & quoted form of folder_name
		end if
	end tell
end check_or_make


Hi,

there are some problems with POSIX file and path to
POSIX file is a file url and cannot be concatenated with another string
path to is a command of Standard Additions with specifies paths to predefined folders or to applications, nothing else.

You can create subfolders with mkdir. The -p switch creates intermediate directories and skips the line if the directory already exists.

Try something like this


set folderpath to "/Volumes/flathound/pictures/"

set stNum to text returned of (display dialog "Enter Street Number:" default answer "")
set stName to text returned of (display dialog "Enter Street Name:" default answer "")
set aptNum to text returned of (display dialog "Enter the Apartment Number:" default answer "")
set destinationFolder to folderpath & stNum & "/" & stName & "/" & aptNum & "/"
do shell script "/bin/mkdir -p " & quoted form of destinationFolder

Thank you so much for the insight, I am wondering now how would this handle folders already present in the structure.

ie i already have street name folder withing flathound/picture but do not have streetnumber and street name folder or the folder within that also containing unit number.

mkdir (with -p) creates the folder at the specified full path if it doesn’t exist.
If it exists, it does nothing

so i have re arranged some things so it fits the current file structure here is what i have now


tell application "Finder"
	set folderpath to "/Volumes/flathound/pictures" as POSIX file
	set stNum to text returned of (display dialog "Enter Street Number:" default answer "")
	set stName to text returned of (display dialog "Enter Street Name:" default answer "")
	set aptNum to text returned of (display dialog "Enter the Apartment Number:" default answer "")
	set address to {stNum & stName & aptNum}
	set stnamenum to {stNum & stName}
	try
		if not (exists folder) make new folder at "/Volumes/flathound/pictures" as POSIX file with properties {name:stName} then
		end if
	end try
	
	set child1 to make new folder at stName with properties {name:stNum & stName}
	set path_to_stnamenum to (path to "/Volumes/flathound/pictures" as POSIX file & stName) as string
	set path to address to (path to "/Volumes/flathound/pictures" as POSIX file & stNum & stName) as string
	check_or_make(path_to_folderpath & stName)
	set path_to_stName to path_to_stName & "stname:"
end tell

To tweak this i am now looking to set a white space between stnum and stname and another between stname and aptnum with a # character preceeding the aptnum.

my other question is does the case matter? If I have anystreet but user inserts Anystreet will a new folder starting with capital letter be created or does case get ignored.

THank you so much for the speedy responses and insight, I am already learning more than I could hope for.

Your script won’t work at all.
The Finder works with HFS paths (colon separated), it doesn’t understand class POSIX file
and path to aString doens’t work either

i messed up and copied the wrong script window here it is:

set folderpath to "/Volumes/flathound/pictures/"

set stNum to text returned of (display dialog "Enter Street Number:" default answer "")
set stName to text returned of (display dialog "Enter Street Name:" default answer "")
set aptNum to text returned of (display dialog "Enter the Apartment Number:" default answer "")
set destinationFolder to folderpath & stName & "/" & stNum & stName & "/" & stNum & stName & aptNum & "/"
do shell script "/bin/mkdir -p " & quoted form of destinationFolder

with mkdir and the Finder case doesn’t matter.
You can use any (allowed) character in the path. quoted form of handles special characters correctly. To insert the spaces and the number sign use this


set destinationFolder to folderpath & stName & "/" & stNum & space & stName & "/" & stNum & space & stName & "#" & aptNum & "/"