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

Nice!

My go at this, unsurprisingly, has the same filter. But I went with passing in a complete path, including file name (IMO, easier to do the split in the sub rather than in every caller). I also changed the logic around -- I don't use the "Until" action enough, and using it here means we don't have to prime the variable before the loop. And I've separated the checks from the writes -- that way, there's only one "Switch/Case" block to update for new file types.

SUB - Write Clipboad and Rewrite on Conflict.kmmacros (9.0 KB)

Image

One obvious improvement would be to return a value to the caller to indicate success (perhaps the final file path used?) or any error (wrong format on clipboard for given extension, broke the version limit).

2 Likes

Yes, this is so much neater - I didn't like having to have two sets of Switch Case and I was sure you'd come up with a way around that :grinning: I've updated my version of your macro to work this new way.

I'm not sure you need to limit to 100 versions - just having three zeros in the format, such as v000 will give way more versions than would ever be needed.

1 Like

Yeah, that was copied over from the previous version.

And that was in there because, in AppleScript, I'd pad numbers by casting to text, prepending some 0s, then taking the last n characters:

set theNum to characters -3 thru -1 of ("000" & 2) as text

...which has the obvious problem of 99 being returned as 99 but 100 as 00, so you need a stop condition to prevent an endless loop once you have 100 versions -- 101 is 01, which you already have, etc.

%CalculateFormat% doesn't have that problem -- %CalculateFormat%101%00% returns 101 -- so the boundary check is redundant.

2 Likes