OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Writer] Python & OOo: Capitalize

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
The gray Cardinal
General User
General User


Joined: 19 Oct 2007
Posts: 5

PostPosted: Sun Oct 21, 2007 1:08 am    Post subject: [Writer] Python & OOo: Capitalize Reply with quote

Macro Capitalise.py is distributed with OpenOffice v2.3. But I don't like it much because of the following reasons:
1. It doesn't work with the last word in a file, when there isn't any text or line feed after this word.
2. If you work with a word that has caret within it (not with a selected piece of text), for some reason Capitalise.py selects this word after processing.
3. If you deal with some non-adjacent selected pieces of text inside a document, a runtime exception occurs.
4. If you deal with a word that contains the caret and stands at the end of a line, after which there is a blank line, Capitalise.py adds another blank line.
Here is my version of the Capitalise.py, free of the above peculiarities:
Code:
# copyleft © The gray Cardinal, Russia, http://script-coding.info/

def CapitalizeString(theString):
    # Capitalize function
    if (not theString or len(theString) == 0):
        return ""
    if theString[0].isupper() and len(theString) >= 2 and theString[1].isupper():
        newString = theString[0:1].upper() + theString[1:].lower()
    elif theString[0].isupper():
        newString = theString.lower()
    else:
        newString = theString.upper()
    import re
    newString = re.sub('\n', '', newString)
    return newString

def Capitalize(dummy = ''):
    '''
    Macro for Writer.
    Changes the case of the selected pieces of text or the word with the caret within it.
    '''
    xController = XSCRIPTCONTEXT.getDocument().getCurrentController()
    # current selection
    xIndexAccess = xController.getSelection()
    # number of selected pieces of text
    count = xIndexAccess.getCount()
    if count == 1 and len(xIndexAccess.getByIndex(0).getString()) == 0:
        # No selection. Process the word with the caret within it.
        xTextRange = xIndexAccess.getByIndex(0)
        xWordCursor = xTextRange.getText().createTextCursorByRange(xTextRange)
        if not xWordCursor.isStartOfWord(): xWordCursor.gotoStartOfWord(False)
        xWordCursor.gotoEndOfWord(True)
        theString = xWordCursor.getString()
        # processing of the text
        newString = CapitalizeString(theString)
        # replace the text by the processed text
        xWordCursor.setString(newString)
    else:
        # Selection occurred. Now process all the selected pieces of text.
        i = 0
        while i < count:
            # selected piece of text
            xTextRange = xIndexAccess.getByIndex(i)
            # text of selected piece of text
            theString = xTextRange.getString()
            if len(theString) > 0:
                # process the text
                newString = CapitalizeString(theString)
                # replace the text by the processed text
                xTextRange.setString(newString)
                # selection
                xController.select(xTextRange)
                # If some non-adjacent pieces of text were selected (by Ctrl),
                # only the selection of the last piece of text will be restored.
                # Unfortunately, there is no way to restore selection of all the pieces of text.
            i += 1
    return None

g_exportedScripts = BDS, Capitalize
Back to top
View user's profile Send private message
JZA
OOo Advocate
OOo Advocate


Joined: 01 Feb 2003
Posts: 432
Location: Mexico

PostPosted: Sun Oct 21, 2007 12:18 pm    Post subject: Reply with quote

you should submit it as a patch for the udk.openoffice.org / api.openoffice.org project. I would like to know if you have any basic GUI programming from PyUNO. Thanks for the snippet.
_________________
Alexandro Colorado
PPMC Apache OpenOffice
http://es.openoffice.org
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
JZA
OOo Advocate
OOo Advocate


Joined: 01 Feb 2003
Posts: 432
Location: Mexico

PostPosted: Mon Oct 22, 2007 6:20 am    Post subject: Reply with quote

so if you are not too intimidated signing a JCA, I found the original Capitalize.py scripting proposal for submition.

http://www.openoffice.org/issues/show_bug.cgi?id=32721
_________________
Alexandro Colorado
PPMC Apache OpenOffice
http://es.openoffice.org
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
The gray Cardinal
General User
General User


Joined: 19 Oct 2007
Posts: 5

PostPosted: Mon Oct 22, 2007 10:15 am    Post subject: Reply with quote

http://www.openoffice.org/issues/show_bug.cgi?id=82861
I use SciTE.
Back to top
View user's profile Send private message
JZA
OOo Advocate
OOo Advocate


Joined: 01 Feb 2003
Posts: 432
Location: Mexico

PostPosted: Mon Oct 22, 2007 12:29 pm    Post subject: Reply with quote

I think you choose the wrong project. The one I sent you was on the Framework project and scripting subcomponent.

I will fix it for you.

Update: Sorry for that, I dont have the permissions to re-assign it to
Component: Framework
subcomponent: scripting
OS: ALL
OOo Version: 2.3
_________________
Alexandro Colorado
PPMC Apache OpenOffice
http://es.openoffice.org
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group