' Draft Scripting class 24 (8/14/2011)
' Doing things with colors of text.
' example 1:
' this example is taken from a routine in a Word related app, which reads
the status bar for Word XP/2003.
' the point of the example is in showing how to deal with elements on the
display whose color, not their text, conveys the needed information.
Function readStatusBar()
' replaces the WE read status bar functionality in a Word document
Dim sbWin
Dim w
Dim winList
Dim strLine
Dim i
Dim j
Dim c
readStatusBar = False
' find the status bar window
If Not ActiveWindow.overlap Is Nothing Then
Set winList = ActiveWindow.overlap.children.FilterByName("_WwC")
Set sbWin = Nothing
For Each w In winList
If Len(w.clips.ClipsText) > 0 Then
' looks like the status bar
If sbWin Is Nothing Then
Set sbWin = w
End If
End If ' Len(w.clips.ClipsText) > 0
Next
If Not sbWin Is Nothing Then
' have a status bar to work with
strLine = Trim(sbWin.clips.ClipsText)
' first speak the elements on the status bar whose text is the information
' code removed here which just breaks apart the status bar elements, and
speaks each element with a descriptive label to make it more understandable
' remainder of the status bar is strings where the foreground color is
significant, not the strings themselves
' (black indicates an option is on, gray indicates that it's off)
' (black is a color where all 3 color components (red, green, and blue) are
0)
' (white has all 3 components set to 255)
' (all other colors are some mix; I believe Word uses a gray made up of 3
values of 128)
' since a string variable doesn't hold any color information, we have to go
to the clips collection which make up the status bar, and locate each clip
for one of these "colored" elements
For Each c In sbWin.clips
If c.Type = ctText Then
' this is a text clip
Select Case UCase(Trim(c.text))
Case "REC"
' this is one of the "colored" elements
If c.ForegroundColor.Red = 0 Then ' this is
black (on)
Speak "recording a macro, "
End If
Case "EXT"
' this is one of the "colored" elements
If c.ForegroundColor.Red = 0 Then ' this is
black (on)
Speak "extended selection mode, "
End If
Case "TRK"
' this is one of the "colored" elements
If c.ForegroundColor.Red = 0 Then ' this is
black (on)
Speak "tracked changes is on, "
End If
Case "OVR"
' this is one of the "colored" elements
If c.ForegroundColor.Red = 0 Then ' this is
black (on)
Speak "overtype mode."
End If
End Select
End If ' C.Type = ctText
Next
readStatusBar = True
End If ' Not sbWin Is Nothing
End If ' Not ActiveWindow.overlap Is Nothing
End Function
' end of example 1
' example 2:
' showing how to manually find out if a clip is highlighted or not, and how
to speak highlighted clips
dim oCurClip
dim oWindow
dim sColorString
dim oBackgrounds
dim highlightColorString
dim s
' first run through the clips of this window and determine which background
color combination is used only one time (and so, likely to indicate
something highlighted)
' background colors are stored as a string of 6 hex characters (2 for each
color component), so they can easily be compared to one another
Set oBackgrounds = CreateObject("scripting.dictionary") ' used to keep track
of the number of clips for each background color
For Each oCurClip In oWindow.clips(, False)
If oCurClip.Type = ctText Or oCurClip.Type = ctFakeText Then
sColorString = Hex(oCurClip.BackgroundColor.Red) &
Hex(oCurClip.BackgroundColor.Green) & Hex(oCurClip.BackgroundColor.Blue)
If oBackgrounds.Exists(sColorString) Then
oBackgrounds(sColorString) =
oBackgrounds(sColorString) + 1
Else
oBackgrounds.Add sColorString, 1
End If
End If
Next
' now look for the one combo which only occurs once
highlightColorString = ""
For Each s In oBackgrounds.keys
If oBackgrounds(s) = 1 Then
' this background color combo only appears once in the clips collection
' this is the highlight background
highlightColorString = s
Exit Function
End If
Next
function speakClipIfHighlighted(clip)
' now determine if the current clip being evaluated is highlighted, and if
so speak it
dim sBackColor
sBackColor = Hex(clip.BackgroundColor.Red)
sBackColor = sBackColor & Hex(clip.BackgroundColor.Green)
sBackColor = sBackColor & Hex(clip.BackgroundColor.Blue)
if (sBackColor = sHighlightColor) then speak clip.text
end function
' or, if you want the highlighted text of the window we were working with,
you can just use this method:
speak oWindow.clips.SearchText("", False, True).clipsText ' speaks
highlighted text
' the above method searches the entire clips collection for the empty
string, which matches all text, has false for the case match, and has true
for the highlighted text only parameter.
' it returns a clips collection, and we speak the clipsText property.
' end of example 2
' archives of these classes can be found at:
' https://www.gwmicro.com/App_Central/Developers/Interactive_Classes/