Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jeromie

macrumors member
Original poster
Jan 28, 2005
50
0
I wrote a short script to do a word count in TextEdit, and I found a curious bug. (I know there are existing applications out there. I was trying to update the AppleScript included with NanoCount to be simpler and work with Leopard and learn AppleScript while I was at it.) I'd like someone to tell me if there's something I'm missing, or if this is just something I need to send into a bug report.

The basic structure of the AppleScript looked like this:
Code:
if application "TextEdit" is running then
	tell application "TextEdit"
		count words of the front document
	end tell
end if

There are a couple of syntax variations that work just as well, but this one gets the job done. Unless I have a number followed immediately by a period or a comma.

This sentence will count as 9 words:
I have turned 10 and the day is glorious.

And so will this one:
I have turned ten, and the day is glorious.

But this one will count as 4 (it stops counting words when it gets to 10, )
Today I turned 10, and the day is glorious.

(Please ignore the lameness of my example sentences.)

A number by itself doesn't disrupt the counting, and other punctuation like a semicolon or exclamation point doesn't cause the odd behavior.

Has anyone seen anything like this before? Is there a good workaround other than handing the work off to a unix command and returning the results? I just found this behavior totally bizarre.
 
I wrote a short script to do a word count in TextEdit, and I found a curious bug. (I know there are existing applications out there. I was trying to update the AppleScript included with NanoCount to be simpler and work with Leopard and learn AppleScript while I was at it.) I'd like someone to tell me if there's something I'm missing, or if this is just something I need to send into a bug report.

The basic structure of the AppleScript looked like this:
Code:
if application "TextEdit" is running then
	tell application "TextEdit"
		count words of the front document
	end tell
end if

There are a couple of syntax variations that work just as well, but this one gets the job done. Unless I have a number followed immediately by a period or a comma.

This sentence will count as 9 words:
I have turned 10 and the day is glorious.

And so will this one:
I have turned ten, and the day is glorious.

But this one will count as 4 (it stops counting words when it gets to 10, )
Today I turned 10, and the day is glorious.

(Please ignore the lameness of my example sentences.)

A number by itself doesn't disrupt the counting, and other punctuation like a semicolon or exclamation point doesn't cause the odd behavior.

Has anyone seen anything like this before? Is there a good workaround other than handing the work off to a unix command and returning the results? I just found this behavior totally bizarre.

Send a bug report.

'Today I turned 10,000 and the day is glorious.' returns 9.

While, 'Today I turned 10. and the day is glorious.' returns 4.


The problem is with whatever it is using to parse text sees the , or the . and thinks it's still in the number.

AppleScript gets it right so you could say.

Code:
if application "TextEdit" is running then
	tell application "TextEdit"
		set theWords to text of the front document
	end tell
end if
count theWords

The proper shell command is wc

$wc
Today I turned 10, and the day is glorious.
1 9 44

Lines Words Letters
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.