For an example of how to use the text engine to do text editing, refer to Listing 3.27.
This section is under construction.
Example Text Engine Scripting:
' file: 07 Text.vbs
' Jim Trudeau
' This script was created as an exercise for the students in
' the Scripting CodeWarrior course available from Metrowerks.
' This script creates an application object and a plain text
' document. The script plays around with the contents of the
' document and displays pertinent statistics. It then saves
' the doucment.
option explicit 'all variables must be declared
dim CW
dim textDocument 'text document object to hold report
dim textEngine 'the object for dealing with text
dim eol 'end-of-line character for formatting
dim result 'returned values
dim theStart 'selection start
dim theEnd 'selection end
eol = chr(13) 'set end of line character
'create an instance of CodeWarrior
set CW = CreateObject("CodeWarrior.CodeWarriorApp")
'*** open a text document (create if it doesn't exist)
set textDocument = CW.OpenTextDocument("C:\temp.txt", true)
'*** get the text engine
set textEngine = textDocument.TextEngine
SelectAll
'*** add text without changing the insertion point
textEngine.SelectionText = ("Now is the time for all good men" &eol)
'*** get selection range after entering that line
theStart = textEngine.SelectionStart
theEnd = textEngine.SelectionEnd
'set insertion point to end of text
MoveToEnd
'display results
textEngine.InsertText("After printing one line," &eol)
textEngine.InsertText("The selection start was " &theStart &eol)
textEngine.InsertText("The selection end was " &theEnd &eol)
'*** set insertion point to start of 2nd line
theStart = textEngine.GetOffsetForLine (2)
textEngine.SelectionStart = theStart
textEngine.SelectionEnd = theStart
'*** add text and change the insertion point
textEngine.InsertText ("to come to the aid of their country." &eol)
'set insertion point to end of text
MoveToEnd
'*** get the number of characters in the document
result = textEngine.TextLength
textEngine.InsertText("Characters before this line: " &result &eol)
'*** get the line number of the previous line
result = textEngine.GetLineForOffset (result)
textEngine.InsertText("The previous line is line " &result &eol)
'*** get the total number of lines
result = textEngine.LineCount
textEngine.InsertText("The total number of lines is " &result)
'=========================================================
' SelectAll - selects all text in a file
' relies on global text engine variable
'=========================================================
'*** write the SelectAll() routine
sub SelectAll
textEngine.SelectionStart = 0
textEngine.SelectionEnd = textEngine.TextLength
end sub
'=========================================================
' MoveToEnd - move insertion point to end of document
' relies on global text engine variable
'=========================================================
sub MoveToEnd
dim length
length = textEngine.TextLength
textEngine.SelectionStart = length
textEngine.SelectionEnd = length
end sub