Change font in all documents in a folder

I have about 30 folders of RTFD documents. For the most part, the font in the documents is Times New Roman. I'd like to automate a way to go thru the documents in a folder and change the font on each document to Arial while maintaining things like Bold, Italic, color, etc. Is there a way to do this via KM or KM with a script?

If this is a one off change, then Keyboard Maestro is probably not the right tool for the job.

The very first thing I would do would be to make a full backup of the entire folder.

Then I would make sure my entire Mac was also backed up and cloned.

Any time you are working on recursive changes to a folder, it is way to easy for things to go wrong.

Then you could probably do something in the Terminal like:

cd /whereever/the/folder/is
find . -type f -name '*.rtf' -exec sed -e 's/Times New Roman/Arial/g' -i {} \;

But I'm not entirely sure that the font names will be exactly that, so I would probably make a duplicate of one of the rtfd documents somewhere else, look at the files within it with BBEdit and verify that the font name is exactly “Times New Roman” and then make a change in an editor to the document to change it to Arial, and then verify with BBEdit again that the font name is exactly “Arial”.

Anyway, that is the approach that I would use, but I have no idea whether it will work - so absolutely make a backup of the folder and of your Mac before trying any of this!

Thanks, Peter. I’ll have a go as suggested in report back. Appreciate your help.

Separating the hairy business of changing all files in a folder from the particular challenge of RTFD font changes in a document, I think you should be able to use textutil for the latter.

1 Like

textutil font changes seemingly only work to change plain text to formatted (RTF) text. At least that’s what I gather from the MAN page.

Just an fyi and anyone else who might be interested in this. The font names (I opened one of the files in BBEdit as suggested) are the Postscript name found in FontBook. So, for instance, Arial is actually ArialMT. Times New Roman is TimesNewRomanPSMT.

1 Like

I really appreciate the help. Where this won't work is that in any given document, there could be Regular or Bold text of the same font. So, as it stands, the sed will only operate on one or the other but not both. I guess I could run the sed against both, separately?

Is that right ? (I haven't tested, and experiment is, of course, the only source of knowledge)

From the manual:

fmt is one of: txt, html, rtf, rtfd, doc, docx, wordml, odt, or webarchive

Right. fmt is for changing the format of the file from any of those listed under fmt to any other. I've actually used it for that. But the font option is
-font font Specify the name of the font to be used for converting plain to rich text.

Why is that? What does the RTF look like for the two cases?

You can’t specify just Arial. It has to be ArialMT for Regular or Arial-BoldMT for Bold.

Just run the command multiple times with the different translations.

Or if it is entirely consistent, then you could translate

Arial(.*?)MT

to

TimesNewRomanPS$1MT

$1 might be \1, I'm not sure for sed (and can never remember in general which one it is).

Is the dot (.) literal or just part of the syntax? The names vary with a hyphen (-). I think multiple times would be better. Will have to try and report.

See the ICU Regular Expression reference linked from the Hel menu.

The meaning of the characters in Arial(.*?)MT are:

  • Arial - explicit text
  • ( capture group start
  • . any character except line ending characters (any character at all if the (?s) flag is set)
  • * match zero or more characters
  • ? in *? match non-greedy. That is, match the minimum number of characters possible
  • ) capture group end
  • MT - explicit text

Thanks. I will want to visit the ICU Regular Expressions reference more often.

1 Like