Get word count in text and/or textedit?

Shane's icon

This seems super basic, but I can't find anything in the Help or Reference files, and I have searched the forums to no avail...

Is it possible to get the word count (the total number of words) in either the [text] or [textedit] objects? Ideally, I would get this straight from textedit, but I don't see a way.

Surely I am missing something. Any help would be appreciated!

2K's icon

Maybe something like this?

Max Patch
Copy patch and select New From Clipboard in Max.

Shane's icon

So simple. Thanks so much!!!

TFL's icon

Or even simpler: [zl.len]

Max Patch
Copy patch and select New From Clipboard in Max.

2K's icon

Oh yeah, 100% TFL. Dunno why I didn't think about zl.len!

Shane's icon

Thanks so much!

Shane's icon

Oh, wait: what if you have the output mode on [textedit] set to Output as one symbol? It then adds quotes and only counts "1"... I assume I need to convert back from symbol to list, which [fromsymbol] does, but then it will treat each letter as a list item, right?

Shane's icon

Update: sorry, just realized this is not the problem I thought it might be. [fromsymbol] seems to do the job for getting the word count from [textedit].

But I still can't figure out how to get it from [text].

Background: I am using the text object to read preexisting text files and to load them into [textedit].

To get everything added, including text filled with line breaks, I am doing the following:

Max Patch
Copy patch and select New From Clipboard in Max.

2K's icon

Try this

Max Patch
Copy patch and select New From Clipboard in Max.

Shane's icon

It looks like my troubles are coming from the fact that there are quotation marks in the txt files. Whatever is inside gets counted fine when the text first dumps into textedit, but it reads quoted material as a single symbol when I start editing in textedit. So I get different readings...

TFL's icon

It's always tricky to deal with " in Max since they have a specific meaning (delimiting symbols with spaces in them).

So the proper solution to your problem will depend on what you actually want to do with these words, if you want to keep the double quotes or not, if you're fine to replace them by other quotation symbols or not...

Anyway, strictly for counting the number of words regardless of " the initial solution of 2K works fine! The text a bit of "something else" loaded from a .txt into [text] gives a length of 5 which is the desired result:

Max Patch
Copy patch and select New From Clipboard in Max.

(here in the example I populate [text] with a message instead of a file so I need to escape the " by a \ so they get treated as any other symbol)