Put a string into the clipboard

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ghe
Posts: 23
Joined: Wed May 19, 2010 5:32 pm

Put a string into the clipboard

Post by ghe »

Hi all,

Is there a simple way to put a string into the clipboard ? In Microsoft's VBA, three simple lines are enough to do it:

Code: Select all

Set d = New DataObject
d.SetText (message)
d.PutInClipboard
I can't find a way to do it in OO Basic (except for constant strings, which is obviously mostly useless).

Thanks in advance,

--ghe
OpenOffice 3.2 on Debian Testing
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Put a string into the clipboard

Post by Charlie Young »

Hi ghe. Yes, you got me looking at this one. Is the method you found similar to this? Note that I'm using a Global for the string, which is the only way I've found to use anything but a constant. I'm certainly doubtful that this is bombproof, and I think you'll sympathize with my choice of literals.

This has been discussed before, my searching indicates, but it would seem to be worth coming back to.

Code: Select all

Global ClipString As String

Sub Clipper
	Dim cBoard As Object
	Dim cTrans As Object
	Dim null As Object
		
	cBoard = createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
	cTrans = createUnoListener("TR_", "com.sun.star.datatransfer.XTransferable") 	
	ClipString = "Now this is ugly!"
	cBoard.setContents(cTrans, null)
End Sub

function TR_getTransferData( aFlavor as com.sun.star.datatransfer.DataFlavor )
	if (aFlavor.MimeType = "text/plain;charset=utf-16") then
  		TR_getTransferData = ClipString 
  	endif
end function
 
function TR_getTransferDataFlavors()
	Dim aF as new com.sun.star.datatransfer.DataFlavor
	aF.MimeType = "text/plain;charset=utf-16"
	aF.HumanPresentableName = "Unicode-Text"
	TR_getTransferDataFlavors = Array(aF)
end function
 
function TR_isDataFlavorSupported(aFlavor as com.sun.star.datatransfer.DataFlavor) as Boolean
	TR_isDataFlavorSupported = (aFlavor.MimeType = "text/plain;charset=utf-16")
end function
I also must say it's anything but simple!
Apache OpenOffice 4.1.1
Windows XP
ghe
Posts: 23
Joined: Wed May 19, 2010 5:32 pm

Re: Put a string into the clipboard

Post by ghe »

Thank you again, Charlie!

