[Solved] Writer macro to change text colour inside brackets

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
parking
Posts: 11
Joined: Sat Feb 22, 2014 11:19 pm

[Solved] Writer macro to change text colour inside brackets

Post by parking »

I have a macro to change text within, and including, square brackets to RED, Arial, as shown below

RDescrip = ThisComponent.createReplaceDescriptor
RDescrip.searchRegularExpression = True
RDescrip.searchString = "\[([^\]]*)\]"
rngs = ThisComponent.findAll(RDescrip)
for i = 0 to rngs.count-1
foundText = rngs.getByIndex(i)
foundText.CharColor = RGB(255,0,0)
foundText.CharFontName = "Arial"
foundText.CharHeight = 14
foundText.CharWeight = 150
next i

I would now like to add a second macro to change text within conventional brackets i.e. () to Brown text (255,160,0)... but I don't know how to change this line to do so
RDescrip.searchString = "\[([^\]]*)\]"

Can anybody help me please?
Last edited by MrProgrammer on Tue Aug 09, 2022 3:52 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
parking
Open Office 4.0.1 on Windows 7
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: writer macro to change text colour inside brackets

Post by karolus »

Hallo

Code: Select all

RDescrip.searchString = "\(([^()]*)\)"
Instead Direct-Formattting you should assign a CharStyle with these Attributes

Code: Select all

FoundText.CharStyleName = your_charstyle_with_red_bold_arial
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
MrProgrammer
Moderator
Posts: 4894
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Writer macro to change text colour inside brackets

Post by MrProgrammer »

parking wrote: Tue Aug 02, 2022 1:45 pm I don't know how to change this line: RDescrip.searchString = "\[([^\]]*)\]"
Your question suggests that you do not understand regular expressions. Basic information is in Regular Expressions in Writer. You also need to understand styles. These are very important features of OpenOffice which will aid you in using the full capabilities of the program.

Once you understand regular expressions and styles, you don't need others to write macros for you to perform the tasks you've asked about. You can record a macro yourself. You don't need to understand any programming to record macros. You need to create character styles, say Brackets and Parens, to set the colors you want for the text. Then the steps you record are:
• Edit → Find and Replace → More options → Check Regular Expressions → Uncheck Current Selection Only
→ Search for → \[[^]]+\] → Find All
• Format → Styles and Formatting → Character Styles (second icon) → Double-click Brackets
• Edit → Find and Replace → More options → Check Regular Expressions → Uncheck Current Selection Only
→ Search for → \([^)]+\) → Find All
• Format → Styles and Formatting → Character Styles (second icon) → Double-click Parens
• Stop recording, then assign a name for your macro
Second icon is Character Styles
Second icon is Character Styles
CharStyles.gif (49.88 KiB) Viewed 816 times

You can record one macro to perform both tasks, or you can have separate macros for brackets and parentheses.

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the subject field. Select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
parking
Posts: 11
Joined: Sat Feb 22, 2014 11:19 pm

Re: Writer macro to change text colour inside brackets

Post by parking »

Thank you both for your help. I will investigate this evening. You are correct, I don't understand enough about what I am doing, but I'm also not using this activity enough to spend many hours learning . I did hope I would get a simple rearrangement of the expression I sent, but it seems I may have to get into it a bit more.
Thank you again
parking
Open Office 4.0.1 on Windows 7
Post Reply