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:
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.
Best Answer
The fact that you want to compare the contents of, I'm assuming, a single folder on the SD card against another folder on the, e.g., Macintosh HD containing an entirely different hierarchical directory structure ... IMO a shell script is the better way to go.
The example shell script code, shown below, get saved as an executable and placed within the
PATH
passed to the shell in, e.g., Terminal, and does the following:In Terminal, assumes
pwd
is your Home directory, run the following compound command:In the opened sdcheck document, copy and paste the example shell script code, shown below, into the document. Modify the value of
dupdir
variable to an existing local folder containing the duplicates from the SD Card. Then save and close the document.sdcheck
file to a location that's within thePATH
passed to shell scripts, then you'd not have to preface the command with./
, or include it's fully qualified pathname.Back in Terminal:
Example test:
Example shell script code:
Using the example shell script code in Automator
To adapt the example shell script code for use in an Automator workflow, application and or Service/Quick Action, it needs to be modified with some additional code added, and is done so below.
The example Automator workflow uses AppleScript to choose the target folders, the SD Card and the local folder to be compared.
The example shell script code and example AppleScript code, shown below, was tested in Automator under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
The Automator workflow outline:
theSDCardMediaFolder
theLocalMediaFolder
theSDCardMediaFolder
theLocalMediaFolder
The Automator actions, code, and settings:
In the first Run AppleScript action, replace the default code with the following example AppleScript code:
In the first Set Value of Variable action, set the value the variable to:
theSDCardMediaFolder
In the second Run AppleScript action, replace the default code with the following example AppleScript code:
In the second Set Value of Variable action, set the value the variable to:
theLocalMediaFolder
In the first Get Value of Variable action:
theSDCardMediaFolder
In the second Get Value of Variable action:
theLocalMediaFolder
In the Run Shell Script action:
Replace the default code with the following modified example shell script code:
Notes:
For the use in Automator the workflow was saved as an application.
When the Automator workflow finishes, it opens the logfile in TextEdit for one's review. Here is a sample of what it could look like:
I did not have an SD Card handy to review its hierarchical directory structure and as such I use a DMG image mounted and pathed as shown in the logfile output above.
The testing set of images was a very small sampling, nonetheless the code logic works as expedited as coded.
In the example Automator workflow image below, there is an obvious disconnect in the center between some of the actions. This is wanted and due to: Options -- [√] Ignore this action's input
Note: The example shell script code and example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.
For AppleScript error handling... Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g.
delay 0.5
, with the value of the delay set appropriately.