Branch libreoffice-5-0-4
[LibreOffice.git] / scripting / examples / python / Capitalise.py
blob3c5366589ef9760b6bcb4cc4a75b6558de32bb31
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 # helper function
20 def getNewString( theString ) :
21 if( not theString or len(theString) ==0) :
22 return ""
23 # should we tokenize on "."?
24 if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() :
25 # first two chars are UC => first UC, rest LC
26 newString=theString[0:1].upper() + theString[1:].lower();
27 elif theString[0].isupper():
28 # first char UC => all to LC
29 newString=theString.lower()
30 else: # all to UC.
31 newString=theString.upper()
32 return newString;
34 def capitalisePython( ):
35 """Change the case of a selection, or current word from upper case, to first char upper case, to all lower case to upper case..."""
36 import string
38 # The context variable is of type XScriptContext and is available to
39 # all BeanShell scripts executed by the Script Framework
40 xModel = XSCRIPTCONTEXT.getDocument()
42 #the writer controller impl supports the css.view.XSelectionSupplier interface
43 xSelectionSupplier = xModel.getCurrentController()
45 #see section 7.5.1 of developers' guide
46 xIndexAccess = xSelectionSupplier.getSelection()
47 count = xIndexAccess.getCount();
48 if(count>=1): #ie we have a selection
49 i=0
50 while i < count :
51 xTextRange = xIndexAccess.getByIndex(i);
52 #print "string: " + xTextRange.getString();
53 theString = xTextRange.getString();
54 if len(theString)==0 :
55 # sadly we can have a selection where nothing is selected
56 # in this case we get the XWordCursor and make a selection!
57 xText = xTextRange.getText();
58 xWordCursor = xText.createTextCursorByRange(xTextRange);
59 if not xWordCursor.isStartOfWord():
60 xWordCursor.gotoStartOfWord(False);
61 xWordCursor.gotoNextWord(True);
62 theString = xWordCursor.getString();
63 newString = getNewString(theString);
64 if newString :
65 xWordCursor.setString(newString);
66 xSelectionSupplier.select(xWordCursor);
67 else :
69 newString = getNewString( theString );
70 if newString:
71 xTextRange.setString(newString);
72 xSelectionSupplier.select(xTextRange);
73 i+= 1
76 # lists the scripts, that shall be visible inside OOo. Can be omitted, if
77 # all functions shall be visible, however here getNewString shall be suppressed
78 g_exportedScripts = capitalisePython,