Macro with ReferenceMarks does not work in footnotes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
teertinker
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Macro with ReferenceMarks does not work in footnotes

Post 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

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
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
teertinker
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post by teertinker »

Dear Jeje,
thanks a lot! I will try your suggestions today and let you know about the results.
Openoffice 3.1
teertinker
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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 ... tnote-text and tried

Code: Select all

  oText = ThisComponent.gettext()
but this didn't change anything.

Code: Select all

'''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
Openoffice 3.1
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post by JeJe »

Try

Code: Select all

 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)

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
teertinker
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post by teertinker »

Yuppie! You saved my day! Wonderful it works - even though I don't understand the logic behind it.
Openoffice 3.1
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
teertinker
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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.
Openoffice 3.1
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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

    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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
teertinker
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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.
Openoffice 3.1
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
teertinker
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post by teertinker »

The document should include the macro.

https://www.dropbox.com/scl/fi/227ftkom ... syqoj46gof
Openoffice 3.1
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post by JeJe »

Try:

Code: Select all

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 


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
teertinker
Posts: 8
Joined: Wed Oct 14, 2020 3:55 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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).
Openoffice 3.1
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro with ReferenceMarks does not work in footnotes

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply