Weird. So Bright Green gets saved as “green”.
and Green gets saved as “darkgreen”
Stupid Microsoft.
So the names in the actual saved xml don’t match exactly the color names saved in the SDEF file
Here is a new version
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"}
property hColorConstants : {"Auto", "Black", "Blue", "Cyan", "green", "Pink", "Red", "Yellow", "White", "darkBlue", "darkCyan", "darkGreen", "darkMagenta", "darkRed", "darkYellow", "Gray", "lightGray", "unknown"}
on run
local DocPath, DocumentText, WordSource, WordColor, nHighlightedWords, n
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 n to getIndexOfItemInList(WordColor, hColors)
set WordColor to item n of hColorConstants
set nHighlightedWords to 0
tell application "Microsoft Word"
set DocPath to (full name of document 1)
end tell
set WordSource to (quoted form of (POSIX path of DocPath))
set DocumentText to do shell script ("unzip -p " & WordSource & " -x /word/document.xml")
set text item delimiters to {"<w:highlight w:val=\"" & WordColor & "\""}
set DocumentText to (rest of text items of DocumentText)
set text item delimiters to {"<w:t xml:space=\"preserve\">", "<w:t>", "</w:t>"}
repeat with NextHighlight in DocumentText
try
set nHighlightedWords to nHighlightedWords + (count (words of text item 2 of NextHighlight))
end try
end repeat
activate me
display alert "# of words with highlight color \"" & myColor & "\" is " & nHighlightedWords giving up after 4
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