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

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?

That there can be easily bigger chaos than fulfilled expectations :slight_smile:
superscript-in-chords.odt (15.3 kB)

Names of chords are varied in different countries (not only B vs H, Bb vs B), and next classical problem is how to write fingering that isn’t named as some chord → for example add the fingering diagram and write X1, X2, X3 etc.?