[Solved] Change text colour inside a macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
TonyRolfe
Posts: 22
Joined: Wed Jun 27, 2018 8:38 am
Location: NSW South Coast

[Solved] Change text colour inside a macro

Post by TonyRolfe »

I am trying to write 4 simple macros, one each to insert a Club, diamond, heart and spade symbol and to ensure that club and spade symbols show as black while heart and diamond symbols show as red.

I recorded the macros by Tools>Macros>Record macro, inserting the special character, setting the font colour and highlighting the character then stopping recording. This works when I run the macro as the character appears in the correct colour. However, the colour change doesn't stop with the character. Any subsequent characters continue in red or black. So, could someone please tell me how do I set the colour back to whatever it was? I could add a space following the symbol and set that to Automatic, but I don't always want a space following the character.

Thanks

Tony
Last edited by MrProgrammer on Sat Aug 08, 2020 3:26 am, edited 2 times in total.
Reason: Added ✓ icon
Open Office 4.1.5
Windows 10 Home
User avatar
Zizi64
Volunteer
Posts: 11362
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change text colour inside a macro

Post by Zizi64 »

Where you want to insert the colorized characters? Into a Writer document? Into a Cals spreadsheet?

The recorded macros never will be so efficient. Write your macros (vased on the API functions) instead of the recording.
If it is a Writer document, then you can apply a user-defined Character style to the inserted character - by the macro.

The Character style feature not exist in the Spreadsheet documents.


Please upload your macro here.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11362
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change text colour inside a macro

Post by Zizi64 »

How to colorize characters in a Calc cell by API functions:

https://forum-test.openoffice.org/en/fo ... 45&t=97884
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
TonyRolfe
Posts: 22
Joined: Wed Jun 27, 2018 8:38 am
Location: NSW South Coast

Re: Change text colour inside a macro

Post by TonyRolfe »

I want the macros to operate in both a Writer document and a Presentation.

I am writing documentation and lessons for contract bridge and need to be able to enter the symbols as efficiently as possible.

Thanks

Tony
Open Office 4.1.5
Windows 10 Home
User avatar
Zizi64
Volunteer
Posts: 11362
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change text colour inside a macro

Post by Zizi64 »

And you can use the Autocorrect function with colorized character (unchecking the "Text only" parameter of the function).

In this case you not need any user defined macros.

for example:


Replace -> With
*D ->
*H ->



The Autocorrect function has a new feature in the LibreOffice: you can insert characters by the AutoCorrect without before/after spaces of the "Replace" code
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
TonyRolfe
Posts: 22
Joined: Wed Jun 27, 2018 8:38 am
Location: NSW South Coast

Re: Change text colour inside a macro

Post by TonyRolfe »

Thank you, that would be a much better approach. However, the Text Only checkbox is ticked and greyed out, so I can't untick it. I can only get the symbols in black.

While I am here, could you please advise how I can set my default language for everything to English (Australia). I can change it on the fly, but it keeps reverting to English (USA).
Open Office 4.1.5
Windows 10 Home
User avatar
Zizi64
Volunteer
Posts: 11362
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change text colour inside a macro

Post by Zizi64 »

However, the Text Only checkbox is ticked and greyed out, so I can't untick it. I can only get the symbols in black.
It works for me in my LibreOffice, when I put a formatted character from the Writer into the "With" box of the Autocorrect function. The pasted character still black in the "With" box, but it has a color property, and the function will insert a colorized character.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Change text colour inside a macro

Post by JeJe »

Tools menu/Options/Language Settings for setting the default

Would Autotext work here for what you want to do?

Select each colored character with the following text set the automatic again and create your autotext from those (edit menu/autotext)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
TonyRolfe
Posts: 22
Joined: Wed Jun 27, 2018 8:38 am
Location: NSW South Coast

Re: Change text colour inside a macro

Post by TonyRolfe »

I've tried, in autocorrect, to replace !H with the heart symbol. It appears in the autocorrect list but when I type !H it doesn't convert, it just appears as !H. I guess I'm missing a "Switch autocorrect on" setting, but I can't find it.

It's getting late here now, so I'll have another look in the morning.

Thanks for all your help so far.

Tony
Open Office 4.1.5
Windows 10 Home
User avatar
robleyd
Moderator
Posts: 5087
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Change text colour inside a macro

Post by robleyd »

