Hi guys.
The main problem here may have more to do with text content, rather than with any particular scripting approach.
For example, text copied from a web page like this one, and then pasted into TextEdit, might look innocuous enough. But without manually retyping it (or crucial parts of it) in TextEdit, I get similar results to kel from his example - and Adam’s routine returns {1} instead of his {3, 7}.
Don’t you just love a mystery?
Looking at this a little more closely, I suspect the confusion is probably caused by the use of line separators instead of line endings.
While line separators may have a similar appearance to paragraph separators (LF/CR), they’re not treated as equivalent. They don’t, for instance, feature in AppleScript’s paragraph delimiters. So if they’re the only separator in town, the text will be considered to contain only a single paragraph (consisting of the entire text):
tell application "TextEdit" to count paragraphs of front document --> 1
Because of this, a conditional filter for matching paragraphs will generally return an empty list (unless, that is, the entire text matches the condition). So, having copied kel’s original text to TextEdit, I get this result:
tell application "TextEdit" to paragraphs of front document whose second word is "quick" --> {}
An attempt to isolate a single paragraph that meets the same condition will therefore not return a result - because no match exists:
tell application "TextEdit" to first paragraph of front document whose second word is "quick" --> [no result]
Of course, if we change the condition so that “rain” is the required match, we’ll get a result from this example - because that’s the second word of the entire text:
tell application "TextEdit" to first paragraph of front document whose second word is "rain" --> "The rain in Spain [etc...]
How can we get around this?
The most effective approach might be to replace every line separator with a line ending (since that’s what we’re expecting anyway). We should then be able to carry out any further operations in TextEdit as normal:
tell application "TextEdit" to tell front document
set characters where it is «data utxt2028» to return
first paragraph whose second word is "quick"
end tell
--> "The quick brown fox."