Here is a word frequency concordance Automator Service that works in both versions of Pages. It does not present a page number reference. It is by descending count, and alphabetically, within the repetitive counts as shown in the image here. In any document, you select the text to process, then Menu > Application Name > Services > Frequency. The Service will open TextEdit with the n-pages of scrollable results. It is 16 pt Helvetica Neue for legibility.
Here is the code:
on run {input, parameters}
try
set mySelection to input as text
set formatted_result to concordance(mySelection)
if formatted_result is not null then
set textHeading to "Word Frequency List"
tell application "TextEdit"
activate
set NewDoc to make new document with properties {name:"Concordance"}
make new paragraph at beginning of text of NewDoc with data textHeading & return
make new paragraph at end of text of NewDoc with data formatted_result
tell text of NewDoc
set font to "Helvetica Neue"
set size to 16
set color of paragraph 1 to {0, 0, 65535}
end tell
end tell
else
display dialog with title ¬
"No input selected" with icon stop ¬
giving up after 15
end if
on error errmsg number errnum
display alert ¬
"AppleScript Error" message errmsg & "[" & errnum & "]" as critical ¬
giving up after 30
end try
return input
end run
on concordance(mySelection)
-- Use Ruby to count word frequency and alphabetically sort words
set rb to ¬
"selected = String.new\nfreqs = Hash.new(0)\nselected = ARGV.join(' ').gsub(/[,.]/, \"\")\n
words = selected.split(/[^\\w-]+/)\nwords.each { |word| freqs[word] += 1 }\n
freqs_sorted = freqs.sort do |a,b|\n\t
a.last == b.last ? a.first <=> b.first : b.last <=> a.last\nend\n
freqs_sorted.each { |k,v| printf \"[ %8s ] %s\", v, k }"
do shell script "/usr/bin/ruby -e " & rb's quoted form & space & mySelection's quoted form
end concordance
Launch /Applications/Automator and choose New Document, then click the Service icon, and then select the Choose button.
On the left, you will have a list of Libraries from which to choose workflow items. Find Library > Utilities. In the adjacent column, locate the Run Applescript workflow. Click on it, then drag and drop it in the large workflow window to your right.
At the top, you can select Service receives text in any application. Leave Output replaces selected text unchecked.
In your Run AppleScript workflow window, you will see AppleScript boilerplate. Click on it and press command+A, then backspace to delete this content. Copy and paste the above code into this Run AppleScript workflow window. Press the Run button in Automator's upper right corner. If (and it should) a TextEdit window pops up, you are good, and you want to press File > Save. A dialog box will pop-up where you can name your Service. I called my Frequency, you may wish to call it something else. Once you have save it, you can exit Automator. Services are deposited in yourlogin directory/Library/Services.
If you have a Pages document open with text, either select it, or command+a to select the entire document. Now, you select your Service via Pages > Services > Frequency. This will now pop-up a new TextEdit document with the results. If you want to save this new document, you must use option+File to Save as...