{"id":2542,"date":"2013-07-26T01:28:47","date_gmt":"2013-07-26T01:28:47","guid":{"rendered":"http:\/\/www.bartbusschots.ie\/blog\/?p=2542"},"modified":"2016-06-10T21:08:58","modified_gmt":"2016-06-10T21:08:58","slug":"os-x-service-to-calculate-word-count","status":"publish","type":"post","link":"https:\/\/www.bartbusschots.ie\/s\/2013\/07\/26\/os-x-service-to-calculate-word-count\/","title":{"rendered":"OS X Service &#8211; Word Count (and More)"},"content":{"rendered":"<p>Inspired by a <a href=\"http:\/\/www.maccast.com\/2013\/07\/19\/maccast-2013-07-19\/\" target=\"_blank\">recent episode of The Mac Cast<\/a> I decided to see if I could come up with a simple way of getting a word count of a PDF on OS X using only tools that come standard with the OS.<\/p>\n<p>Because of OS X&#8217;s Unix underpinnings, all Macs have access to the Unix <code>wc<\/code> command which calculates word counts on given input. OS X also has a handy built in Terminal command to access the contents of the clipboard (<code>pbpaste<\/code>). This leads to an obvious simple manual solution:<\/p>\n<ol>\n<li>Open the PDF in Preview<\/li>\n<li>Select All Text<\/li>\n<li>Copy to clipboard<\/li>\n<li>Run the Terminal command: <code>pbpaste | wc -w<\/code><\/li>\n<\/ol>\n<p>This is a bit cumbersome though, so I went on to create a simple OS X Service to calculate the word count of any selectable text in any app (the fact that this is even possible, let alone easy, is why I love OS X).<\/p>\n<p>For those of you just looking for a copy of the Service, you can download it here:<\/p>\n<p style=\"text-align:center;\"><a href=\"http:\/\/www.bartbusschots.ie\/blog\/wp-content\/uploads\/2013\/07\/Word-Count1.zip\"><img decoding=\"async\" alt=\"Download\" src=\"\/downloads\/download.png\" style=\"border:0px solid black\" \/><\/a><\/p>\n<p>To install the service simply extract the automator file from the ZIP archive and copy it into either the <code>Library\/Services<\/code> folder in your home directory, or the system-wide service folder <code>\/Library\/Services<\/code>.<\/p>\n<p>Once the Service is installed you can use it in almost any OS X app (specifically in any app written using the standard Cocoa libraries) by selecting some text, right-clicking on it, and selecting the <em>Word Count<\/em> service:<\/p>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.bartbusschots.ie\/blog\/wp-content\/uploads\/2013\/07\/Screen-Shot-2013-07-26-at-02.17.27.png\" alt=\"Right Click Selected Text to Invoke the Service\" style=\"border-width:0px;\" width=\"490\" height=\"246\" \/><\/p>\n<p>When done the results will look something like this:<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"http:\/\/www.bartbusschots.ie\/blog\/wp-content\/uploads\/2013\/07\/Screen-Shot-2013-07-26-at-03.03.55.png\" alt=\"Sample Output\" style=\"border-width:0px;\" width=\"490\" \/><\/p>\n<p>Those of you who want to see how easy this Service was to write, read on and I&#8217;ll walk you through it.<\/p>\n<p><!--more--><\/p>\n<p>The Automator app that comes free with OS X makes it easy to create system-wide services. Lets start by launching that app, and selecting <em>Service<\/em> from the list of possible action types to create.<\/p>\n<p>Using the drop-downs at the top of the workflow, set the input to <em>Service receives <u>selected Text<\/u> in <u>any application<\/u><\/em>.<\/p>\n<p>Next add a <em>Copy to Clipboard<\/em> action into the workflow, and then add a <em>Run Shell Script<\/em> action below that, within the <em>Run Shell Script<\/em> action set the shell to <code>\/bin\/bash<\/code>.<\/p>\n<p>Then paste the following code into the body of the <em>Run Shell Script<\/em> action:<\/p>\n<pre class=\"lang:sh decode:true \" >\r\nword_count=`\/usr\/bin\/pbpaste | \/usr\/bin\/wc -w`\r\n\/usr\/bin\/osascript -e \"tell app \\\"System Events\\\" to display alert \\\"Word Count: $word_count\\\"\"\r\n<\/pre>\n<p>The first line calculates the word count of the contents of the clipboard and saves it to a variable, and the second line uses Apple Script to display the calculated word count in a dialog box.<\/p>\n<p>All you have to do now is save the action and give it a name, then it&#8217;s ready to use!<\/p>\n<p>We can do a little better though &#8211; why only show the word count, why not list the character count and line count as well? OK, lets do that, alter the contents of the <em>Run Shell Script<\/em> to the following and save:<\/p>\n<pre class=\"lang:sh decode:true \" >\r\nchar_count=`\/usr\/bin\/pbpaste | \/usr\/bin\/wc -m`\r\nword_count=`\/usr\/bin\/pbpaste | \/usr\/bin\/wc -w`\r\nline_count=`\/usr\/bin\/pbpaste | \/usr\/bin\/wc -l`\r\n\/usr\/bin\/osascript -e \"tell app \\\"System Events\\\" to display alert \\\"Character Count: $char_count\\nWord Count: $word_count\\nLine Count: $line_count\\\"\"\r\n<\/pre>\n<p>When you&#8217;re done your Automator workflow should look like this:<\/p>\n<p style=\"text-align:center\"><a href=\"http:\/\/www.bartbusschots.ie\/blog\/wp-content\/uploads\/2013\/07\/Screen-Shot-2013-07-26-at-02.06.00.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.bartbusschots.ie\/blog\/wp-content\/uploads\/2013\/07\/Screen-Shot-2013-07-26-at-02.06.00_small.png\" alt=\"Workflow Screenshot (click to enlarge)\" style=\"border-width:0px;\" width=\"480\" height=\"356\" \/><br \/>Click to Enlarge<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inspired by a recent episode of The Mac Cast I decided to see if I could come up with a simple way of getting a word count of a PDF on OS X using only tools that come standard with the OS. Because of OS X&#8217;s Unix underpinnings, all Macs have access to the Unix [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[12,440,16],"tags":[269,26,268,456,457],"series":[],"class_list":["post-2542","post","type-post","status-publish","format-standard","hentry","category-computers-tech","category-tech-projects","category-programming","tag-automator","tag-os-x","tag-scripting","tag-services","tag-software-release"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7t9xK-F0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/2542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/comments?post=2542"}],"version-history":[{"count":9,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/2542\/revisions"}],"predecessor-version":[{"id":13977,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/2542\/revisions\/13977"}],"wp:attachment":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/media?parent=2542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/categories?post=2542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/tags?post=2542"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/series?post=2542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}