Help with folder generation

I have pieced together two scripts that I have that will generate a set of folders in a correct structure based on a user inputting a number

Two issues:

  1. I set it up to replace any foreign characters that the user may input in the name with “_”
    This works fine, but one of the folders generated are “12345_Transmissions”, and I cannot get the script to create two folders within this one called “In” & “Out”

  2. I haven’t been working with applescript in a while, and I am trying to refresh myself, so I realize that this script may be more complex than needed, Is there an easier way to write what I need

Basically I want a user to give me a job number based on this structure:“XXXX_XXXX_12345”, this number is used to build a file structure like this:
“XXXX_XXXX_12345”-main folder
“12345_12345_EPS_TIF”, “12345_ILL_EPS”, “12345_Old_QXD”, “12345_PSD”, “12345_Resources”, “12345_Transmissions” as sub-folders inside main folder

Inside of “12345_Transmissions” I need two folders: “In” & “Out”

Here’s what I have thus far:

on run

set destination_ to choose folder with prompt "Choose a destination for the job folder."
set dd to (display dialog "Enter Your FCB Job Number." default answer "XXXX_XXXX_12345")
set prefix_ to text -5 thru end of text returned of dd
set subfolder_names to {"Old_QXD", "ILL_EPS", "PSD", "Transmissions", "Resources", "EPS_TIF"}
set transmission_names to {"In", "Out"}

