AnsweredAssumed Answered

Applescript: Create a series of subfolders based on variable names

Question asked by breeanne on Dec 20, 2016
Latest reply on Dec 21, 2016 by breeanne



Goal: Export container fields with images in a defined folder structure with a script. The script needs to create the folders, so: AppleScript..


I have zero experience with AppleScript, but did some reading yesterday and today to try and wrap my brain around it... I reviewed a bunch of examples online and started building the AppleScript to do what I need, and got it to work to create 1 folder and 1 subfolder within the folder, but I don't understand the syntax enough to modify the file path to continue to add the next level of subfolders I need.


I have a table that has photos in a container field, and 4 other fields, each specifying the name for folders (if required, so some records may only have Folder1 and Folder2 populated, etc.). Folder 4 is a subfolder of Folder 3, which is a subfolder of Folder 2, etc. I also created $Folder to be "Exported Images" so, technically there are 5 potential folders.


Here is my AppleScript so far:


//Exported Images Folder

"set theOutputFolderPath to path to " & "desktop" & " folder" & ¶ &

"set theNewFolderName to " & Quote( $Folder) & ¶ &



"set theOutputFolder1Path to (path to " & "desktop" & " folder as text) &" & Quote( $Folder) & ¶ &

"set theNewFolder1Name to " & Quote( $Folder1) & ¶ &



// This is one place where I'm stuck... I need a way to add folder1 to the existing outputfolderpath but I can't understand the syntax...

"set theOutputFolder2Path to (path to " & "desktop" & " folder as text) &" & Quote( $Folder) & ¶ &

"set theNewFolder2Name to " & Quote( $Folder2) & ¶ &


//Creates Export Folder

"tell application \"Finder\" " & ¶ &

//if doesn't exist, make folder

"if (exists folder (theOutputFolderPath & theNewFolderName as string)) = false then make new folder at theOutputFolderPath with properties {name:theNewFolderName} " & ¶ &


//Creates Folder1

//if doesn't exist, make folder

"if (exists folder (theOutputFolder1Path & theNewFolder1Name as string)) = false then make new folder at theOutputFolder1Path  with properties {name:theNewFolder1Name} " & ¶ &

//Creates Folder2

//if doesn't exist, make folder

"if (exists folder (theOutputFolder2Path & theNewFolder2Name as string)) = false then make new folder at theOutputFolder2Path  with properties {name:theNewFolder2Name} " & ¶ &

"end tell"


I've read a little bit about shell scripts, but I don't really understand how the shell AppleScript would be able to source the variables. Would be nice to ditch all the extra quotes... they're quite confusing.


My thought was to run the script as a loop:

set Folder1 variables, etc based on first record

run apple script to check if folder exists (*** Another thing, I'm confused why I still get an error message saying the folder already exists. Maybe I don't understand the if statement, but there is no else specified, so it shouldn't do anything if it exists, yes?)


next record


Any help pointing me in the right direction will be much appreciated! Thanks so much!!