Creating a series of folders based on user inputs

I've been struggling with this problem for a few hours so I thought I would ask for help.
I'm trying to create a macro to take user entered text and create a series of folders using the text.
I have managed to get it working when using a single set of variables but for the life of me I can't get it working with two sets.
Please can someone take a look and tell me where I'm going wrong?

In the macro 'Job Count 1' is currently working fine and as I want it to.
'Job Count 2' works until it hits the 'Agent 2' variable sets.
This is my first time using KM but I've previously used AppleScript.

Many thanks for any help!

Keyboard Maestro 8.2.1 β€œCapture Folders” Macro

Capture Folders.kmmacros (21 KB)

Hi @c_cooper88,

I haven't downloaded and tested your macro, but could it be that a space is missing in the variable name you use when attempting to create the "Second Agent" folder ? You specify the path %FinderInsertionLocation%/%Variable%Date%/%Variable%Agent2% but earlier on in the macro when requesting user input, the variable name is defined as Agent 2 (with a space):

Here, the left-hand box is the variable (Agent 2) and the right-hand box is the default value. Regardless of what you intended the right-hand box here to have as its contents, it seems that the folder to be created should want to use %Variable%Agent 2% as its name.

Then skimming down, I can see formatting errors of similar ilk, e.g. with %Variable%Address2% instead of %Variable%Address 2%, etc.

Did you consider writing the bulk of this macro in AppleScript and just using that in an Execute an AppleScript action ? It might have been easier and more efficient upon execution too (although I don't know this to be a certainty).

1 Like

Hey, thanks for that. I didn't realise they had to be the same spelling.
It now runs, but still fails when trying to create any combination of the photos, plans or epc subfolders.
I might well look at running it as an AppleScript as I was hoping KM would be easier to manage it, apparently not!

As with most things, it's about choosing the right tools for the job. You can mix-and-match which tools you use at which stage of the macro. It can be a single Execute an AppleScript action that constitutes the entire macro (most of mine are, in fact), but you can also do one bit with AppleScript, the next bit with some proper KM "macro-ing", and a wee shell script to round it off.

Which leads me to one suggestion that eliminates a good chunk of your macro actions with a single line of shell script: since it looks like you're creating a directory tree (one folder, then a folder inside that one, then a folder inside that one), this can be done extremely easily with mkdir -p. As an example, say you wanted to ultimately end up with a folder structure that looked like this:

%FinderInsertionLocation%/Jobs/1/Property Details/2018-06-16/Smith/

where none of these sub-folders presently exist; and inside folder Smith, you would eventually like there to be folders called Photos, Plans, and Shoot; then this is how you would do it using an Execute Shell Script action:

f="$(</dev/stdin)"       # text input = %FinderInsertionLocation%
filepath="$f/Jobs/1/Property Details/$KMVAR_date/$KMVAR_Agent_2"

mkdir -p "$filepath"     # -p ensures all folders in the path are created

cd "$filepath"           # Switch into the $KMVAR_Agent_2 directory, 
                         # inside which we create...
mkdir Photos Plans Shoot # ...these three folders

If it's not implicitly evident, the name of the sub-folder of Property Details is retrieved from a KM variable called "date" (which I had set to 2018-06-16), and the name of sub-folder of this is retrieved ffrom the KM variable "Agent 2" (which I had set to Smith).

It's 5 lines of script in total, but the one line that replaces all of those Create New Folder actions that were giving you so much misery is mkdir -p /Path/of/Folders/To/Create; and the second use of mkdir then creates those three individual folders that all live inside the same directory.

Here's a schematic of the final result:

 β”‚  β”‚
 β”‚  └──1
 β”‚     β”‚
 β”‚     └──Property Details 
 .        β”‚
 .        └──2018-06-16  
                β”œβ”€β”€β”€ Photos
                β”œβ”€β”€β”€ Plans
                └─── Shoot