[Solved] Minor issue with copying content from file to file

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
espinosa_cz
Posts: 10
Joined: Thu Aug 20, 2015 4:15 am

[Solved] Minor issue with copying content from file to file

Post by espinosa_cz »

Hello everyone,
After tough fight and lots of experimentation I figured out how to copy a selection from one document to another, copy everything - tables, images, text style, everything preserved. Not like that dummy example in wiki where only bare text is copied over.
It works nearly as desired, only one exception - the original document does not return to original "view", it hangs at the very bottom of the document, on the last page. I can return view cursor back to original position, that works, but the view does not follow the view cursor. Pressing like arrow keys brings back view cursor back into active view, back into focus.
You need at least two page document to notice the issue.

Code: Select all

'''
''' Copy content from one document to another document; 
''' select all and copy, open new blank unnamed document and paste all the selected content from first document
''' Copy everything, images, tables, text styles; rich text copy 
'''
Sub CopyContentToNewFile
	dim c1, c2, newDoc, selectedContent, origPosition, origPositionTC
	
	origDocument = ThisComponent
	
	' preserve original position, when working with controller selection otherwise it gets lost
	origPosition = origDocument.CurrentController.getViewCursor() 
	origPositionTC = origDocument.getText.createTextCursorByRange(origPosition)
 
    ' select all in original document
	c1 = origDocument.text.createTextCursor
	c1.gotoStart(false)	
	c1.gotoEnd(true)
	
	' copy selected from original document
	origDocument.CurrentController.select(c1)            ' select content of the whole document, from the start to the end; here cursor represents whole text range, selected content of a document 
	selectedContent = origDocument.CurrentController.getTransferable() ' copy the content for later use 
		
	' create new blank document
	newDoc = StarDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, Array())	'Open blank Writer doc
	
	' paste selected to the new document, on cursor position
	c2 = newDoc.text.createTextCursor ' 
	c2.gotoStart(false)
	newDoc.CurrentController.select(c2)  ' set insertion point, represented by cursor C2, in the new file at the document start; here cursor represents pointer to a position, not a range
	newDoc.CurrentController.insertTransferable(selectedContent)
	
	' deselect current selection; current selection is whole document by now and it is visible selection
	' return cursor to original position; and selection to original selection if any was present before running macro	
	origDocument.CurrentController.select(origPositionTC)
	' ..problem is, although cursor returns to the original position Writer view is stuck on the last page. 
End Sub
Please find example document attached.
Macro is run from main menu "Macro -> CopyContentToNewFile"
Attachments
CopyContent2.odt
(18.85 KiB) Downloaded 208 times
Last edited by espinosa_cz on Sun Aug 23, 2015 7:23 pm, edited 1 time in total.
Libre Office 4.4 / AOO 4.1 / Windows 8 & OpenSuse
FJCC
Moderator
Posts: 9635
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Minor issue with copying content from one file to anothe

Post by FJCC »

Try adding

Code: Select all

origDocument.lockControllers()
near the top of the code and

Code: Select all

origDocument.unlockControllers(
near the bottom.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
espinosa_cz
Posts: 10
Joined: Thu Aug 20, 2015 4:15 am

[SOLVED] Minor issue with copying content from one file to a

Post by espinosa_cz »

Thank you FJCC!
Works perfectly!

Final form:

Code: Select all

'''
''' Copy content from one document to another document; 
''' select all and copy, open new blank unnamed document and paste all the selected content from first document
''' Copy everything, images, tables, text styles; rich text copy 
'''
Sub CopyContentToNewFile
	dim c1, c2, newDoc, selectedContent, origPosition, origPositionTC
	
	origDocument = ThisComponent
	
	origDocument.lockControllers()
	
	' preserve original position, when working with controller selection otherwise it gets lost
	origPosition = origDocument.CurrentController.getViewCursor() 
	origPositionTC = origDocument.getText.createTextCursorByRange(origPosition)
 
    ' select all in original document
	c1 = origDocument.text.createTextCursor
	c1.gotoStart(false)	
	c1.gotoEnd(true)
	
	' copy selected from original document
	origDocument.CurrentController.select(c1)            ' select content of the whole document, from the start to the end; here cursor represents whole text range, selected content of a document 
	selectedContent = origDocument.CurrentController.getTransferable() ' copy the content for later use 
		
	' create new blank document
	newDoc = StarDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, Array())	'Open blank Writer doc
	
	' paste selected to the new document, on cursor position
	c2 = newDoc.text.createTextCursor ' 
	c2.gotoStart(false)
	newDoc.CurrentController.select(c2)  ' set insertion point, represented by cursor C2, in the new file at the document start; here cursor represents pointer to a position, not a range
	newDoc.CurrentController.insertTransferable(selectedContent)
	
	' deselect current selection; current selection is whole document by now and it is visible selection
	' return cursor to original position; and selection to original selection if any was present before running macro	
	origDocument.CurrentController.select(origPositionTC) 
	
	origDocument.unlockControllers()
