Way to get character count of selection?

There is an action available for word count of a selection, but I need character counts of selections (for writing headlines).

Is there already a way to do this?

Or would someone with more script aptitude than I write an action?

The fundamental part is a one-line Script action:

alert(editor.getSelectedRange()[1] + " characters")

editor.getSelectedRange() returns a list. The first item (index 0) is the character position of the start of the selection; the second (index 1) is the length of the selection.

You could add some niceties, like returning the length of the line where the cursor is if there’s no selection, but this’ll get you going.

Or maybe it got me going. Here’s a script that gives the length of the selection if there is one and the length of the line the cursor is on if there isn’t.

var [loc, len] = editor.getSelectedRange()
if (len == 0) {
	[loc, len] = editor.getSelectedLineRange()
	if (editor.getTextInRange(loc+len-1, 1) == '\n') {
		len--;
	}
}
alert(len + " characters")

The inner if statement chops off the trailing newline that comes attached to the end of every line except the last one.

1 Like

Thank you so much. Will this now be added as an action I and others can install?

“Add New Action” —> Give it a name —> Add 1 Step (“Script”) —> paste in the script above —> Save.

(And it works. Thanks - helpful in creating tweet threats but not wanting sentences chopped in half in the process of keeping under 280 characters).

1 Like

It works! Brilliant. Now I should learn Javascript…

This is brilliant and just what I needed. Thanks so much @drdrang

FYI, display of the selection count in the interface is something that has been added to current beta of Drafts and will be in R15 when it comes out this Fall.

1 Like