I started from the same base, but did not have the idea to assign the global variable inside of putStringInClipboard...
I modified your code sligthly (to make it work with an argument), and it seems to work (it's really black magic for me):

Code: Select all

global clipboard_string as string

sub putStringInClipboard (byval s as string)
  dim sc, xt, null as object

  sc = createUnoService ("com.sun.star.datatransfer.clipboard.SystemClipboard")
  xt = createUnoListener ("clipboard_transferable_", "com.sun.star.datatransfer.XTransferable")    
  clipboard_string = s
  sc.setcontents (xt, null)
end sub

function clipboard_transferable_getTransferData (df as com.sun.star.datatransfer.DataFlavor)
  if (df.mimetype = "text/plain;charset=utf-16") then
    clipboard_transferable_getTransferData =  clipboard_string
  end if
end function

function clipboard_transferable_getTransferDataFlavors ()
  dim df as new com.sun.star.datatransfer.DataFlavor
  df.mimetype = "text/plain;charset=utf-16"
  df.humanpresentablename = "Unicode-Text"
  clipboard_transferable_getTransferDataFlavors = array (df)
end function

function clipboard_transferable_isDataFlavorSupported (df as com.sun.star.datatransfer.DataFlavor) as boolean
  clipboard_transferable_isDataFlavorSupported = (df.mimetype = "text/plain;charset=utf-16")
end function
I have two more questions:

1. Is that code portable? It works at least with OOo 3.0, 3.1 and 3.2, but is it guaranteed to work with OOo 3.3, etc. ?

2. Do you also observe crashes when you close your the document? I open it from the terminal, and get 'terminate called after throwing an instance of 'com::sun::star::uno::RuntimeException'' errors when I close it (and sometimes a "Document Recovery" dialog opens, with no files in the list). Since it does not happen when the macro putStringInClipboard has not been called, I guess it has something to do with the UnoListener created. I have observed that after its creation, the functions clipboard_transferable_getTransferDataFlavors and clipboard_transferable_isDataFlavorSupported are called every 2-3 seconds (the function clipboard_transferable_getTransferData is only called when the data is actually pasted), and I suppose one of those is responsible of an error during OOo's "shutdown". This happens both under 3.1 and 3.2.

--ghe
OpenOffice 3.2 on Debian Testing
ghe
Posts: 23
Joined: Wed May 19, 2010 5:32 pm

Re: Put a string into the clipboard

Post by ghe »

Replying to my own question #2, it seems that my impression is correct; I get the following backtrace under gdb (when closing the document after having run putStringInClipboard):

Code: Select all

#0  0x00007ffff6ca7f45 in raise () from /lib/libc.so.6
#1  0x00007ffff6caad80 in abort () from /lib/libc.so.6
#2  0x00007ffff752ed45 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/libstdc++.so.6
#3  0x00007ffff752d176 in ?? () from /usr/lib/libstdc++.so.6
#4  0x00007ffff752d1a3 in std::terminate() () from /usr/lib/libstdc++.so.6
#5  0x00007ffff752d29e in __cxa_throw () from /usr/lib/libstdc++.so.6
#6  0x00007fffebbef6a0 in gcc3::raiseException () from /usr/lib/ure/lib/libgcc3_uno.so
#7  0x00007fffebbf1283 in cpp2uno_call () from /usr/lib/ure/lib/libgcc3_uno.so
#8  0x00007fffebbf1941 in cpp_vtable_call () from /usr/lib/ure/lib/libgcc3_uno.so
#9  0x00007fffebbf2ff2 in privateSnippetExecutor () from /usr/lib/ure/lib/libgcc3_uno.so
#10 0x00007fffee8728ce in x11::SelectionManager::handleSelectionRequest () from /usr/lib/openoffice/basis3.2/program/libvclplug_genlx.so
#11 0x00007fffee873a15 in x11::SelectionManager::handleXEvent () from /usr/lib/openoffice/basis3.2/program/libvclplug_genlx.so
#12 0x00007fffee873f1b in x11::SelectionManager::dispatchEvent () from /usr/lib/openoffice/basis3.2/program/libvclplug_genlx.so
#13 0x00007fffee873fb6 in x11::SelectionManager::run () from /usr/lib/openoffice/basis3.2/program/libvclplug_genlx.so
#14 0x00007ffff7a1e76c in osl_thread_start_Impl () from /usr/lib/openoffice/program/../basis-link/ure-link/lib/libuno_sal.so.3
#15 0x00007ffff641973a in start_thread () from /lib/libpthread.so.0
#16 0x00007ffff6d4169d in clone () from /lib/libc.so.6
#17 0x0000000000000000 in ?? ()
This backtrace has been produced with Debian's OOo 3.2.0, but I get a similar backtrace with the "official" OOo 3.2.0. If I understand things correctly, an (asynchronous) event is generated by OOo while it is terminating, but can't be handled correctly...

--ghe
OpenOffice 3.2 on Debian Testing
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Put a string into the clipboard

Post by Charlie Young »

I'm using your version of the code now. I had not gotten any crashes, until yesterday, when I tried something dubious with the listener, which I agree is the problem. If I run the code from

Code: Select all

Sub Main
	putStringInClipBoard("Hi There")
End Sub
then try pasting into the spreadsheet with Ctrl-V, I find that clipboard_transferable_getTransferData is called twice each time I paste, though "Hi There" is pasted into the active cell. If I paste into Word, clipboard_transferable_getTransferData is called a bunch of times, but eventually pastes the string as well. If I paste into Windows notepad, the function is called once, the string is pasted, and subsequent pastes paste the string without triggering the function.

I have used listeners of various sorts in other OO apps, and I have found that it is best to dispose of them once they have served their purpose or else they accumulate in memory and don't go away until exiting the application. In the present case, I haven't determined any way to dispose of the listener referred to by the xt variable. If I add Erase xt to the code, the variable does disappear, but the listener remains. One problem seems to be that XTransferable, though created in this case as a listener, isn't one of the usual listener types.
Apache OpenOffice 4.1.1
Windows XP
ghe
Posts: 23
Joined: Wed May 19, 2010 5:32 pm

Re: Put a string into the clipboard

Post by ghe »

Thanks for those indications...
Although I'm not certain about this, I think you do get crashes: they are absolutely systematic in my case, whichever version of OO I use. But they may be handled more gracefully by OOo under Windows?
Personally I don't know how to get further with this problem... I can't use this code in production with it :(

--ghe

P. S. : I submitted an issue for this problem: http://qa.openoffice.org/issues/show_bug.cgi?id=111904
OpenOffice 3.2 on Debian Testing
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Put a string into the clipboard

Post by ms777 »

I think you should also implement the function TR_disposing

Good luck,

ms777
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Put a string into the clipboard

Post by Charlie Young »

ms777 wrote:I think you should also implement the function TR_disposing

Good luck,

ms777
I had thought of that and tried it, but the Xtransferable "listener" doesn't have a dispose method.
Apache OpenOffice 4.1.1
Windows XP
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Put a string into the clipboard

Post by ms777 »

Hi,

see http://www.oooforum.org/forum/viewtopic ... =disposing and http://www.oooforum.org/forum/viewtopic ... =disposing . That lead me to my statement. I am not absolutely sure if it is required to implement disposing ... XTransferable is not a daughter of XEventListener

Good luck,

ms777
ghe
Posts: 23
Joined: Wed May 19, 2010 5:32 pm

Re: Put a string into the clipboard

Post by ghe »

ms777, did you read Charlie's reply? He tried it, and it did not work.
BTW, I just tried it, to be sure, and I have exactly the same error.
OpenOffice 3.2 on Debian Testing
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Put a string into the clipboard

Post by ms777 »

Yes, I read Charlies post ... but I am not 100% that every thing in OO is implemented as documented

One other thing just came to my attention: you let getTransferData return a String, whis is in accordance with the any in the doc. You may also try to return an array of integer (OO has no byte), and fill it with a 0-terminated string. Again, I am just guessing here ...

Good luck,

ms777
Post Reply