End Sub
Libre Office 4.4 / AOO 4.1 / Windows 8 & OpenSuse
espinosa_cz
Posts: 10
Joined: Thu Aug 20, 2015 4:15 am

Re: Minor issue with copying content from one file to anothe

Post by espinosa_cz »

In the meantime, I found another, alternative, solution.

The trick is to move command to deselect content in original file, this one line

Code: Select all

origDocument.CurrentController.select(origPositionTC)
to BEFORE the new document is open.
This indicates, that 'selectedContent', the transferable, is independent on actual selection once it is made.
(It would be interesting to find out how many selected contents, transferables, can one make? Would another selection, transferable, invalidated the previous already saved one?)

Full code:

Code: Select all

'''
''' Copy whole document; open new blank unnamed document and paste all the selected content from first document
''' Including FIX for 'last minor issue with original document not returning back to view cursor'
Sub CopyContentToNewFile
	dim c1, c2, newDoc, selectedContent, origPosition, origPositionTC
	
	origDocument = ThisComponent
	
	' preserve original position, when working with controller selection it gets otherwise lost
	origPosition = origDocument.CurrentController.getViewCursor() 
	origPositionTC = origDocument.getText.createTextCursorByRange(origPosition)
 
        ' select all in original document
	c1 = origDocument.text.createTextCursor
	c1.gotoStart(false)	
	c1.gotoEnd(true)
	
	' copy selected from original document
	origDocument.CurrentController.select(c1)            ' select content of the whole document, from the start to the end; here cursor represents whole text range, selected content of a document 
	selectedContent = origDocument.CurrentController.getTransferable() ' copy the content for later use 
	
	' deselect selection
	' set view curson in the original document at the beginning of document and that also deselect selection
	' It is important to do it BEFORE new document is open (looks like a bug in LO)
	origDocument.CurrentController.select(origPositionTC)
		
	' create new blank document
	newDoc = StarDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, Array())	'Open blank Writer doc
	
	' paste selected to the new document, on cursor position
	c2 = newDoc.text.createTextCursor ' 
	c2.gotoStart(false)
	newDoc.CurrentController.select(c2)  ' set insertion point, represented by cursor C2, in the new file at the document start; here cursor represents pointer to a position, not a range
	newDoc.CurrentController.insertTransferable(selectedContent)
	
	' set view curson in the new document at the beginning of document
	' not essential, feels more 'intuitive' behaviour
	newDoc.currentController.viewCursor.gotoStart(false)
End Sub
But I think FJCC's solution is more general and therefore preferred.

Interestingly solution from to Andrew Pitonyak, in his book, he mentions there a similar issue, but his solution:

Code: Select all

origDocument.CurrentController.Frame.ContainerWindow.setFocus
did not work for me.
Libre Office 4.4 / AOO 4.1 / Windows 8 & OpenSuse
Post Reply