For each & a Finder selection

My ultimate goal for my macro is to select a number of files in the Finder and for the macro to take each file and append (user inputted text) to each of those filenames. The macro will effectively "rename" all files that have been selected.

I enclose a screenshot of a macro that works correctly. I am selecting only one file as a test, and so the loop (in this example) only operates on one file. The macro successfully displays the name of a file in the "For Each" loop.

I am assuming that each file in such a "For each" loop is an "object" (a file object in this case), and that therefore each filename cannot be amended within a Keyboard Maestro macro.

My intention is to use a shell script which will execute each "rename command".

In shell script:
If I was executing this outside of Keyboard Maestro, I would build a variable named say 'MV_CMD', a variable that would ultimately contain the value, "move $current_filename $new_filename" and then use the shell command, eval "$MV_CMD" which would execute the command contained within the string.

I know that there are "word splitting issues" with variables. Is this approach feasible for Keyboard Maestro?

Is there an easy way to rename files (from a Finder selection) within Keyboard Maestro? Note that I would rather avoid AppleScript.

Thank you for your assistance.

Something like this? I'm not quite sure if you're wanting to still use shell scripting or a purely KM solution. There might even be an easier way that I'm missing, but this is how I would do it.

No, it's a file path -- that's why @hemicyon's suggested macro works.

In general you should avoid eval in shell scripts whenever possible -- it's rather risky. And there's no need here. If you want to use the shell you could do something like this, which appends "-New Name" to every file selected in Finder:

Shell Rename Demo.kmmacros (3.4 KB)

Image

Not that I like to discourage creating macros, but do you know that the Finder can do this on its own? If you select a bunch of files and then choose File ➤ Rename… in the Finder it will let you batch rename the files.

Of course, if you want to automate this process, then a macro will be appropriate.

2 Likes

Meaning calling on variables with Shell? You can have spaces in variables no problem but I am probaby I am missing something.

Thanks for this reminder I completely forgot that the Finder does this. There are several times I could just use the basic features here instead of "A Better Finder Rename 12" The funny thing is even the new name of the app is a reminder that the Finder has a renamer. I do love all the presets and things you can save in that app.

https://www.publicspace.net/ABetterFinderRename/index.html