Search/Replace text only when the parastyle is set to "Chord" via Macro <Solved>

Thank you for your patience This has grown into a nearly perfect solution for me. Is there any way to possibly exclude certain CHARACTER styles or search on JUST those Char styles i.e.

if parastyle(main) and charstyle(thisone) then code else other code endif

EDIT: I’m looking specifically at Superscript and Subscript formatting. I can find plenty of stuff on Bold and other formatting aids, but, precious little on Super and Subscript.

This is way how to detect the parts with different character properties in selection, probably you can remake it to Function and use oFound instead oSel. But the conditions if ParaName= AND Style<> AND Props= then will be more complicated.

Select some text that has the different formatted parts (bold, superscript, underline etc.) and run macro

Sub differentCharProperties 'show the parts that has different character properties in selection
	dim oDoc as object, oSel as object, oEnum as object, o as object, oEnum2 as object, o2 as object
	oDoc=ThisComponent
	oSel=oDoc.CurrentController.Selection.getByIndex(0)
	oEnum=oSel.createEnumeration
	do while oEnum.hasMoreElements
		o=oEnum.nextElement
		oEnum2=o.createEnumeration
		do while oEnum2.hasMoreElements
			o2=oEnum2.nextElement
			msgbox o2.string 'show part of string with different properties than other parts
			'xray o2
		loop
	loop
End Sub

There are the properties CharEscapement and CharEscapementHeight (or maybe you also use CharAutoEscapement) for Super/Sub-scriptCharEscapement has plus values for Superscript (for example 30), and minus values for Subscript (-30).

Be carefull when make the replace definitions, longer ones have to be before shorter. In your example SourceWords() = Array("Gb","G","G#", ...) → there will never be found G# because single expression G is before longer expression G# → so it will replace all G that means also all G from G#.

I’m aware of the order of precedence in search functions and have already taken care of that, i.e. altering “G#” and “Gb” before “G”.

CharEscapement does not seem to show any differences when inspecting the particular character in the watch list whilst running the code.

I have Yet to find a reference to superscript and subscript in any of the formatting code, so, even though your code shows text that changes format, it does not tell me what, exactly it is finding different. I Need to identify the difference between the number 4 and the superscripted (or subscripted) 4, or any other number in the range 1 - 7.

An overview of the project I am working on is displaying a chord chart for a song on screen and being able to change to Nashville Numbers and back, including transpositions. So far, changing from a Chord to NN works fine, it is changing from NN back into chords.

I appreciate your help, Everything is, so far, coming together beautifully.

do while oEnum2.hasMoreElements
			o2=oEnum2.nextElement
			msgbox o2.CharEscapement & chr(13) and o2.CharEscapementHeight
		loop

If it is not functional to see the changes in CharEscapement, please upload some (short) example.

Thank You for your time, I’ve had a change of thinking on my numbering so that rather than using numbers “1 - 9” if I Use roman numerals “I-IX” then the issue with superscript and subscript is passe.

I shall call this problem solved by a change of direction.
Thanks again for your help.
this is a test document showing what I hope to achieve
Untitled 1.odt (21.7 KB)

There have to be also the replacements for theoretical forms like Cb, E# etc. I added the simple condition to detect the Character Styles (Function detectCharStyle) → replacements are now only for No Character Styles.
lsemmens.odt (25.9 kB)

Roman numerals aren’t well arranged, the Widths are very different, it could be problem to write VI# or VII in monospace font.
For interest, you can look to my script for guitar (Spanish romance in ODT) Bug : Libre office WRITER looses text box color after saving - #5 by KamilLanda :slight_smile:

Fair point. Monospaced fonts will have some minor issues, far less than Proportional, it is definitely worth considering, however, I am going to persevere with this solution for now.

I shall spend some time looking at your linked thread when I have a few moments. (I’ve just won - at auction - a convertible laptop that I hope will replace an iPad for my chord charts) so, the next few days will be spent, setting that up and (hopefully) will utilise this script) If all works well, I shall, happily, share my solution with whosoever is interested.

Thank You for all your help, Just FYI here is a copy of the code I have created. It converts to any Key and even to NN. Converting from NN does work, but with the issue that chord modifiers are also Converted so any 2,4,5,6,7th chords become chords with the appropriate referred note. e.g. 4 7th converted to C would become F B. It’s a nuisance, but not a major problem.
TEST.odt (40.6 KB)

1 Like

Just an update of the code to fix an issue with one transposition and prompt to save the modified document
Happy Birthday (C).odt (17.4 KB)

