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.

About Nima

Hi, my name is Nima Yousefi and this is my frickin’ sweet website. I’m not an expert in any particular field, however I do own a computer and an Internet connection, and therefore naturally assume that my random thoughts and opinions are important and that the lives of everyone on Earth would be improved dramatically by reading those random thoughts and opinions.
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

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

  1. Andrés says:

    That was an unnecessary note on Paragraph counts there. :)

  2. Pete says:

    Just a guess, but I’d imagine the reason it overestimates paragraphs is that a lot of people hit enter twice to start a new paragraph, and it is probably counting the number of line breaks as paragraphs, so it would overestimate by a factor of two. You probably have to iterate over the paragraphs and exclude the empty ones to get the correct number. One day I might try that! But your script does everything I need already, and just a little bit more!

  3. Like all things bad, this trend caught on pretty quick and very soon many websites were trying to find the easy way to the top. The whole piece they write may amount to little more than a contrived pile of keywords and keyword phrases.

    If used in another way, it might result in the site being removed from the index for a time.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>