OK
Here is a working version that goes by paragraphs
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
property hColors : {"Auto", "Black", "Blue", "Turquoise", "Bright Green", "Pink", "Red", "Yellow", "White", "Dark Blue", "Teal", "Green", "Violet", "Dark Red", "Dark Yellow", "Gray 50", "Gray 25", "unknown"}
on run
local myDoc, myResult, i, pc, wc, wordList, WordColor, myIndex, nHighlightedWords, ws, wl, psteps, tsteps
set myColor to choose from list hColors with title "Windows Hightlight Colors" with prompt "Please choose a highlight color..."
if class of myColor is boolean then return -- user chose 'Cancel'
set WordColor to item 1 of myColor
set myIndex to getIndexOfItemInList(WordColor, hColors)
set nHighlightedWords to 0
tell application "Microsoft Word"
if not (exists window 1) then return
set pc to count (paragraphs of document 1)
set WordColor to item myIndex of {auto, black, blue, turquoise, bright green, pink, red, yellow, white, dark blue, teal, green, violet, dark red, dark yellow, gray50, gray25, no highlight}
set my progress description to "Getting MS-Word highlight colors…"
set my progress completed steps to 0
if pc < 1000 then
set psteps to pc div 100 + 1 --if psteps = 0 then set psteps to 1
set tsteps to pc div psteps
else
set tsteps to 100
set psteps to pc div tsteps
end if
set my progress additional description to "(Paragraph 0 of " & pc & ")"
set my progress total steps to tsteps
repeat with i from 1 to pc
set myResult to highlight color index of text object of paragraph i of document 1
set wc to count (words of text object of paragraph i of document 1)
if myResult is not no highlight then
if myResult is in {WordColor, missing value} then -- partial highlight
repeat with j from 1 to wc
set myResult to highlight color index of word j of text object of paragraph i of document 1
if myResult = WordColor then
set ws to my trimSpace(content of word j of text object of paragraph i of document 1)
set wl to length of ws
if wl = 1 then
if ws is not in {".", ",", ";", ":", "!", "?", "«", "»", "$", "€", "%", "-", "+", "@", "#", "*", "^", "<", ">", "(", ")", "/", "\\", "~"} then
set nHighlightedWords to nHighlightedWords + 1
end if
else if wl = 2 then
if ws is not {"« ", " »"} then
set nHighlightedWords to nHighlightedWords + 1
end if
else
set nHighlightedWords to nHighlightedWords + 1
end if
end if
end repeat
end if
end if
if (i mod psteps) = 0 then
set my progress completed steps to (my progress completed steps) + 1
set my progress additional description to "(Paragraph " & i & " of " & pc & ", # of words = " & wc & ") " & (myResult as text) & ", \"" & (content of word i of document 1) & "\""
end if
--delay 0.2
end repeat
set my progress completed steps to tsteps
set my progress additional description to "(" & pc & " of " & pc & ") All Done!"
delay 1
end tell
display alert "# of words with highlight color \"" & myColor & "\" is " & nHighlightedWords
return nHighlightedWords
end run
on getIndexOfItemInList(theItem, theList)
script L
property aList : theList
end script
repeat with a from 1 to count of L's aList
if item a of L's aList is theItem then return a
end repeat
return 0
end getIndexOfItemInList
on trimSpace(aString)
local i
repeat with i from length of aString to 1 by -1
if text i of aString ≠ " " then
exit repeat
end if
end repeat
return text 1 thru i of aString
end trimSpace
WAY FASTER
** EDIT ** - FIXED
** EDIT ** - FIXED again