@LSemmens hint Code from your makros… doesnt fly by magic into other computers meanwhile downloading your attachments :thinking:

I Don’t quite understand your question. Do you mean that it is not portable? Given that it is Libre Office Basic code, it should be portable to any system running LibreOffice, Of course the Folder/Directory conversions might need be addressed for Windows or Mac, I have not tested those.

What @karolus is saying is that your macros are not stored in the document(s) but on your computer. Thus we don’t see them.

1 Like

Thanks for that, it occurred to me after I replied that this might be the case. I’ve now fixed that.
Happy Birthday (C).odt (27.8 KB)try again! Sorry folks!

Really interesting and really good 1st prototype :slight_smile:.
a) I deleted unnecessary empty Else before End If

Else
Endif


b) undoMgr.leaveUndoContext is moved to the end (before Call CloseAllDocs) to get really one step in UndoManager for changing the Key

c) added new Key to the name of saved file in CloseAllDocs

d) added changing the part of chord to superscript (for example G7 → 7 is changed to Superscript if it isn’t), there is comment rem change next part of chord to Superscript
Happy Birthday-edit-kl1.odt (28.5 kB)


Few tips:

  1. maybe select case instead the series of if … elseif … elseif
  2. maybe array( array(..i), array(...)) and traverse the array instead the series IF tDest = "..." then tDest = "..."
  3. The part of strings "Key = " is redundant, it is possible to use the arrays without one.
  4. Add the control for inputed Key? See what it does for invalid Key like Fb, E# or mistaked Dx (I use AltGr+X for # and I pressed badly AltGr so I got x instead #) etc.
  5. Must there be gotos?
	If chordChange = "Y" then 
	   chordChange = "Z"
	   goto SetKey
	end if
	If chordChange = "A" then goto  ChangeB
    IF chordChange = "B" then goto SetKey

For me, the Keys are too much complicated and unmemorable, so I don’t know the transformations of Keys. But with gotos it seems there are some “doubled” operations, is it indispensable?


If I understand well, the chords aren’t mixed with text, so it means the chords are in single paragraphs. So it is possible to get raw text from the chords paragraphs, and work with this raw string instead Visible Cursor → there will not be the comparison of Regions in searching. Then rewrite the paragraph with changed string + set Character Style for Superscript parts of chords.

Thank you for that tidy up. My initial attempt was to use a Case statement, but for some reason I could not get it to accept a string as the condition, hence the if statements. Some of the operations were doubled because in correcting one item, it introduced another issue, by repeating, it seemed to have sorted it. When I have the time, (and inclination) I will attempt to tidy it all up AND add comments.
Much of my code is C&P from elsewhere and I have yet to find a comprehensive way of determining what each command does/is. i.e. much of the online documentation explains the syntax, but not the effect. I learn by modifying existing code and studying the command structure. (my background has been procedural languages from last century).

edit: Yes, you are correct, the chords reside in their own paragraphs (hence the need for a monospaced font.)

Assuming youre use true superscript-figures¹²³⁴⁵⁶⁷⁸⁹ instead superscript-Formatting:

import re

repl_dict = {"Ab": "1", "A#": "2", "A": "1#", 
			 "Bb": "2", "B#": "3#", "B": "2", 
			 "Cb": "2#" "C#": "4", "C": "3", 
			 "Db": "4", "D#": "5#", "D": "5", 
			 "Eb": "5#", "E#": "6#", "E": "6", 
			 "Fb": "6", "F#": "7", "F#": "6#", 
			 "Gb": "7", "G#": "1", "G": "1b"}

rex = re.compile(r'|'.join(repl_dict))

def chord_transpose( some_string ):    
    def repl(match):
        return repl_dict[match.group(0)]    
    return rex.sub(repl, some_string )

def main(*_):
    doc = XSCRIPTCONTEXT.getDocument()
    for para in doc.Text:
        if para.ParaStyleName == "Chord":
            para.String = chord_transpose(para.String)
Superscript Figures Unicode Hexadecimal
¹ B9
² B2
³ B3
2074
2075
2076
2077
2078
2079

no it’s just superscript formatting. might have to develop code to convert those to true superscript.
Good thinking! It shouldn’t be too hard to add a few extra lines of code to the existing macro.

No, you should write directly superscripts into your Writer-Doc

@karolus not all fonts has Superscripts Figures :frowning:; and there also could be very complicated names of chords like Caug, Dsus2, Fdim7, Hmib5


source of image: https://www.all-guitar-chords.com

And whats the point of your reply?