How to insert text (from prompt user input) into filenames of selected Finder files?

How can I insert text (from prompt user input) into the filenames of any selected Finder files? If possible I'll have one macro to insert the text after the filename and another to insert the text before the filename. Very noob with KM variables here :slightly_smiling_face:

Here you go! :slight_smile:

Prepend-Append File Names.kmmacros (22 KB)

Macro screenshot

@noisneil wrote exactly what I would have written, but he types faster than I do :).

My version would have been identical with two exceptions: The small difference is that I would have assumed you want to add spaces to your before and after text to set it apart from the filename, but probably wouldn't do that by typing it in the input box, so my version adds a space where needed when building the filename.

The larger differnce is that I would wrap the Prompt for User Input action in an Until action:

Until.kmactions (2.1 KB)

Keyboard Maestro Export

By doing this, you won't accidentally try to put a path marker in your filenames, which isn't allowed. Technically, the ":" is allowed, but it's converted to a "/", and that may cause issues for things that don't expect slashes in filenames, so I trap for both characters.

This may not be critical in this particular case, but it's a good example of how you can use Keyboard Maestro's Until action to make sure the input is clean before proceeding to the next step of a macro. You can do all sorts of things with it, like making sure numbers only contain digits, or that they're within a certain range, or that a string starts with a certain word, etc.

-rob.

1 Like

I might type faster but you type better!

I like the Until addition. Very smart. I did consider the spaces, but erred on the side of allowing for names such as:

Filename.txt

to

Version 1-Filename-ToDo.txt

This is edge case stuff though, so I think your approach is best.

Hehe, I considered the same thing and thought "nah, it'd be spaces." Rethinking it, your approach would be better, as it gives the user more choice—you just pass along whatever they include.

We could get really fancy and check if the first/last character of the variables is a letter or not, and if it is, then insert the required space. But that's a lot of work to do what your version lets them do anyway.

-rob.

1 Like

Here's a version with your Until block in it and the space condition (not that much work, as it turns out). Teamwork! :raised_hands:t3:

Prepend-Append File Names.kmmacros (27 KB)

Macro screenshot

2 Likes

Aghhh you guys are the best, thank you so much!!! Worked as expected :+1:

Just one tiny thing: if I'm only appending the file name, it adds a space before the basename (which I'm assuming has something to do with the prepending in the "Set Up Spaces" group). Would there be a way to make sure KM doesn't prepend at all if there is no prepend value in the input box? Thank you again!

Ah yes. Try this one instead...

Prepend-Append File Names.kmmacros (29 KB)

Macro screenshot

1 Like

Perfect, that worked flawlessly!!! Thank you!

1 Like