For that I would use Python. Probably just a very few lines of code. Applescript surely would work but I never really got into it. Google probably can help you find some code examples to start with.
I would not know where to start with Keyboard Maestro but others can comment.
I thought I came across a KM macro that would do this, but in a quick search I couldn’t find it. I’ve found the people on the forum to be very helpful, so I’ll bet they’ll come up with a solution for you.
Actually, I might have seen this on the Hazel forums. You could try looking there in case someone’s already written something. I’m pretty sure this will require a script, but maybe not.
I messed up a bunch of PDF names, and used A Better Finder Rename to bail me out… independent dev, great customer service and support… check it out!
It’s coming up to bed-time here, but I thought I would contribute this for you.
Doing this in Python probably is simple and quick, but takes a small bit of care to think what might go wrong.
The psuedo-code, which is almost Python now and probably can be done in other languages like AppleScript, bash scripts, Perl, etc. But I have no time/energy for me to code it now for you.
change to the directory of interest
read the names of all files into a Python List, say "filenames"
loop through the list putting the loop index into a variable, say "orig_filename"
for each orig_filename in filenames
open the file and read the first line of the file
put the line into a variable, say "filename_new"
close the file
with a while loop check if a file with name "filename_new" exists
if yes, append a prefix or suffix to the "filename_new"
keep checking until there a "filename_new" that does not exist already
check that the "filename_new" is valid as a file name and if not fix
have something in there so it doesn't loop forever
rename the file from "orig_filename" to "filename_new"
Or something like that. Check the logic. Probably ought to put in some try/except statements
And, of course, please test on a collection of test files before doing it for real.
Alternatively, you can augment thinking and ask ChatGPT for a shell or python script to do what you want. It is shockingly good in performing such tasks.
I use ChatGPT more and more for getting ideas for solving similar issues.
EDIT: Updated with working macro that should work by grabbing only the first line of each txt file🤞. Thanks to @rms for catching the error.
Here is a KM Macro that will do what I think you want without regex or scripting. It assumes all files in the folder are non-empty txt files, but you could (should) add some error checking to handle files that aren’t txt or txt files that are empty, etc. But this should get you started. Change the path in the first For Each action to the target folder.
Nice, but I don’t see where you read the first line of the content of each text message to get the file name, as the OP @dealtek wants. Can you pls point me to that new-to-me line in the macro. The “read file” command reads only the first line? thanks.
Hi Evan Thanks so Much - right now I am very close to a working version (using your great macro) I will post asap. So feel free to relax and wait until I post it soon…
Hi Evan, I used your very good macro and made some mods with suggestions from PeterLewis and came up with this example (still a work in progress). Peter suggested :
Or you can use the For Each action and the Lines In collection to read the lines from the file, and break out of the loop immediately.