tell application "Finder"
	try
		--make FCB Job Number Folder--	
		
		set nf to make new folder at destination_ with properties {name:text returned of dd}
		
		--make sub-folders for job--
		
		repeat with name_ in subfolder_names
			set sub_ to (make new folder at nf with properties {name:prefix_ & "_" & name_})
		end repeat
		
		set elem to nf as string
		set fileItem to alias elem
		set strName to name of fileItem
		set replace_strings to ¬
			{" ", "  ", "-", "_", "__", "~", "`", ":", ";", "!", "@", "™", "'", "®", "#", "$", "?", "<", ">", "%", "^", "&", ¬
				"*", "(", ")", "=", "+", "{", "}", "[", "]", "|", "\", "/", "'", ",", """}
		repeat with this_replace_string in replace_strings
			set strName to my FixName(strName, (contents of this_replace_string))
		end repeat
		set name of nf to strName
		
		--making In & Out folder in Transmissions--
		get name of sub_
		if name of sub_ is (prefix_ & "_Transmissions") then
			repeat with item_ in transmission_names
				make new folder at sub_ with properties {name:item_}
			end repeat
		end if
	on error e
	end try
end tell

end run

on FixName(currentName, fixString)
set AppleScript’s text item delimiters to fixString
set listName to every text item of currentName
set AppleScript’s text item delimiters to “_”
return (listName as string)
end FixName

With regard to your first issue, your script is checking the name of sub_ after all the names in subfolder_names have been processed — that is, when sub_ is folder “EPS_TIF”. It would be better to check during the creation loop. This has an additional advantage in that you already have the relevant part of each name to hand and don’t need to read it back from each folder:

-- (tell application "Finder")
      --make sub-folders for job--
      
      repeat with name_ in subfolder_names
        set sub_ to (make new folder at nf with properties {name:prefix_ & "_" & name_})
        if name_ ends with "Transmissions" then
          repeat with item_ in transmission_names
            make new folder at sub_ with properties {name:item_}
          end repeat
        end if
      end repeat

I would also fix the name of name of the job folder first, before actually creating it, to save having to read it back again from the folder later.

As an alternative to using the Finder, OS X has a Unix method for creating folders which is pretty neat when you get the hang of it, and very fast. It can be invoked from ‘do shell script’:

on run
	
	set destination_ to (choose folder with prompt "Choose a destination for the job folder.") as string
	set dd to (display dialog "Enter Your FCB Job Number." default answer "XXXX_XXXX_12345")
	set jobName to text returned of dd
	set prefix_ to text -5 thru end of jobName & "_"
	set subfolder_names to prefix_ & "{Old_QXD,ILL_EPS,PSD,Resources,EPS_TIF}" -- no spaces
	set transmission_names to prefix_ & "Transmissions/{In,Out}" -- no spaces
	
	set replace_strings to ¬
		{" ", "  ", "-", "_", "__", "~", "`", ":", ";", "!", "@", "™", "'", "®", "#", "$", "?", "<", ">", "%", "^", "&", ¬
			"*", "(", ")", "=", "+", "{", "}", "[", "]", "|", "\", "/", "'", ",", """}
	set astid to AppleScript's text item delimiters
	repeat with this_replace_string in replace_strings
		set jobName to my FixName(jobName, (contents of this_replace_string))
	end repeat
	set AppleScript's text item delimiters to astid
	
	set jobFolderPOSIX to POSIX path of (destination_ & jobName) & "/"
	-- Create the job folder and all subfolders except "Transmissions".
	-- "mkdir -p /path/to/jobFolder/prefix_{Old_QXD,ILL_EPS,PSD,Resources,EPS_TIF}"
	do shell script ("mkdir -p " & jobFolderPOSIX & subfolder_names)
	-- Create the "Transmissions" folder and its "In" and "Out" subfolders.
	-- "mkdir -p /path/to/jobFolder/prefix_Transmissions{In,Out}"
	do shell script ("mkdir -p " & jobFolderPOSIX & transmission_names)
	tell application "Finder" to update folder destination_
end run

on FixName(currentName, fixString)
	set AppleScript's text item delimiters to fixString
	set listName to every text item of currentName
	set AppleScript's text item delimiters to "_"
	return (listName as string)
end FixName

Further research this morning shows that it’s possible to specify the entire hierarchy in one string:

"mkdir -p " & (POSIX path of (destination_ & jobName)) & "/" & ¬
  prefix_ & "{Old_QXD,ILL_EPS,PSD,Transmissions/{In,Out},Resources,EPS_TIF}" -- no spaces

So only one ‘do shell script’ call is needed!

on run
  
  set destination_ to (choose folder with prompt "Choose a destination for the job folder.") as string
  set dd to (display dialog "Enter Your FCB Job Number." default answer "XXXX_XXXX_12345")
  set jobName to text returned of dd
  set prefix_ to text -5 thru end of jobName & "_"
  set subfolder_spec to prefix_ & "{Old_QXD,ILL_EPS,PSD,Transmissions/{In,Out},Resources,EPS_TIF}" -- no spaces
  
  set replace_strings to ¬
    {" ", "  ", "-", "_", "__", "~", "`", ":", ";", "!", "@", "™", "'", "®", "#", "$", "?", "<", ">", "%", "^", "&", ¬
      "*", "(", ")", "=", "+", "{", "}", "[", "]", "|", "\", "/", "'", ",", """}
  set astid to AppleScript's text item delimiters
  repeat with this_replace_string in replace_strings
    set jobName to my FixName(jobName, (contents of this_replace_string))
  end repeat
  set AppleScript's text item delimiters to astid
  
  set jobFolderPOSIX to (POSIX path of (destination_ & jobName)) & "/"
  -- Create the job folder and all subfolders.
  -- "mkdir -p /path/to/jobFolder/prefix_{Old_QXD,ILL_EPS,PSD,Transmissions/{In,Out},Resources,EPS_TIF}"
  do shell script ("mkdir -p " & jobFolderPOSIX & subfolder_spec)
  tell application "Finder" to update folder destination_
end run

on FixName(currentName, fixString)
  set AppleScript's text item delimiters to fixString
  set listName to every text item of currentName
  set AppleScript's text item delimiters to "_"
  return (listName as string)
end FixName

If spaces are required in folder names, the relevant parts can be enclosed in single quotes:

set subfolder_spec to prefix_ & "{Old_QXD,ILL_EPS,PSD,Transmissions/{In,Out/{Back,Front}' door'},Resources,EPS_TIF}"