JohnV Administrator

Joined: 07 Mar 2003 Posts: 8979 Location: Lexinton, Kentucky, USA
|
Posted: Mon Jan 12, 2004 8:39 am Post subject: Change Text Case (rotate through several possibilities) |
|
|
Hi Russ,
I'll go first here to get things started.
Here's a macro that changes text between uppercase, title case, small capitals and lowercase. The cursor can be before, in or after the word. It also works on a selected area.
Although someone else has probably already done this, I did it as a programing exercise in response to a question in the SO forum. When finished, I realized there is enough code in place that it can fairly easily be change (or expanded) to say, alternate between plain, bold, italics and underlined simply by using the recording feature to get the needed code. On the theory that some beginners might try this I have heavily commented the code.
| Code: | REM ***** BASIC *****
'Set up variables that can track where we are from one macro run to the next
Global F3%, Was$ 'Counter & text present at the previous cursor position
Sub ChangeCase
Dim bCollapsed as Boolean 'true of false variable
oFrame = thisComponent.CurrentController.Frame 'get the current document
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")'set up dispatcher
oVC = oFrame.Controller.getViewCursor() 'This is the cursor you normally see. If something
'is selected then it's the entire highlighted area and thus has two ends. The cursor
'below is a programing cursor and this statement puts it wherever the view cursor is.
oTC = thisComponent.Text.createTextCursorByRange(oVC)
If (oVC.isCollapsed()) then bCollapsed = true 'no text selected
Is$ = oTC.String(oTC.goRight(10, true)) : oTC.collapseToStart()'Get some text to see if the
If (Lcase(Is$) <> Lcase(Was$)) then F3% = 0 : Was$ = Is$ 'cursor been moved between runs.
Select Case F3% 'select the routine below based on the current value of F3%, initially 0.
case 0 'TO UPPERCASE
If (bCollapsed and oTC.isEndOfWord()) then'If both these things are true move into the word.
oVC.goLeft(1,false) '"true" will cause the text to be selected, not needed here
ElseIf (bCollapsed and oTC.isStartOfWord()) then 'but is below.
oVC.goRight(1,false)
EndIf 'the code between here and the cleanup was recorded in all routines.
dispatcher.executeDispatch(oFrame, ".uno:ChangeCaseToUpper", "", 0, Array())
If (bCollapsed and oTC.isEndOfWord()) then 'this is just clean up and ensures that
oVC.goRight(1,false) 'the cursor ends up back where it started.
ElseIf (bCollapsed and oTC.isStartOfWord()) then
oVC.goLeft(1,false)
EndIf
case 1 'TO TITLECASE
If (bCollapsed and oTC.isEndOfWord()) then
oVC.goLeft(1,true)
dispatcher.executeDispatch(oFrame, ".uno:WordLeftSel", "", 0, Array())
ElseIf (bCollapsed and oTC.isStartOfWord()) then
oVC.goRight(1,true)
dispatcher.executeDispatch(oFrame, ".uno:WordRightSel", "", 0, Array())
EndIf
dispatcher.executeDispatch(oFrame, ".uno:ChangeCaseToLower", "", 0, Array())
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "CaseMap"
args1(0).Value = 3
dispatcher.executeDispatch(oFrame, ".uno:CaseMap", "", 0, args1())
If (bCollapsed and oTC.isEndOfWord()) then oVC.collapseToEnd() 'another clean up
If (bCollapsed and oTC.isStartOfWord()) then oVC.collapseToStart()
case 2 'TO SMALL CAPS
If (bCollapsed and oTC.isEndOfWord()) then
oVC.goLeft(1,true)
dispatcher.executeDispatch(oFrame, ".uno:WordLeftSel", "", 0, Array())
ElseIf (bCollapsed and oTC.isStartOfWord()) then
oVC.goRight(1,true)
dispatcher.executeDispatch(oFrame, ".uno:WordRightSel", "", 0, Array())
EndIf
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "CaseMap"
args2(0).Value = 4
dispatcher.executeDispatch(oFrame, ".uno:CaseMap", "", 0, args2())
If (bCollapsed and oTC.isEndOfWord()) then oVC.collapseToEnd()
If (bCollapsed and oTC.isStartOfWord()) then oVC.collapseToStart()
case 3 'TO LOWERCASE
If (bCollapsed and oTC.isEndOfWord()) then
oVC.goLeft(1,true)
dispatcher.executeDispatch(oFrame, ".uno:WordLeftSel", "", 0, Array())
ElseIf (bCollapsed and oTC.isStartOfWord()) then
oVC.goRight(1,true)
dispatcher.executeDispatch(oFrame, ".uno:WordRightSel", "", 0, Array())
EndIf
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "CaseMap"
args3(0).Value = 0
dispatcher.executeDispatch(oFrame, ".uno:CaseMap", "", 0, args3())
dispatcher.executeDispatch(oFrame, ".uno:ChangeCaseToLower", "", 0, Array())
If (bCollapsed and oTC.isEndOfWord()) then oVC.collapseToEnd()
If (bCollapsed and oTC.isStartOfWord()) then oVC.collapseToStart()
End Select
If (F3% = 3) then 'adjust the value of F3%
F3% = 0
Else
F3% = F3% +1
EndIF
End Sub
|
|
|