Jump to content

Request: a workflow to count the number of words in the current selection


Recommended Posts

I've tried to no avail to create a workflow using AppleScript that counts the number of words in the selection of the topmost window, I'd like it to work using a keyword rather than a hotkey in order not to clutter my hotkey, someone knows if this is possible? Or is it only feasible to count the number of words of the current clipboard for instance? (I'd rather have a script that counts the words without having to copy to clipboard)  

Link to comment

You can save the current clipboard content to a string, copy the selection to the clipboard using System Events keystroke command, count the words then put the initial content back to the clipboard. In resume:

try
set sCurrentCB to get the clipboard
end try

tell application “System Events”
keystroke "c" using command down
end tell

set sTextToCount to get the clipboard as text
—count words here

try
set the clipboard to sCurrentCB
end try
Link to comment
  • 8 years later...

That works, thank you. I have been looking for a way to do that ... and with a minor modification it can be used as a Universal Action:

 

on run
    set theWordCount to count words of ("{query}" as string)
    display dialog (theWordCount & " words in the selected text." as string)
end run

Edited by fillmoreeast
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...