2

I would like an AppleScript .scpt file to determine the character count of any given .rtf file. I'm not sure if this is standard for character counts, but I would like each space character (" ") to also count as one character.

I would like the script to accomplish this, without actually opening the file in TextEdit.

It is important to note that this is a rich text file, not a simple plain text (.txt) file. This fact complicates things. It is not as simple as chopping off the rich text header, because a single .rtf file may contain multiple different types of rich text formatting.

What is the best way to go about this?

2

You asked, "What is the best way to go about this?" and while I'll not make that determination, nonetheless, here's an example using the do shell script command and some command line utilities.

set theRichTextFile to quoted form of "/path/to/filename.rtf"
set theCharacterCount to do shell script "textutil -stdout -convert txt " & theRichTextFile & " | LANG=en_US.UTF-8 wc -m | sed 's/ //g'"

Note that theRichTextFile must be a quoted POSIX pathname.

Update: Per comment by @jackjr300, added LANG=en_US.UTF-8 to the command.

  • 2
    Note : The Locale is not defined in any shell when you use the do shell script command, So, the wc command count all ASCII character in any composed character. Example : the RTF file contains "Здравствуйте!" (a Russian word), the wc command return 25 (ASCII characters). To get the correct number, set the LANG to some lang.. with the UTF-8, like this : do shell script "textutil -stdout -convert txt '/path/of/someFile.rtf' | LANG=en_US.UTF-8 wc -m | sed 's/ //g'" --> now, the wc command return 13 (characters) – jackjr300 Aug 7 '17 at 18:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .