I recently recently launched a new WordPress plugin for Google's Universal Analytics. Check it out!.
Close

Quick Tip: Get Line Count in nvALT with TextExpander

Quick Tip: Get Line Count in nvALT with TextExpander

Recently at work we’ve been quoting a lot of really large websites. Part of our process is reviewing the current site map and in the case of large websites, type it out. This makes it much easier to identify pages that need custom elements or entire sections that should be converted to a custom post type or possibly even consolidated. I’ve found that nvALT is one of the quickest and easiest ways for me to build these sitemaps but I had been struggling with the process of actually getting a page count. I was copying from nvALT and pasting into Coda2 to check against the number of lines. Not a huge deal, but not perfect either. There had to be an easier way, right?

Finding the fix

I reached out to Brett Terpstra, the creator of nvALT (and many other things) to see if there was any way to do this in the current version. He is crazy busy so much to my surprise he answered within a couple of hours. He let me know that there isn’t a way to do this in nvALT directly but mentioned I may want to look into some utilities.

If you do it more than once, make a TextExpander snippet.

— David Sparks

After Googling for a bit and not finding anything that just jumped out at me I did what any tinkerer would do and built something. Following David Spark’s motto of “If you do it more than once, make a TextExpander snippet for it.” I made a TextExpander snippet for it. I’ve been using it for a couple of weeks now so I thought I would share it with the world.

How it works

For those of you not familiar with TextExpander, it is a utility for Mac and iOS that lets you type keyboard shortcuts that expand into larger bodies of text called snippets.  This snippet is pretty simple. You select the text you want the line count of and copy it to the clipboard, enter the snippet abbreviation (..lines) and it runs a quick little AppleScript to return the number of lines. I also built a second one called Count that gives a bit more info.

Below are the steps I used to create the snippet but if that stuff bores you to death you can always just download the snippets.

Count will give you the character count, word count and line count.
Lines will give you only the line count.

Disclaimer: This is my first AppleScript so sorry if it sucks or is sloppy. I’m sure there are more effective way’s to get this done, but it got the job done for me. Any suggestions are appreciated.

How I did it

Below I’ll breakout the content of the snippet using the Count version. You can scale it back to use just the lines version.

1. Create a new “AppleScript” snippet

Open up TextExpander and press the “New Snippet” plus sign in the top left. So far, so good.
Change TextExpander snippet content type to AppleScript
Now we need to change the content type from the default of “Plain Text” to “Apple Script”. You can also go ahead and give the new snippet a Label and it’s Abbreviation. I always try to get that out of the way before I start adding the snippet content

2. Get the Counts

We start the snippet by declaring our variables and then using the AppleScript command on the contents of the clipboard.

set myCount to count (the clipboard)
set myWords to count words of (the clipboard)
set myLines to count paragraphs of (the clipboard)

Pretty self explanatory but myCount will get the character count, myWords will get the number of words used and myLines will return the number of lines (paragraphs) of the clipboard contents.

3. Return the Clipboard and the Counts

Since we’ve declared the counts, now it’s time to return them. When I first built the snippet I found myself overwriting my selection with the count so I added the clipboard contents back in as part of the return.

return “%clipboard” & “

Characters: “ & myCount & ”
Words: “ & myWords & ”
Lines: " & myLines

In the lines above “return” is the return for our script and %clipboard is a TextExpander variable that outputs the contents of the clipboard.

I then have a blank line to provide some space before providing the counts. Each count has a title and then we return the variable that we defined in the first step.

Insert contents of clipboard into snippet

Download the Snippets

So now that I’ve walked you through the steps you can easily create your own version of this snippet. However, because I’m a nice guy I actually exported these snippets so that you can easily add them to your library.

Conclusion

Though I don’t use these everyday they have become a pretty regular go to snippet. I should also point out that even though I use this mostly in nvALT, it will work anywhere that you can copy text and run TextExpander. I would also like to thank Brett Terpstra for providing the inspiration to do this and to all of the 5by5 hosts who talked about TextExpander for the entire year before I finally bought it.

I hope you found this tutorial and snippet useful. I would love to hear any other suggestions for similar snippets too. Cheers!

2 Responses

  1. Shashi

    Since nobody appreciated your work in the comments, thanks!

Leave a Comment

What's on your mind? Fill out the form below to let us know.