Page 1 of 1

Macro with ReferenceMarks does not work in footnotes

PostPosted: Wed Oct 14, 2020 4:05 pm
by teertinker
Dear Community,
I have build a small macro that tries to convert referencemarks into a text code. The sub routine works quite ok at the moment. My Problem is, that all reference marks in footnotes won't be converted. Instead OpenOffice throws an exception at the "oText.insertString(oViewCursor, "\cite{" +temp + "}", false)" line. Has anyone a tip for me?

Code: Select all   Expand viewCollapse view
Sub FromReferenceToText

dim temp as string
Dim Pos As Long
Dim oText As Object
Dim oViewCursor As Object
Dim i As Integer, oRefmarks, oRefmarkNames()

oText = ThisComponent.Text

oRefmarks = ThisComponent.getReferenceMarks()
oRefmarkNames = oRefmarks.getElementNames()

For i = 0 To Ubound( oRefmarkNames )
If instr(oRefmarkNames(i), "JR_cite") Then

oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()


oRefmarks.getByName( oRefmarkNames( i ) ).dispose()
temp=oRefmarkNames(i)
    if left(temp,7)="JR_cite" then
    temp = mid(temp,8)

    end if
oText.insertString(oViewCursor, "\cite{" +temp + "}", false)

End if
Next
End Sub

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Wed Oct 14, 2020 9:58 pm
by JeJe
Move the dispose line to after that one.

Edit: if that doesn't work run the for-next a second time and do the dispose then (not in the first for-next). Or try reversing the order of the for-next and go backwards from the highest to the lowest index, step -1.

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Thu Oct 15, 2020 11:20 am
by teertinker
Dear Jeje,
thanks a lot! I will try your suggestions today and let you know about the results.

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Thu Oct 15, 2020 1:14 pm
by teertinker
Dear Jeje,
I followed your suggestion and did a second for-next loop. This did not exactly solve my problem, but directs me closer to the error. If I run only the second loop (dispose) every reference mark gets deleted properly (including footnotes). The error is already thrown in the first loop when I try to insert the string into the footnote. I found another discussion about accessing footnotes in https://superuser.com/questions/1289749/libreoffice-macro-access-footnote-text and tried
Code: Select all   Expand viewCollapse view
  oText = ThisComponent.gettext()

but this didn't change anything.

Code: Select all   Expand viewCollapse view
'''first loop
Dim a as integer
dim temp as string
Dim Pos As Long
Dim oText As Object
Dim oViewCursor As Object
Dim i As Integer, oRefmarks, oRefmarkNames()

  oViewCursor = ThisComponent.GetCurrentController.ViewCursor
  oText = ThisComponent.text

For i = 0 To Ubound( oRefmarkNames )
If instr(oRefmarkNames(i), "JR_cite") Then

  oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()
  temp=oRefmarkNames(i)
   if left(temp,7)="JR_cite" then
       temp = mid(temp,8)
   end if
oText.insertString(oViewCursor, "\cite{" +temp + "}", false)

End if
Next


'''second loop
For i = 0 To Ubound( oRefmarkNames )
If instr(oRefmarkNames(i), "JR_cite") Then

  'oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()

  oRefmarks.getByName( oRefmarkNames( i ) ).dispose()
End if
Next

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Thu Oct 15, 2020 1:45 pm
by JeJe
Try

Code: Select all   Expand viewCollapse view
oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()
  temp=oRefmarkNames(i)
   if left(temp,7)="JR_cite" then
       temp = mid(temp,8)
   end if
oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor().getText.insertString(oViewCursor, "\cite{" +temp + "}", false)


Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Thu Oct 15, 2020 1:52 pm
by teertinker
Yuppie! You saved my day! Wonderful it works - even though I don't understand the logic behind it.

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Thu Oct 15, 2020 1:59 pm
by JeJe
Different parts of the document have their own text. The main document has its text. But each text frame also has its own separate text. A table consists of cells and each cell has its own text. You have to use the correct text.

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Thu Oct 15, 2020 10:17 pm
by teertinker
The code still has a small "hickup". I moved the dispose command below the insert command as suggested in your first comment. That works well for all kinds of ReferenceMarks.
However, if the referencemark is at the beginning of the paragraph, the inserted text becomes part of the referencemark and will be disposed as well. Do you have an idea how I can prevent this behavior?

If I call the dispose command first, there is the problem that reference marks without text create an exception when the insert command is called.

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Thu Oct 15, 2020 10:34 pm
by JeJe
Instead of

oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor()
try

oViewCursor= oRefmarks.getByName( oRefmarkNames( i ) ).getAnchor().getstart

Or instead of insert string you can do


Code: Select all   Expand viewCollapse view
    ref = oRefmarkNames(i)
    If instr(ref, "JR_cite") Then

    oViewCursor= oRefmarks.getByName( ref ).getAnchor().getstart

       temp=ref
        if left(temp,7)="JR_cite" then
        temp = mid(temp,8)

        end if
   oViewCursor.string = "\cite{" +temp + "}"
    oRefmarks.getByName( ref ).dispose()
    End if


Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Fri Oct 16, 2020 1:48 pm
by teertinker
I had also the idea to use getstart. Unfortunately it does not work. The string will still become part of the field and thus gets disposed.

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Fri Oct 16, 2020 2:16 pm
by JeJe
Worked in the simple one instance test I created. If you post your document stripped of any sensitive information (and with the code) then people can look at it.

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Fri Oct 16, 2020 2:30 pm
by teertinker
The document should include the macro.

https://www.dropbox.com/scl/fi/227ftkom ... syqoj46gof

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Fri Oct 16, 2020 3:01 pm
by JeJe
Try:

Code: Select all   Expand viewCollapse view
REM  *****  BASIC  *****

Sub FromReferenceToText
dim temp as string
Dim oText As Object
Dim i As Integer, oRefmarks, oRefmarkNames()

  oRefmarks = ThisComponent.getReferenceMarks()
  oRefmarkNames = oRefmarks.getElementNames()

For i = 0 To Ubound( oRefmarkNames )
ref = oRefmarkNames(i)
If instr(ref, "JR_cite") Then
  temp=ref
   if left(temp,7)="JR_cite" then
       temp = mid(temp,8)
   end if
anchor = oRefmarks.getByName( ref).getanchor
rr= anchor.gettext.createtextcursorbyrange(anchor)
rr.string="\cite{" +temp + "}"
End if
Next


End Sub



Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Sat Oct 17, 2020 10:51 am
by teertinker
Wonderful! It works like a charm now. Thank you so much. I hope its ok to include your suggestion into my extension (https://github.com/teertinker/JabRef_Li ... _Converter).

Re: Macro with ReferenceMarks does not work in footnotes

PostPosted: Sat Oct 17, 2020 1:18 pm
by JeJe
I hope its ok to include your suggestion into my extension (https://github.com/teertinker/JabRef_Li ... _Converter).


Of course - however you like.