AppleScript Word Count Service

I am trying to create a service in OSX vocabulary that counts the word count of the selected text. I have an automaton to run applescript, with the following in it:

on run {input, parameters}
        count words of input
        display alert "Words: " & input
        return input
end run

      

When I compile the script, it says it cannot read every word. What am I doing wrong?

Thanks for the help,

Elliot

+2


source to share


2 answers


First of all, I assume you are testing this in Automator and what is where the error occurs? If so, the likely problem is that there is no input - so it can't read words of anything. To test it successfully, you need to temporarily add a Get Specified Text action before the Run AppleScript action and enter some test text in this field. You will need to remove the Get Specified Text activity before using it as the actual service.

Second, you need to use



count words of (input as string)

      

to get the correct score, otherwise it will return zero.

+3


source


I made one here on Github:

https://gist.github.com/1616556

Current source:



on run {input, parameters}
    tell application "System Events"
        set _appname to name of first process whose frontmost is true
    end tell
    set word_count to count words of (input as string)
    set character_count to count characters of (input as string)
    tell application _appname
        display alert "" & word_count & " words, " & character_count & " characters"
    end tell
    return input
end run

      

Use Automator.app to create a new service and then choose the Run AppleScript action. Paste this code into a text box and save it as Word and Character Count. Now switch to the new app, select the text and open the context menu to find the new option.

+3


source







All Articles