Counting characters in a segment

I came across this script from Selcuk Akyuz in the Proz website a few weeks ago:

;------------------------------------------------------------------------------
; Count characters in a segment
;------------------------------------------------------------------------------
!c::
Send, ^c
ClipWait
Chars := StrLen(Clipboard)
RegExReplace(Clipboard,A_Space,"",Spaces)
MsgBox % "The highlighted text is " Chars " characters in length and contains " Spaces " spaces."
clipboard = ; Empty the clipboard
return

The way it works is you select some text in the source or target segment and press Alt+c.  You then get something like this with a small message box that contains the count including spaces:

  • Hi Paul,

    I have a problem with this script. I think it needs some way to reset it, although I have no idea how. If I select a three-character word after having run the script, say, for a 10-character selection, the message box says the new selection, which I plainly see has only 3 characters, has 10 characters, in other words, the previous result seems to remain in the clipboard and is output again after a new selection and new press of the hotkey.

  • Hi Nora,

    ok - I amended it as I also had the same issue when I repeat it like this. So I added:

    Clipboard =

    This just clears the clipboard.

    That works for me now flawlessly. I'll update the script above in case someone else takes it.

    Regards

    Paul
  • That's great, thanks Paul!
  • Thanks, Paul. I had to change alt+c to something else (I chose ctrl+t, after removing this default kb shortcut) in order not to trigger Studio's menu. It worked the first time, but then not anymore. Would that be typing speed or keyboard related? Anyway, it works great with ctrl + t now.
  • Thanks !

    I ran into a problem with the script that I don't know how to solve. Maybe an IF no text is selected, then return, or IF no text selected, show 0 character?

    I have no problem running the script on selected text, but if I press the hotkeys without first selecting text, nothing happens, and the script no longer works.

    This script would be super useful to my team when translating social media content, but some of my translator are not that comfortable with computers, and I definitely anticipate one of them forgetting to select the text, and they would’nt know how to restart the script.

    Any idea how to work around this error?

  • ClipWait with no parameter will wait indefinitely until the clipboard contains some data. Because there is nothing selected, the script doesn't ever get to the next line.

    Please change the ClipWait line to something like this:

    ClipWait, 1 ; wait for 1 sec max.

    and it will not hang up.

    Good luck!

  • Works perfectly and I learned a new thing! Thanks Jesús!

    Here’s my code now, with a different hot key :

    ^+!c::
    Send, ^c
    ClipWait, 1 ; wait for 1 sec max
    Chars := StrLen(Clipboard)
    RegExReplace(Clipboard,A_Space,"",Spaces)
    MsgBox % "The highlighted text is " Chars " characters in length and contains " Spaces " spaces."
    clipboard = ; Empty the clipboard
    return