Newbie: Create folders in a path based on dates

Hey Richard,

Ha – Peter beat me to the punch...

This is simple, but it requires a little special knowledge.

The problem you're running into is that a string of directories cannot be created by Keyboard Maestro in one fell swoop.

If the intermediate directories do not exist then Keyboard Maestro will error – e.g. only the LAST directory in the chain can be created – and only if ALL the preceding directories already exist.

The trick to the task is to use the shell's mkdir command which can create intermediate directories (using the -p switch).

We could do this entire task in the shell, but let's use Keyboard Maestro functions for as much of it as we can.

First saving your $HOME-based (tilde) directory string to a variable.

Then expanding the tilde using a filter, which makes it easier to use in the following shell script.

Then a simple shell script that uses a Keyboard Maestro variable.

Voilà!

Here's the shell script one-liner:

mkdir -p $HOME/Pictures/$(date "+%Y/%m/%Y%m%d")

Ah, I wasn't paying attention when I cut-and-pasted the path string. You only need to employ ICUDateTime once:

~/Pictures/%ICUDateTime%yyyy/MM/yyyyMMd%

* AppleScript-Objective-C is also capable of making intermediate directories, although vanilla AppleScript cannot.

-Chris


Set Variable var to Text.kmmacros (2.5 KB)

1 Like