4

I'm ultimately looking to create a service that will allow me to count all selected non-whitespace characters. To get there though, I'm looking for a way to remove all whitespace characters from a string in a service that I can then use for other such purposes. For example, I mainly want to use it to get quick DNA coordinates while looking at output in Terminal.app, but I can think of other uses, like counting code characters.

I have found applescripts to remove spaces, but nothing I've found appears to account for tabs, newline characters, carriage returns, or any other strange or weird character that doesn't display.

Note: I don't want to create a file or use or change the clipboard.

Side-question: Is there a way to display the result (briefly) in a bezel, like the way you can display phone numbers from the Contacts app?

  • “not create a file”—not even a temporary one? I frequently put intermediate results in /tmp/tmp knowing that I will either soon overwrite it or the next boot will remove it. – WGroleau Jan 31 '18 at 2:21
  • I could write my own solution if I was willing to do it with temporary files. I was wondering how to do it without any files (feels cleaner that way). – hepcat72 Jan 31 '18 at 22:00
  • After more than thirty years cleaning up messes in code, I can understand wanting to “feel cleaner.” But sometimes I have to force myself to resist details that don’t really matter. For code that needs to last, readable is more important than elegant. – WGroleau Feb 1 '18 at 2:32
  • Is there a way to pipe the selection as stdin to a command and replace the selection with stdout? The regex in perl would be 's:\S::' – WGroleau Feb 2 '18 at 1:54
  • Yeah, it’s a very simple perlre, but you don’t have it quite right. It would be s:\s::gs. You need the s at the end for \s to match new lines, but I don’t think that covers carriage returns. For that, you’d need s:[\r\s]:gs. That’s how I would have done it if I were to use a file. I wouldn’t trust echoing, especially if it’s multi-line. That’s the first thing that comes to mind. But really, the AppleScript solution is what I was looking for. It doesn’t handle weird characters, but it’s good enough. – hepcat72 Feb 2 '18 at 3:05
4

In Automator, create a new Service with the following settings:

  • Service receives selected [text] in [Terminal]

Add a Run AppleScript actions, replacing the default code with the code below:

Example AppleScript code:

on run {input, parameters}

    set selText to item 1 of input as text
    set AppleScript's text item delimiters to {space, tab, linefeed, return}
    set selText to text items of selText
    set AppleScript's text item delimiters to {}
    set selText to selText as string

    display dialog ¬
        "The selection contains " & (count characters of selText) ¬
        & " characters." & linefeed & linefeed & selText ¬
        buttons {"OK"} default button 1

end run

Save the Automator Service as, e.g.: Count Selected Characters

Now in Terminal, select the wanted selection, e.g:

Selected Text

Right-click and select Count Selected Characters from the Services context menu.

Example output of the selection show above:

Display Dialog


Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

  • Awesome. Thanks so much. I knew someone would know how to do this! – hepcat72 Jan 31 '18 at 21:55
0

You could try WordService. DEVONtechnologies.

It is freeware, and does what you want (choose Services -> WordService: Statistics) plus a lot more.

Here's the result with a selection on your question:
enter image description here

From top to bottom:
Characters
Spaces
Total
Words
Lines

  • 1
    This is a nice utility, I've used it for many years, however this question is about Terminal and AppleScript, not some third-party utility and this does not really answer the question within the scope as asked. – user3439894 Jan 31 '18 at 17:03
  • 1
    "I'm looking to create a service that will allow me to count all selected non-whitespace characters", Wordservice does this & it works with terminal output. – PeterVP Feb 1 '18 at 12:22
  • Yes, but I can’t use the output to count nucleotides. I want a string back, though I admit I didn’t ask this explicitly. – hepcat72 Feb 2 '18 at 3:10
  • I edited the question to clear up the confusion of what I was looking for. You're right, it was not very clear. – hepcat72 Feb 2 '18 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .