From 422854eab62c3fbbe21b815e7d28e1611bc9251d Mon Sep 17 00:00:00 2001 From: Jens-Heiner Rechtien Date: Fri, 11 Feb 2005 15:32:16 +0000 Subject: [PATCH] =?UTF-8?q?INTEGRATION:=20CWS=20pyunofixes1=20(1.1.2);=20F?= =?UTF-8?q?ILE=20ADDED=202005/02/01=2015:04:20=20toconnor=201.1.2.2:=20#i2?= =?UTF-8?q?5309#=20fix=20typo=20in=20python=20example=20Issue=20number:=20?= =?UTF-8?q?Submitted=20by:=20Reviewed=20by:=202004/12/12=2019:46:22=20jbu?= =?UTF-8?q?=201.1.2.1:=20=C3=83#i25309#=20moved=20scripts=20one=20level=20?= =?UTF-8?q?higher,=20as=20files=20are=20now=20also=20container=20for=20scr?= =?UTF-8?q?ipts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripting/examples/python/Capitalise.py | 61 +++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripting/examples/python/Capitalise.py diff --git a/scripting/examples/python/Capitalise.py b/scripting/examples/python/Capitalise.py new file mode 100644 index 000000000000..195d74801b13 --- /dev/null +++ b/scripting/examples/python/Capitalise.py @@ -0,0 +1,61 @@ + +# helper function +def getNewString( theString ) : + if( not theString or len(theString) ==0) : + return "" + # should we tokenize on "."? + if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() : + # first two chars are UC => first UC, rest LC + newString=theString[0:1].upper() + theString[1:].lower(); + elif theString[0].isupper(): + # first char UC => all to LC + newString=theString.lower() + else: # all to UC. + newString=theString.upper() + return newString; + +def capitalisePython( ): + """Change the case of a selection, or current word from upper case, to first char upper case, to all lower case to upper case...""" + import string + + # The context variable is of type XScriptContext and is available to + # all BeanShell scripts executed by the Script Framework + xModel = XSCRIPTCONTEXT.getDocument() + + #the writer controller impl supports the css.view.XSelectionSupplier interface + xSelectionSupplier = xModel.getCurrentController() + + #see section 7.5.1 of developers' guide + xIndexAccess = xSelectionSupplier.getSelection() + count = xIndexAccess.getCount(); + if(count>=1): #ie we have a selection + i=0 + while i < count : + xTextRange = xIndexAccess.getByIndex(i); + #print "string: " + xTextRange.getString(); + theString = xTextRange.getString(); + if len(theString)==0 : + # sadly we can have a selection where nothing is selected + # in this case we get the XWordCursor and make a selection! + xText = xTextRange.getText(); + xWordCursor = xText.createTextCursorByRange(xTextRange); + if not xWordCursor.isStartOfWord(): + xWordCursor.gotoStartOfWord(False); + xWordCursor.gotoNextWord(True); + theString = xWordCursor.getString(); + newString = getNewString(theString); + if newString : + xWordCursor.setString(newString); + xSelectionSupplier.select(xWordCursor); + else : + + newString = getNewString( theString ); + if newString: + xTextRange.setString(newString); + xSelectionSupplier.select(xTextRange); + i+= 1 + + +# lists the scripts, that shall be visible inside OOo. Can be omited, if +# all functions shall be visible, however here getNewString shall be surpressed +g_exportedScripts = capitalisePython,