I use this VBA macro in MS Word to get a word count in a specific color.
Sub HighlightedWordCount()
Dim objDoc As Document
Dim objWord As Range
Dim nHighlightedWords As Long
Dim strHighlightColor As String
Dim highlightColorName As String
Application.ScreenUpdating = False
Set objDoc = ActiveDocument
nHighlightedWords = 0
strHighlightColor = InputBox("Choose a highlight color (enter the value):" & vbNewLine & _
vbTab & "Auto" & vbTab & vbTab & "0" & vbNewLine & _
vbTab & "Black" & vbTab & vbTab & "1" & vbNewLine & _
vbTab & "Blue" & vbTab & vbTab & "2" & vbNewLine & _
vbTab & "Turquoise" & vbTab & vbTab & "3" & vbNewLine & _
vbTab & "BrightGreen" & vbTab & "4" & vbNewLine & _
vbTab & "Pink" & vbTab & vbTab & "5" & vbNewLine & _
vbTab & "Red" & vbTab & vbTab & "6" & vbNewLine & _
vbTab & "Yellow" & vbTab & vbTab & "7" & vbNewLine & _
vbTab & "White" & vbTab & vbTab & "8" & vbNewLine & _
vbTab & "DarkBlue" & vbTab & vbTab & "9" & vbNewLine & _
vbTab & "Teal" & vbTab & vbTab & "10" & vbNewLine & _
vbTab & "Green" & vbTab & vbTab & "11" & vbNewLine & _
vbTab & "Violet" & vbTab & vbTab & "12" & vbNewLine & _
vbTab & "DarkRed" & vbTab & vbTab & "13" & vbNewLine & _
vbTab & "DarkYellow" & vbTab & "14" & vbNewLine & _
vbTab & "Gray 50" & vbTab & vbTab & "15" & vbNewLine & _
vbTab & "Gray 25" & vbTab & vbTab & "16", "Pick Highlight Color")
If strHighlightColor = "" Then
' User pressed cancel button
Exit Sub
ElseIf Not IsNumeric(strHighlightColor) Then
MsgBox "Invalid input. Please enter a value between 1 and 16."
Exit Sub
Else
Dim inputNum As Integer
inputNum = CInt(strHighlightColor)
If inputNum < 1 Or inputNum > 16 Then
MsgBox "Invalid input. Please enter a value between 1 and 16."
Exit Sub
End If
End If
Select Case strHighlightColor
Case "0"
highlightColorName = "Auto"
Case "1"
highlightColorName = "Black"
Case "2"
highlightColorName = "Blue"
Case "3"
highlightColorName = "Turquoise"
Case "4"
highlightColorName = "BrightGreen"
Case "5"
highlightColorName = "Pink"
Case "6"
highlightColorName = "Red"
Case "7"
highlightColorName = "Yellow"
Case "8"
highlightColorName = "White"
Case "9"
highlightColorName = "DarkBlue"
Case "10"
highlightColorName = "Teal"
Case "11"
highlightColorName = "Green"
Case "12"
highlightColorName = "Violet"
Case "13"
highlightColorName = "DarkRed"
Case "14"
highlightColorName = "DarkYellow"
Case "15"
highlightColorName = "Gray 50"
Case "16"
highlightColorName = "Gray 25"
Case Else
highlightColorName = "unknown"
End Select
Dim S$
For Each objWord In objDoc.Words
If objWord.HighlightColorIndex = CInt(strHighlightColor) Then
S = Trim(objWord.Text)
If Len(S) = 1 Then
Select Case S
Case ".", ",", ";", ":", "!", "?", ChrW(171), ChrW(187), "$", "€", "%", "-", "+", "@", "#", "*", "^", "<", ">", "(", ")", "/", "\", "~", Chr(34), Chr(160), Space(1), Chr(255)
'Do nothing or skip it. You can add more special characters to exclude them.
Case Else
nHighlightedWords = nHighlightedWords + 1
End Select
ElseIf Len(S) = 2 Then
If (S = ChrW(171) & ChrW(160)) Or (S = ChrW(160) & ChrW(187)) Then 'Exclusion
'Do nothing to ignore the special case: "«" + <nbsp> and "»" + <nbsp>
Else
nHighlightedWords = nHighlightedWords + 1
End If
Else
nHighlightedWords = nHighlightedWords + 1
End If
End If
Next objWord
Select Case strHighlightColor
Case "0"
highlightColorName = "Auto"
Case "1"
highlightColorName = "Black"
Case "2"
highlightColorName = "Blue"
Case "3"
highlightColorName = "Turquoise"
Case "4"
highlightColorName = "BrightGreen"
Case "5"
highlightColorName = "Pink"
Case "6"
highlightColorName = "Red"
Case "7"
highlightColorName = "Yellow"
Case "8"
highlightColorName = "White"
Case "9"
highlightColorName = "DarkBlue"
Case "10"
highlightColorName = "Teal"
Case "11"
highlightColorName = "Green"
Case "12"
highlightColorName = "Violet"
Case "13"
highlightColorName = "DarkRed"
Case "14"
highlightColorName = "DarkYellow"
Case "15"
highlightColorName = "Gray 50"
Case "16"
highlightColorName = "Gray 25"
Case Else
highlightColorName = "unknown"
End Select
MsgBox ("The number of alphanumeric words highlighted in " & highlightColorName & " is " & nHighlightedWords & ".")
Application.ScreenUpdating = True
Set objDoc = Nothing
End Sub
The Macro displays a dialog where the user is asked to enter a value that corresponds to the various stock Word highlight colors, and the word count is then displayed in a. second dialog.
Wondering if something similar could be achieved in AppleScript. I’d like to be able to get the word count through th Shortcuts app instead of a macro. Would that even be possible?
Cheers