Hi Folks,
I have a bunch of text files in a folder that I would like to modify.
Currently, the filename is not useful.
I would like to be able to grab the first line of each text file and use that as the new filename title and rename each file with the 1st line.
I also have Hazel if you think that may be helpful...
I am curious how best to:
1 - do this with just one file?
2 - batch process all items in the folder with Keyboard Maestro?
Thanks Dave
I would create a macro that operates on the selected files in the Finder. See:
In Keyboard Maestro, you work with the Finder Selection by using the For Each action to iterate over the selected files. From there, you can do whatever you would like with the files using the various file actions, or by processing with a script or other means. There is a smart action in the acton selector for For Each Path in the Finder Selection, which is simply a For Each action, preset to iterate through the Finder Selection collection. You can put whatever actions you like within that ac…
Use the Read a File action to read the file
Use the Search using Regular Expression action to get the first line.
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.
Then use the Rename a File action to rename the file.
For safety sake, you might want to ensure that the initial file extension is “txt” (hopefully the files already have a .txt extension).
You also might want to limit the length and/or characters of the line before renaming.
And for heavens sake, make sure you have full and complete backups of your entire Mac and this folder specifically before testing the macro.
4 Likes
ALYB
July 28, 2023, 8:13am
3
That's an interesting approach!
1 Like
Thanks so much Peter for you great suggestions!
This is my first attempt at the macro.
My current problem is the try / catch is not working as expected.
I expected the if the file already exists (error) then it would execute the catch which would add the random and timetag unique text and save ok. but it does not - I get the error:
File action failed because destination already exists…
Any Idea how to get this working so the file will save simply and add the unique text only if needed?
Rename-txt-files-using-first-line-of-each-file-mod1-mpu-v001.kmmacros (7.8 KB)
dealtek:
My current problem is the try / catch is not working as expected.
I expected the if the file already exists (error) then it would execute the catch which would add the random and timetag unique text and save ok. but it does not - I get the error:
File action failed because destination already exists…
This is really counter-intuitive, but in order for the Try Catch to work, the failing Action has to be set to "Failure Aborts Macro". The Action to move a file comes in by default with that option unchecked, which is why your Try Catch is not working as it should.
If you use the gear menu to set it like this, it should work:
3 Likes
Wow thanks - THAT WORKS!
I never would have caught that without your help!
Honestly, you would be better starting with some simpler macros and building up experience with the basics of how Keyboard Maestro works.