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

MacRumors

macrumors bot
Original poster
Apr 12, 2001
63,547
30,864


In this article, we're going to show you how to create a system-wide service that enables you to get an immediate word count and character count in TextEdit, Safari, Mail, or indeed any app on your Mac that lets you highlight text. It offers a handy home-made solution if you often find yourself pasting into a blank Word or Pages document for no other reason than to find out how many words are in a text selection.

word-count-automator-service-2.jpg

The steps below will guide you through the process of creating your own word count service using the macOS Automator app. It's a simple procedure to follow and should only take five minutes, but if you'd rather use a third-party solution, then check out WordCounter, a free Mac menu bar utility from Onekerato.


Click here to read more...

Article Link: How to Set Up a System-Wide Word Count Service on Your Mac
 
Last edited:

H2SO4

macrumors 603
Nov 4, 2008
5,651
6,937
Srsly guys... I don't think I'll need this. But you rock! Keep these guides and tutorials coming!!! They are MUCH appreciated!
Was gonna say exactly this till I saw your post. Reminds me of MacOXSHints.
[doublepost=1526030048][/doublepost]
Or get WordService for free from the App Store to give you text stats and so much more:
https://itunes.apple.com/app/wordservice/id899972312?mt=12
Maybe but this also teaches you stuff.
 

jmh600cbr

macrumors 65816
Feb 14, 2012
1,031
2,496
Teach us how to write a script that checks the size of the trash and downloads folder and when it exceeds a certain size it empties
 

cgriffis753

macrumors regular
Oct 12, 2010
144
243
Michigan
Mine Just Keeps showing 0. what am I doing Wrong?

Edit: I fixed it. for some reason the copy paste was breaking it. When I typed the script it worked fine
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,561
6,059
Interesting. Writing a service like this seems like it might be easier than writing a Sublime plugin which is what I normally do when I want to automate a common text related activity.

Although sublime plugins are cross platform whereas services would only work on a Mac.
 

stop-hammertime

macrumors newbie
May 11, 2018
3
3
This seems like a terrible idea because I don't believe input into the shell script is sanitized. A malicious string, perhaps something like "lol ; evilcommand" could cause "evilcommand" to be executed, enough to recursively delete everything in your home directory.
 
  • Like
Reactions: JKAussieSkater

KazKam

macrumors 6502
Oct 25, 2011
496
1,687
Interesting, but all the text and code editors I use these days already have word and character counters built-in. Also, as stop-hammertime mentioned, if terminal commands aren't filtered, this script could execute code with unintended, or even dire consequences.
 
  • Like
Reactions: JKAussieSkater

MauiPa

macrumors 68040
Apr 18, 2018
3,429
5,080
Interesting, but all the text and code editors I use these days already have word and character counters built-in. Also, as stop-hammertime mentioned, if terminal commands aren't filtered, this script could execute code with unintended, or even dire consequences.

Padawan, perhaps it is the journey and not the destination.
 
  • Like
Reactions: JKAussieSkater

ignatius345

macrumors 604
Aug 20, 2015
6,865
11,206
Interesting. Writing a service like this seems like it might be easier than writing a Sublime plugin which is what I normally do when I want to automate a common text related activity.

Although sublime plugins are cross platform whereas services would only work on a Mac.
There are definitely existing Sublime plugins for word count. I'm using this one and it's great.

iMac 2018-05-11 at 12.37.18 PM.png
 
  • Like
Reactions: lec0rsaire

IJ Reilly

macrumors P6
Jul 16, 2002
17,909
1,496
Palookaville
Or get WordService for free from the App Store to give you text stats and so much more:
https://itunes.apple.com/app/wordservice/id899972312?mt=12

Beat me to it. I've used this indispensable collection of services for over 15 years. Can also be downloaded here:

https://www.macupdate.com/app/mac/10523/wordservice
[doublepost=1526061714][/doublepost]
Maybe but this also teaches you stuff.

I learned that I don't have to do it myself. ;)
 

Wanted797

macrumors 68000
Oct 28, 2011
1,715
3,591
Australia
Automater is a awesome app.

I used to use it for photos. I’d shoot in raw so the photos wouldn’t be ready to view straight away.

I built to quick conversion tool so that I could easily bulk convert a folder of raw files. Really handy to quickly view or if I want to give them to someone else.
 

Manzanito

macrumors 65816
Apr 9, 2010
1,073
1,743
Gotta love all these useful articles macrumors is publishing lately. Keep up the good work!

Cheers!
 

lec0rsaire

macrumors 68000
Feb 23, 2017
1,525
1,450
Once again this is very useful. Automator and AppleScript are powerful tools. While both were really essential in the past, macOS has evolved to the point where many common tasks (renaming a batch of files, etc.) can now be done quicker through native apps and the OS itself. Even though Apple has neglected both in recent years, there are so many creative solutions that can be found to make life a little bit easier. This is one of the reasons why I consider macOS to be superior to Windows. Automator, AppleScript and the Terminal let users have much more control over one’s machine. It’s well worth the time required to master all of them.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,561
6,059
There are definitely existing Sublime plugins for word count. I'm using this one and it's great.

