"Create Unique File" Action in Keyboard Maestro v10.0.1

From what I understand, "Create Unique" will create a file based on the path you provide and change the file name in case a file already exists at the specified path.

It seems like a useful addition, particularly because the self-generated path is automatically added to a Variable. However the file you create will be either empty (.txt, .rtf, etc.) or unusable (.jpg, .mp4).

So, in the end, all you can do is to use the path added to the variable, trash the file, and create another one.

Two questions:

  1. Is that the only/best usage, at this time, or am I missing something?
  2. If a file already exists, the unique file will be created by adding a space, the suffix "copy" and a number at the very end of the path (i.e., AFTER the extension). This way the file extension is invalidated, preventing the Finder from identifying the type of file. Wouldn't it be a better option add the suffix after the base name (i.e., before the extension?)

Fair question. I confirm your description of the behaviour of the action embedded in your second question.

I would have expected that KM would have behaved exactly like macOS when you duplicate a file. MacOS creates a unique name there by placing "copy #" before the file type.

I was puzzled why anyone would want to use that new feature since I've already been creating files with unique names by using the token %RandomUUID% as the base name for the file. So I'm not sure if I agree with your assertion that you have to go through several steps to create a file with a unique name, because I do it with only one step.

Hey Alex,

Nyet. Keyboard Maestro can write asogs¹ to a file:



¹ All Sorts of Good Shi* (er Stuff)...

@peternlewis – I agree with Sleepy – operating in the same way as the Finder would provide the most consistent user-experience.


Yes, that is the correct way to use it - it creates a new file with a new unique name, and then you can write to that file by some other means knowing it will be unique.

Yes, the “copy n” should be before the extension, I will look at getting that fixed.

1 Like

This is done for 10.0.2.


Is there a way to get the Write to a File action to create unique files too? Would be super handy if subsequent files with the same name were numbered.

If that feature existed, wouldn't it have the same meaning as this:

1 Like

That's nifty! So the first action looks at the directory to see how it should name a file and the second actually creates it. I think I get it now. Thanks!

EDIT: Actually no, that's not what I was talking about. This copies an existing file and adds "copy 1" to the filename.

I'm talking about the Write to a File action which can actually generate a new file from a clipboard. I mistakenly thought there might be a way, when creating a new file in this way, that it could look at the destination directory, see if there's already a file with the same name and, if so, add a number to the new file's name so it doesn't overwrite the existing one.

I empathize. I simply wanted to point out the equivalence in case others didn't know about it. And to make sure I understood your request.

FOOTNOTE: I didn't want to add this footnote, but the web server didn't let me post the exact same text in a separate thread. That's so sad, because I thought it was hilarious that I could write two identical posts in two separate threads and both of them be meaningful and correct.

1 Like

This can be done, but you have to script it – or use a loop in a Keyboard Maestro macro to try to rename your unique file to the desired name and increment if there is a failure due to a collision.


1 Like

15 posts were split to a new topic: Creating a Uniquely Named Folder

I'm wondering if @Nige_S's method here could be adapted to do this?


It could, but you only need to do that if you're unhappy with how KM/the OS handles duplicate names, i.e. test.html, test copy 1.html, test copy 2.html, etc -- those come "free" with the "Create Unique File" action.

You'll need to either edit "Create Unique"'s path or make a folder on your Desktop call "fileTestFolder" for this example:

Unique File Test.kmmacros (3.1 KB)


I'd start with why these duplicates are being generated, whether you need to keep the previous "version", the most appropriate naming convention, and so on -- for example, it might be all-round better to always generate the file names with appended time stamps for both uniqueness and the instantly available metadata.


Thanks. This is great. And the reason "why" I wanted to do this is that sometimes I want to overwrite a file and sometimes I want to keep versions (depending what option I choose in a Prompt). I had already been using the timestamp idea but this seems neater.

It might be worth mentioning that your example Macro only needs these three Actions to make unique versions of a Write System Clipboard to File Action. I do find this "Create Unique" option quite confusing, but you have clarified it for me with your example.

1 Like

I did the extras so it would loop and put different data into each "duplicate" file name so it was obvious that each was, indeed, different, and to check that each loop would skip over any previously-appended file names to the next available number.

For my sake as much as anyone else's -- appending "Copy n" to the file name implies (to me, at least) that it's a copy of the original, and I wanted to prove to myself that it wasn't!

1 Like

Yes, that's true. So, maybe this isn't such a great way to indicate version numbers... That's why I was wondering if your other method (-001 -002 etc) could be adapted to the Write System Clipboard to File Action

"Write to file" doesn't (AFAIK) error out if there's already a file. So we're back to test-then-write. I'd prefer to have "v01" etc to show these are deliberate versions, because the folks at work do a lot of image sequence exports and similar which get the "-001" treatment. So:

Write Clipboard with Rename on Conflict.kmmacros (6.2 KB)



Thank you. That works really well. I was able make it into a Subroutine to use in my Macros.

And editing the three occurrences of v00 in the Actions to another format such as -000 or Copy-00 allows any number format to be used (as in your original Macro on the other thread).

Next move would be to pass the formatting string in as a parameter :wink:

I think you could do that by double-%ing the rest of the line, Filtering with "Process tokens", then using the result in the test/write actions, but I didn't get that far. Maybe one for after-dark...

Unless you're already there?

Yes, I've got that to work by doing exactly what you suggested. In my Subroutine the Filter Action looks like this (which is a lot of %s !)

And the Parameters in the Subroutine are:

LOCAL__Folder is the path to the containing folder
LOCAL__File is the filename to be used for the Save Clipboard to File
LOCAL__Extension is the extension such as png or txt or kmmacros
LOCAL__NumberFormat is the number format such as -000 or (000) or v000 or version000 etc, written as it will appear after the filename, with the number of digits being shown by the number of zeros.

I also had to add in a Switch Case depending on the file extension for the format the file is saved in.

Uploaded here for info and to be adapted for other file types etc.

Click to Show Image of Macro

Write Clipboard with Rename on Conflict @Nige_S (Subroutine).kmmacros (7.4 KB)