[Solved] Change certain styles background or font color

Creating a macro - Writing a Script - Using the API

[Solved] Change certain styles background or font color

Postby DynV » Mon Jun 08, 2015 2:01 am

I have a document that contain mostly styles based on the default, black on white. Certain styles are different, one is red font rest default (white background) and the other a yellow background rest default (black font). By default I want those style to be easy to read, thus subtle, so the red is dark and yellow is pale ; I'm calling this the read mode. However those information are important to stand out when I want to "diagonally read" or read at a glimpse the document (please let me know the proper term) ; I call this the overview mode. I could change the styles manually but I'd like to have a way to quickly change between the 2 "modes".

If one want to make an example, it doesn't matter the name of the style nor the colors, as long as 1 style has its font color changed and another has its background color changed. Also for now I can simply have a macro that search for the style name, I assume as a string; but later I'd like to find that style ID, which I assume is a number, and have the modifications applied on that ID instead, so that I may change the style name and the macro will still be operational.

I started wanting to learn about macros, instead of asking for help. So I searched using a web engine, which I didn't find anything suitable to my issue (1st paragraph). I then looked at this forum finding the Macros and UNO API forum and started with the Basic Guide sub-forum leading me to a page which I followed to Text Documents then The Structure of Text Documents but I didn't find something about style. So I then [url=http://forum.openoffice.org/en/forum/search.php?keywords=style&terms=all&author=&fid[]=20&sc=1&sf=all&sr=topics&sk=t&sd=d&st=0&ch=300&t=0&submit=Search]searched[/url] this forum and the closest I found (to my issue in the 1st paragraph) was Change paragraph style and I didn't see how it can be applied to my issue.

Thank you kindly for your help or for a hint
Last edited by DynV on Tue Jun 16, 2015 12:37 am, edited 1 time in total.
AOO414m5 on Windows 7 Home Premium SP1. Je suis francophone.
User avatar
DynV
 
Posts: 181
Joined: Tue Apr 06, 2010 10:50 pm
Location: Montreal, Canada

Re: Change certain styles background or font color

Postby FJCC » Mon Jun 08, 2015 4:14 am

This code will change the Text body style so that the font is red and the paragraph background is green.
Code: Select all   Expand viewCollapse view
oStyleFam = ThisComponent.StyleFamilies
oParaStyles = oStyleFam.getByName("ParagraphStyles")
TextBod = oParaStyles.getByName("Text body")
TextBod.CharColor = RGB(255,0,0)
TextBod.ParaBackColor = RGB(0,255,0)
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7191
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Change certain styles background or font color

Postby DynV » Tue Jun 09, 2015 2:40 am

I got to making the macro and it runs just fine. However contrary with my experience working with Calc, I don't see the option to assign a macro to a text box in its contextual menu. Please let me know how can I do something similar in a Writer document.

Also is there a way to first extract the style, before modifying it, then apply it again? A couple of the options I intend to apply in the document is to reset the styles, usually after modifying them (but it could be while its unmodified). If I can't do that then I'd need to keep in mind to keep updating the macro resetting the style.

Thank you kindly for your help
AOO414m5 on Windows 7 Home Premium SP1. Je suis francophone.
User avatar
DynV
 
Posts: 181
Joined: Tue Apr 06, 2010 10:50 pm
Location: Montreal, Canada

Re: Change certain styles background or font color

Postby FJCC » Tue Jun 09, 2015 5:30 am

I don't know any way to modify and existing style without immediately affecting the paragraphs in the document. You can create a new style and apply it like this.
Code: Select all   Expand viewCollapse view
oStyleFam = ThisComponent.StyleFamilies
oParaStyles = oStyleFam.getByName("ParagraphStyles")
NewStyle = ThisComponent.createInstance("com.sun.star.style.ParagraphStyle")
NewStyle.CharColor = RGB(255,0,0)
NewStyle.ParaBackColor = RGB(0,255,0)
oParaStyles.insertByName("MyStyle", NewStyle)
oCurs = ThisComponent.Text.createTextCursor()
oCurs.gotoStart(False)
oCurs.gotoNextParagraph(False)
oCurs.ParaStyleName = "MyStyle"


I don't see a way to associate a macro to a text box made with the drawing tools. You can associate a macro to a form control text box.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7191
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Change certain styles background or font color

Postby DynV » Tue Jun 09, 2015 8:10 am

The form controls work nicely at running the macro, thanks again. I turned the design mode on, then created a Label Field, then in its contextual menu chose Control... popping a window entitled Properties: Label Field; then in the tab Events clicked the button associated (on the same row) with Mouse button pressed popping a window entitled Assign Action then clicking Mouse button pressed then clicking the button Macro... popping a window entitled Macro Selector; then choosing the macro; then clicked a button Ok (usually next to Cancel) whenever I could to close the windows; then finally the association between the Label Field and the macro was completed.

Still about the saving the style for later restoration: Could there be a way to either serialize (as binary) or export it (as text; hopefully XML) into something, perhaps a hidden text area/field? To, of course, reverse the process at a later date.
AOO414m5 on Windows 7 Home Premium SP1. Je suis francophone.
User avatar
DynV
 
Posts: 181
Joined: Tue Apr 06, 2010 10:50 pm
Location: Montreal, Canada

Re: [Solved] Change certain styles background or font color

Postby DynV » Tue Jun 16, 2015 12:39 am

I guess what I asked in my last post was too complicated, so I changed the OP marking the issue solved as the most important part is solved.
AOO414m5 on Windows 7 Home Premium SP1. Je suis francophone.
User avatar
DynV
 
Posts: 181
Joined: Tue Apr 06, 2010 10:50 pm
Location: Montreal, Canada


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 6 guests