I’m trying to create a macro that can rename selected files in Finder by adding a - after the 4th and 6th character of the base filename (before the extension).
Could we see what you have for your macro so far or else a list of the overall steps that you think the task could be broken down into? Then it will be clearer where you need help.
I’m sure that someone would gladly offer a complete working macro if it’s needed, but it might not do what you expect it to (besides @ComplexPoint’s question, what other ifs and buts might need to be thought about?) and a “full macro” might cheat you of some useful experience based on your own ideas.
Sure, though I feel the title of the post captures it well: I am intending to add dashes to filenames. The context is dates -- I have a string of files with date listed out (e.g, 20250617) at the start of the file. I'd like to batch select these and be able to quickly convert their names to 2025-06-17.
So, the steps as I would see it, are:
For each item in a collection execute actions (Finder's selection)
Add a '-' before character 5. and a '-' before character 7.
Then the macro as intended would not work, but I am not intending to run this on filenames that don't start with yyyymmdd (why would I?) so I think this is a non-issue?
When a macro does something to files selected in the Finder, sooner or later it will be inadvertently applied – for example to an edge case at the end of the selection, or an anomaly within it, or simply applied twice to the same files by accident.
(Not worth risking unexpected side-effects to files)
Copy - For filenames that are too short, perhaps the macro could detect this and send an error alert?
Obviously, longer filename lengths will vary as I typically have text beyond the yyyymmdd at the start - there is no consistency here. However, an error could also be worked in for a 255 max file name length scenario.
The For each action operates on “the Finder’s selection”, on the assumption that you will want to select the files first, before running the macro. If you had another workflow in mind (e.g. renaming all files in a given folder), you would have to modify the macro.
Notes:
The items making up “the Finder’s selection” are not just the names of files (e.g. “ABCD1234.txt”) but the names of paths (e.g. “/Users/KeyFinder/foo/bar/baz/ABCD1234.txt”.
The Rename file requires a full path to work on, but for the new filename we have to supply it with just that new filename, not a full path.
I have tweaked the regex in the Search and replace action so that it will match everything in the path up to and including the final slash (“/”), followed by the regex to match the filename (which is as detailed previously).
The points that have been made about checking for the lengths of filenames and so on are of course worth considering, but not, in my opinion, until after one has managed to put a basic macro that does the job. This demo macro performs no error checking.
Before running your macro (this one or any other): make sure that everything is backed up; test the macro only on disposable files; try to break the macro with invalid inputs—or, if are confident that you can live with the consequences and that you know what your macro is doing, forget about perfectionism and test-driven development and just wait for something to break and then you can fix it..? It’s your call, according to your needs.
This doesn’t at all match the input example that you gave (that started with letters rather than date numbers).
A slight adjustment to the regular expression, taking the late-mentioned yyyymmdd pattern into account, would allow for leaving the file name unchanged when it already contained hyphens, or was simply too short, etc