Kitroyan

Q: Apple Script Editor: Adding sub sub folders.

Hello I am very very green when it comes to Apple Script and I having an issue figuring this out.

I have a script that lets me Create a folder at location I choose and then populate it with sub folders. Now I need to do the same but add sub sub folders to this sub folders. I have been though what feels like thousands of forums and tried different options and they end up being totally wrong or error full. Here is my script:

 

property subf_names : {"Contract Proposals", "Existing Conditions", "Programming", "Code-Permit", "Design Sketches", "Construction Documentation", "Bidding", "Contract Administration (CA)", "Close-Out", "Product Info", "Photos", "Cost Data", "Communication"}

 

display dialog "Enter Job Number:" default answer "0"

set j_number to text returned of result

repeat

  display dialog "Enter Job Name:" default answer "job name"

  set j_name to text returned of result

  try

  if (j_number as integer) < 10000 and ¬

  (j_number as integer) > -1 then exit repeat

  on error

  display dialog "Error: Project Number is already taken."

  end try

end repeat

set job_number to text -5 thru -1 of ("00" & j_number & " ")

set temp_name to job_number & j_name

tell application "Finder"

  set main_folder to (make new folder at {choose folder} with properties {name:temp_name}) as alias

  repeat with this_name in subf_names

  set temp_name to job_number & this_name

  make new folder at main_folder with properties {name:temp_name}

  end repeat

end tell

 

 

Please help me.

Posted on Jul 20, 2016 2:27 PM

Close

Q: Apple Script Editor: Adding sub sub folders.

  • All replies
  • Helpful answers

  • by Niel,

    Niel Niel Jul 20, 2016 2:30 PM in response to Kitroyan
    Level 10 (311,916 points)
    Jul 20, 2016 2:30 PM in response to Kitroyan

    Once you've created a subfolder, use code such as:

     

    make new folder at folder "subfolder" of main_folder with properties {name:temp_name}

     

    (143560)

  • by red_menace,

    red_menace red_menace Jul 20, 2016 5:19 PM in response to Kitroyan
    Level 6 (15,526 points)
    Desktops
    Jul 20, 2016 5:19 PM in response to Kitroyan

    If your folder structure has many items, manually creating each one with subfolders in the right spot can be a bit unwieldy.  A general-purpose handler that creates folder structures from a list of items can also be used, for example:

     

    property structure : {"Contract Proposals", "Existing Conditions", {"one", "two"}, "Programming", "Code-Permit", "Design Sketches", "Construction Documentation", "Bidding", "Contract Administration (CA)", "Close-Out", "Product Info", "Photos", "Cost Data", {"three"}, "Communication"}
    
    set j_name to text returned of (display dialog "Enter Job Name:" default answer "job name")
    repeat
      try
        set j_number to text returned of (display dialog "Enter Job Number:" default answer "0")
        if (j_number as integer) < 10000 and ¬
           (j_number as integer) > -1 then exit repeat
      on error errMess number errNum
        if errNum is -128 then error errMess number errNum -- pass 'user canceled' back to script
        display alert "Error" message "Project Number is not a number or is out of range."
      end try
    end repeat
    set j_number to text -5 thru -1 of ("0000" & j_number & " ")
    set structure to {j_number & j_name} & {structure} -- add job folder to previously defined structure
    makeFolderStructure out of structure at (choose folder with prompt "Choose parent folder for structure")
    
    
    to makeFolderStructure out of someItem at someFolder --  make a folder structure
      (*
        someItem defines the structure - nested list items create folders in the previous item:
        {"A", {"B",{"D", "E"}, "C"}} = "B" and "C" are created in "A", "D" and "E" are created in "B"
    
        parameters - someItem [list]:  the folder structure
        someFolder [alias]:            the destination folder
        returns [boolean]:             true if successful, false otherwise
      *)
      set currentParent to someFolder
      if class of someItem is not list then set someItem to {someItem}
      set completed to true -- this will be a result to test if the handler completed
      repeat with anItem in someItem
        if class of anItem is list then -- add sub structure(s) to the current parent
          makeFolderStructure out of anItem at currentParent
        else -- add a single folder to the current parent
          tell application "Finder" to try
            make new folder at someFolder with properties {name:(anItem as text)}
            set currentParent to result as alias -- update potential parent
          on error errMess number errNum
            if errNum is -48 then -- use existing structure
              set currentParent to ((someFolder as text) & (anItem as text)) as alias
            else -- problem with creating structure, so it is not complete
              set completed to false
            end if
          end try
         end if
      end repeat
      return completed
    end makeFolderStructure