1) You should have your autocorrect setting such that autocorrection is enabled when typing; Tools | Autocorrect Options and select the Options tab; see the highlighted section in the image. You may also want the option to replace while modifying existing text.
oo_autocorr.png
2) You need to type the shortcut followed by a space for the autocorrect to happen.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
User avatar
Zizi64
Volunteer
Posts: 11362
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change text colour inside a macro

Post by Zizi64 »

I've tried, in autocorrect, to replace !H with the heart symbol. It appears in the autocorrect list but when I type !H it doesn't convert, it just appears as !H. I guess I'm missing a "Switch autocorrect on" setting, but I can't find it.
Additional information:
The function only work properly in the Apache OpenOffice, when the code seems as a standalone word:

Code: Select all

{paragraph beginning}!H{space}
{space}!H{space}
{space}!H{sentence end character}
{space}!H{line end mark}
{space}!H{paragraph end mark}
(The LibreOffice has an improved function: you can substitute a code even inside a word.)
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
MrProgrammer
Moderator
Posts: 4907
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Change text colour inside a macro

Post by MrProgrammer »

TonyRolfe wrote:I am trying to write 4 simple macros, one each to insert a Club, diamond, heart and spade symbol and to ensure that club and spade symbols show as black while heart and diamond symbols show as red.
Here is a similar topic, with a non-macro solution for Writer.
Creating Card Suit Symbol Shortcuts in OpenOffice For Bridge
For Impress, perhaps one should create the text in Writer and paste into the presentation.
TonyRolfe wrote:I recorded the macros by Tools>Macros>Record macro, inserting the special character, setting the font colour and highlighting the character then stopping recording. This works when I run the macro as the character appears in the correct colour. However, the colour change doesn't stop with the character. Any subsequent characters continue in red or black.
Of course. This is how text works in OpenOffice.
TonyRolfe wrote:I could add a space following the symbol and set that to Automatic, but I don't always want a space following the character.
Thin space

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can 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).
TonyRolfe
Posts: 22
Joined: Wed Jun 27, 2018 8:38 am
Location: NSW South Coast

Re: Change text colour inside a macro

Post by TonyRolfe »

The thin space idea sounds good. However, I can't see how to add one in Writer. I've tried the unicode 2009 (Ctrl-Shift-u2009) but that doesn't do anything. I've also tried various combinations of ctrl, shift, alt space, but they all seem to do other things. I've looked at the various options in Insert > special Character but can't find the thin space character

I'm running Windows 10, if that is relevant.
Open Office 4.1.5
Windows 10 Home
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Change text colour inside a macro

Post by JeJe »

This will insert a red diamond where the caret is and the following text will be black so long as its typed straight afterwards.

Code: Select all

Sub RedDiamond
vc = thiscomponent.currentcontroller.viewcursor
vc.string = "♦"
vc.charcolor = rgb(255,0,0)
vc.collapsetoend
vc.charcolor = 0
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
MrProgrammer
Moderator
Posts: 4907
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Change text colour inside a macro

Post by MrProgrammer »

TonyRolfe wrote:I've looked at the various options in Insert > special Character but can't find the thin space character
From the Insert → Special Character dialog you will find the suits in the Miscellaneous Symbols subset. Clicking the heart, for example, puts it in the list of characters. Then in the General punctuation subset clicking the thin space (U+2009) adds it to the list of characters. It is hard to tell which of the spacing characters in the row is the thin space, however it is the one above the Euro sign.
Insert Characters copy.jpg
If you click the wrong spacing character (not U+2009) in the dialog by mistake, you should start over since it will be added to the list where it is invisible. After adding both characters to the list, the OK button inserts them in the text document. Then you can format just the heart with a red font and create your AutoText. The thin space will still have the Automatic font color so characters following your AutoText do not take the red font. You won't need an evil macro, just standard Writer features. It is best to use standard features so you are not dependent on others to create macros for you.

Once created, the AutoText is available in any document. Just type the shortcut and press F3. Read about AutoText in Help → Index or in User Guides (PDF) or searching for topics about it in the Writer Forum.

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can 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).
TonyRolfe
Posts: 22
Joined: Wed Jun 27, 2018 8:38 am
Location: NSW South Coast

Re: [Solved]Change text colour inside a macro

Post by TonyRolfe »

Many thanks to all who replied. I now have a number of ways to do this. I've added [Solved] to the header.

Tony
Open Office 4.1.5
Windows 10 Home
Post Reply