Mac – Automator: Issue with nested folders

automatormac

Can anybody explain how can I do this:

Add to context menu in finder element nested folders which contains of :
nested folders
– template one
– template two
– template three

and each element will create a nested structure of folder in current directory.

So, if I choose in nested folders->template one, it will create in current directory nested structure of folders with template one.

What I did?

I made new Service for Automator.
Add action 'Create new Folder'

Everything works fine, but I can't understand how to tell Automator to create nested empty directories.
What Automator did instead?
Suppose I choose folder '/pathToFolder/ParentFolder/SourceFolder'.
If I select 'Folder' and hit 'makeNewCustomFolder', Automator would create a 'CustomFolder' like this:

'/pathToFolder/ParentFolder/CustomFolder' – on the same level of 'SourceFolder'.

But also Automator copy content of 'SourceFolder' into 'CustomFolder'

Best Answer

The way to create nested folder structures is probably AppleScript, not Automator. I don't think Automator can create nested folder structures as easily as AppleScript.

The following AppleScript creates a folder structure of the form

- My first folder
  - A folder within my first folder
    - Another folder within both of those

within the frontmost folder in Finder. Here it is:

try
  tell application "Finder" to set FOLDER0 to (folder of the front window as alias)
end try

tell application "Finder"
  set folder1 to make new folder at FOLDER0 with properties {name:"My first folder"}
  set folder2 to make new folder at folder1 with properties {name:"A folder within my first folder"}
  set folder3 to make new folder at folder2 with properties {name:"Another folder within both of those"}
end tell

I've deliberately tried to keep it basic. All the structure is declared in the second tell block, and hopefully it should be easy to see how to extend it. If you want to test if before attaching it to a service, copy and paste it into AppleScript Editor (in the Utilities folder).

This script is fairly naive, and throws an error if the folders already exist. If that's a case you might be working with, then this question on Stack Overflow explains how to modify a script like this and only create folders that don't already exist.

If you want to create lots of folders with similar names (folder001, folder002, ..., folder999), then this forum thread on Apple Insider may be helpful.

You then want to bind this to a service in Automator (which I see from the question you know how to do). The service should receive folders in Finder, and you use the Run AppleScript action to run the script.

Once you've got that service installed, you can right-click on a folder in the Finder, and there'll be a service which creates this structure for you.


To get the different templates, I can think of two methods.

You could create multiple services ("Create nested folders 1", "Create nested folders 2", …). But that seems rather messy. Again, AppleScript is our friend. We can make a dialog that looks like this appear:

enter image description here

and which option we choose dictates which folder structure gets created. Here's the relevant code:

set choice to choose from list {"template1", "template2", "template3"}

try
  tell application "Finder" to set FOLDER0 to (folder of the front window as alias)
end try

if choice is "template1" then
  -- create some folders
end if

if choice is "template2" then
  -- create some different folders
end if

if choice is "template3" then
  -- create a third set of folders
end if

Replace the lines starting with -- with the folder generating code that we discussed above. The list in the first line gives you a list of choices, and then you create an if block for each choice that generates the appropriate folder structure.

If you use this AppleScript in Automator, then the procedure is exactly the same, except now you get the choice when you run the service in Finder.