View attachment 761434

Of course. There's plenty of plugins for Sublime for anything lots of people need to do.

I was saying that, more generally, I write plugins for Sublime when I regularly need to do something with text in some specific way. As opposed to writing a MacOS service, where it'll only work on Macs.
 
  • Like
Reactions: ignatius345

ignatius345

macrumors 604
Aug 20, 2015
6,865
11,206
Of course. There's plenty of plugins for Sublime for anything lots of people need to do.

I was saying that, more generally, I write plugins for Sublime when I regularly need to do something with text in some specific way. As opposed to writing a MacOS service, where it'll only work on Macs.
I would too, if I knew how to! I'm just a lowly writer who uses Sublime as a great writing environment. :)
[doublepost=1526309715][/doublepost]
Thanks! This was a big reason why I didn't use TextEdit for my writing. Problem solved now!
I gotta say, Pages is pretty great (and free) and has a live word count. Only big drawback is that it locks you into .pages file format.
 

SirichandKK

macrumors newbie
Sep 13, 2018
1
0
Hey there.. thanks.. this would be perfect. i followed these directions exactly as best i could tell and no count
was given with the selected text.
 

Attachments

  • screenshot.png
    screenshot.png
    153.5 KB · Views: 192

leeuk321

macrumors regular
Jul 21, 2018
166
141
Has anyone actually checked how exact this is? I'm testing it now and the "characters including spaces" count seems to always be one more than it should be, regardless of the text or the program that I'm using the action in (e.g. safari, textedit, etc). Can anyone confirm if it's working 100% properly or not on their mac?

I know it's not a 'huge deal', and I can just manually deduct 1 myself, but it'd be interesting to know if anyone else is experiencing this and if so, if there's a way to get it exact. The exact character count is what I'm using this function for, because I use sites which limit titles & descriptions to exact character counts; and some sites, for some reason, don't actually tell you how many characters you've entered, just that you've "exceeded the maximum number of characters".

Thanks
 
Last edited:
  • Like
Reactions: JKAussieSkater

JKAussieSkater

macrumors 6502
Mar 13, 2009
263
392
Tokyo, Japan
Clear the text in the shell script box (highlight the text with your mouse cursor and hit Backspace) then copy and paste the following text into the same area:

Bash:
echo Words:
echo $1 | wc -w
echo Characters including spaces:
echo $1 | wc -c

I love the simplicity here! However, the `wc` command has a longstanding bizarre quirk since the birth of UNIX where it pads its result with spaces and a newline. This is both ugly and incompatible with inline output (the result must occupy its own line independently). Additionally, the characters code is also afflicted with an incorrect output; it returns the number of characters + 1.

I would like to suggest the following code instead, which compensates for these anomalies and gives the reader more flexibility/choice about using inline results or newline results:

Bash:
echo $(echo $1 | wc -w) words
echo $[$(echo $1|tr -d ' '|wc -c)-1] "characters (no spaces)"
echo $[$(echo $1 | wc -c)-1] "characters (with spaces)"

## Output for "Hello World!" ##
# 2 words
# 11 characters (no spaces)
# 12 characters (with spaces)

Now regarding the accuracy, the user was inquiring on if anybody had done any testing:

Has anyone actually checked how exact this is? I'm testing it now and the "characters including spaces" count seems to always be one more than it should be… Can anyone confirm if it's working 100% properly or not on their mac?

I know it's not a 'huge deal', and I can just manually deduct 1 myself, but it'd be interesting to know if anyone else is experiencing this and if so, if there's a way to get it exact.

Yes, indeed, the `wc` command is off by 1. See the code I've posted for the correction to resolve this.
However, I have done some other testing, and the results vary. For example, with CJK characters are assumed to be 3 characters each (after compensating for the +1 bug); The Japanese word "好き" is considered to be 6 characters instead of 2. So be cautious.

This seems like a terrible idea because I don't believe input into the shell script is sanitized. A malicious string, perhaps something like "lol ; evilcommand" could cause "evilcommand" to be executed, enough to recursively delete everything in your home directory.

I think you might be right; raw unsanitised text could exploit a BASH shell script vulnerability. I'm not sure how your post was overlooked by so many other readers/commenters. I tried to do some "evil commands" without success, so perhaps Automator itself sanitises the text. Somebody should investigate this further!

But for now, people probably aren't going to character-count code, so even if this potential vulnerability does exist, it's unlikely to eventuate in my opinion.

===========
BONUS CODE!

===========

So, maybe you're a little particular about the final output dialogue not having text in the "Title Bar", and having two buttons "[Cancel] [OK]" when only one is necessary. Instead of using Automator's "Ask for Confirmation" dialogue, you could use the following Applescript instead:

AppleScript:
on run {lstInput}
    
    ## input is a "list" variable type, so you need to convert it to a string:
    set {text item delimiters, TID} to {"
", text item delimiters} ## This ugly line break is a literal new line character, needed as the delimiter
    set {text item delimiters, strInput} to {TID, lstInput as text}
    
    display dialog strInput with title "Word Count" with icon note buttons {"OK"} default button 1
    
end run
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.