A Slightly Better Systemwide ‘Word Count’ Service for OS X

I previously wrote about and praised John Gruber’s Word Count script, which uses Jesper’s ThisService to add a systemwide service to Mac OS X.

I have one minor quibble with it, however. While a word count is invaluable for prose, there times in computer-related writing when you actually might want a character count as well or instead, which the script doesn’t provide. Such was the case when I wrote the UCSD redesign post, where I wanted to character counts of the URLs.

Fortunately, AppleScript is pretty easy to hack, and even though I have no idea how to write AppleScripts, hacking John’s script to include a character count proved painless. The new script is:

on process(_str)
    tell application "System Events"
        set _appname to name of first process whose frontmost is true
    end tell
    tell application _appname
        display alert "Word count: " & (count words of _str) & "
Character count: " & (count characters of _str)
    end tell
end process

Seriously, count characters of _str counts the characters in the string. I have to hand it to AppleScript, that’s cool beans.

Just reload the script into ThisService and you’ll have a slightly better systemwide word count service.

Counting Paragraphs

Coming off of the god-like high of manipulating the code to do my bidding, my mind naturally gravitated towards giving the script even more power. Counting paragraphs seemed like something the script could use, even though, really, I can’t think of a reason why I would want to ever count paragraphs. But, hey, that sort of thinking never stopped Microsoft from dumping features into it’s products, so I added a count paragraphs in _str part to the code and tested it.

Turns out, no, I’m not a god, and apparently AppleScript becomes retarded when you ask it to count paragraphs. It works fine for counting the paragraphs in a single paragraph block, but anything more than that and it overestimates the number. Again, I don’t know AppleScript — or even paragraphs — well enough to troubleshoot this, and I don’t really care enough to try writing a Ruby or Perl script to add a feature I don’t actually want. If, however, you do, knock yourself out.

Tags: ,

Leave a Reply