OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Copy arbitrary string (text) to Clipboard

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Feb 18, 2004 11:50 am    Post subject: Copy arbitrary string (text) to Clipboard Reply with quote

Here is a routine that will take any string as a parameter, and put that text into the clipboard.

Code:
Sub Main
   TextToClipboard( "This is a test." )
End Sub


' Given a string of text, put that text into the clipboard.
' This works by...
'   1. Invisibly create a new Writer document
'   2. Insert text into that document.
'   3. Select All
'   4. Copy
'   5. Close the invisible writer document.
' So what ends up in the clipboard can be pasted to an external
'  program as Text.  But the clipboard contains additional information
'  about the text, such as whatever default font and style it had in
'  the temporary Writer document.
'
Sub TextToClipboard( ByVal cText As String )
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0,_
      Array( MakePropertyValue( "Hidden", True ) ) )

   ' Get the text of the document.
   oText = oDoc.getText()
   ' Get a cursor that can move over or to any part of the text.
   oCursor = oText.createTextCursor()
   
   ' Insert text and paragraph breaks into the text, at the cursor position.
   oText.insertString( oCursor, cText, False )
   
   SelectAll( oDoc )
   ClipboardCopy( oDoc )
   
   oDoc.close( True )
End Sub



In order to make the above code work, you need some additional routines.

Go to this message...
MakePropertyValue
http://www.oooforum.org/forum/viewtopic.php?t=5108
and get the function...
MakePropertyValue()

Go to this message...
Making the Dispatcher easier to use
http://www.oooforum.org/forum/viewtopic.php?t=5058
and get the functions...
SelectAll()
ClipboardCopy()
in order to make the above two functions work, you also need...
DocumentDispatch()
in order top make DocumentDispatch() work, you also need...

Go to this message...
Document model, controller and frame
http://www.oooforum.org/forum/viewtopic.php?t=5057
and get the functions...
GetDocumentFrame()


One possible application of this is when you need to script OOo to get data from somewhere, then put text into the clipboard, so that you can script some other program outside of OOo to paste that text in, for example, paste text onto part of a command line you're "typing".

Hope this is useful.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Sep 07, 2004 8:45 am    Post subject: Reply with quote

Here is the above program, assembled as a complete working example....

Code:
Sub Main
   TextToClipboard( "This is a test." )
End Sub


' Given a string of text, put that text into the clipboard.
' This works by...
'   1. Invisibly create a new Writer document
'   2. Insert text into that document.
'   3. Select All
'   4. Copy
'   5. Close the invisible writer document.
' So what ends up in the clipboard can be pasted to an external
'  program as Text.  But the clipboard contains additional information
'  about the text, such as whatever default font and style it had in
'  the temporary Writer document.
'
Sub TextToClipboard( ByVal cText As String )
   oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0,_
      Array( MakePropertyValue( "Hidden", True ) ) )

   ' Get the text of the document.
   oText = oDoc.getText()
   ' Get a cursor that can move over or to any part of the text.
   oCursor = oText.createTextCursor()
   
   ' Insert text and paragraph breaks into the text, at the cursor position.
   oText.insertString( oCursor, cText, False )
   
   SelectAll( oDoc )
   ClipboardCopy( oDoc )
   
   oDoc.close( True )
End Sub




' The MakePropertyValue() function is defined here...
'   http://www.oooforum.org/forum/viewtopic.php?t=5108
'
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
   oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
   If Not IsMissing( cName ) Then
      oPropertyValue.Name = cName
   EndIf
   If Not IsMissing( uValue ) Then
      oPropertyValue.Value = uValue
   EndIf
   MakePropertyValue() = oPropertyValue
End Function



' Routines from
'    Making the Dispatcher easier to use
'    http://www.oooforum.org/forum/viewtopic.php?t=5058


Sub ClipboardPaste( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:Paste" )
End Sub

Sub ClipboardCopy( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:Copy" )
End Sub

Sub ClipboardCut( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:Cut" )
End Sub


Sub SelectAll( oDocumentFrame )
   DocumentDispatch( oDocumentFrame, ".uno:SelectAll" )
'   DocumentDispatch( oDocumentFrame, "slot:5723" )
End Sub


'----------
' An easy to use Dispatch on an office document.
' Arguments are similar to the args for the com.sun.star.frame.XDispatchHelper
'  interface of com.sun.star.frame.DispatchHelper.
' What makes this so easy to use are two things:
'   1. The fact that the oDocumentFrame parameter can actually accept
'      either the document model or one of its controllers.
'   2. The optional parameters.
' For an example of how simple this routine is to use, see
'  routines such as ClipboardCopy().
'
' Parameters:
'      oDocumentFrame      -   An office document frame.
'                        But wait!  It could be the document controller
'                         or the document model.  This routine will find
'                         the document frame from either of these.
'      cURL            -   The dispatch URL.
' Optional:
'      cTargetFrameName   -   Defaults to blank.
'      nSearchFlags      -   Defaults to zero.
'      aDispatchArgs      -   Defaults an an empty sequence.
'
Sub DocumentDispatch( ByVal oDocumentFrame As Object,_
                  ByVal cURL As String,_
                  Optional cTargetFrameName,_
                  Optional nSearchFlags,_
                  Optional aDispatchArgs )
   
   ' If they gave us the wrong parameter...
   If Not HasUnoInterfaces( oDocumentFrame, "com.sun.star.frame.XFrame" ) Then
      ' Be sure that we've got the document frame.
      ' Someone might have passed us the document model or one of
      '  its controller's.
      oDocumentFrame = GetDocumentFrame( oDocumentFrame )
   EndIf
   
   If IsMissing( cTargetFrameName ) Then
      cTargetFrameName = ""
   EndIf
   If IsMissing( nSearchFlags ) Then
      nSearchFlags = 0
   EndIf
   If IsMissing( aDispatchArgs ) Then
      aDispatchArgs = Array()
   EndIf
   
   oDispatchHelper = createUnoService( "com.sun.star.frame.DispatchHelper" )
   oDispatchHelper.executeDispatch( oDocumentFrame, cURL, cTargetFrameName, nSearchFlags, aDispatchArgs )
End Sub


' Routines from
'    Document model, controller and frame
'    http://www.oooforum.org/forum/viewtopic.php?t=5057



'----------
' This will always return the document's frame.
' Pass in any one of...
'   * the document's model (subclass of com.sun.star.document.OfficeDocument)
'   * the document's controller
'   * the document's frame
Function GetDocumentFrame( oDoc As Object ) As Object
   Dim oFrame As Object
   
   ' If the caller gave us the document model...
   If oDoc.supportsService( "com.sun.star.document.OfficeDocument" ) Then
      ' ...then get the controller from that.
      oCtrl = oDoc.getCurrentController()
      ' ...then get the frame from the controller.
      oFrame = oCtrl.getFrame()

   ' If the caller gave us a document controller...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XController" ) Then
      oCtrl = oDoc
      ' ...then get the frame from the controller.
      oFrame = oCtrl.getFrame()
   
   ' If the caller gave us the document frame...
   ElseIf HasUnoInterfaces( oDoc, "com.sun.star.frame.XFrame" ) Then
      ' ...thanks!  That's just what we wanted!
      oFrame = oDoc
   
   Else
      ' The caller did not give us what we expected!
      MsgBox( "GetDocumentFrame called with incorrect parameter." )
   EndIf
   
   GetDocumentFrame() = oFrame
End Function

_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Peter
OOo Enthusiast
OOo Enthusiast


Joined: 28 May 2004
Posts: 105
Location: Berlin / Germany

PostPosted: Thu Jun 15, 2006 11:29 pm    Post subject: Reply with quote

Hi Danny,

I think there's no need to workaround this problem with the dispatcher and a hidden document.
http://api.openoffice.org/docs/DevelopersGuide/OfficeDev/OfficeDev.xhtml#1_2_1_1_Using_the_Clipboard

I was not able to transform this java example into Basic.
Code:

DataFlavor[] adf = new DataFlavor[1];
                   DataFlavor uniflv = new DataFlavor(
                            UNICODE_CONTENT_TYPE,
                            "Unicode Text",
                            new Type(String.class) );

 

                  adf[0] = uniflv;
 
                   return adf;


Any hints how to create the DataFlavor object containing a simple string from a variable in BASIC?

Regards

Peter
Back to top
View user's profile Send private message
Fernand
OOo Enthusiast
OOo Enthusiast


Joined: 22 Jun 2004
Posts: 142

PostPosted: Thu Dec 02, 2010 2:52 am    Post subject: Reply with quote

Finaly i found somewhere on the web, with thanks to the unknwon author
a working code for pasing a string to the clipboard
Code:

Global sTxtCString As String
 
Sub clipboard_1
  sText = "123456"
  CopyToClipBoard(sText)
End Sub
 
Sub CopyToClipBoard( sText )
  ' create SystemClipboard instance
  oClip = CreateUnoService( _
      "com.sun.star.datatransfer.clipboard.SystemClipboard")
  oTR = createUnoListener("Tr_", _
      "com.sun.star.datatransfer.XTransferable")
  ' set data
  oClip.setContents(oTR,Null)
  sTxtCString = sText
  'oClip.flushClipboard() ' does not work
End Sub
 
Function Tr_getTransferData( _
    aFlavor as com.sun.star.datatransfer.DataFlavor)
  If (aFlavor.MimeType = "text/plain;charset=utf-16") Then
    Tr_getTransferData() = sTxtCString
  End If
End Function
 
Function Tr_getTransferDataFlavors()
  Dim aFlavor As new com.sun.star.datatransfer.DataFlavor
  aFlavor.MimeType = "text/plain;charset=utf-16"
  aFlavor.HumanPresentableName = "Unicode-Text"
  Tr_getTransferDataFlavors() = array(aFlavor)
End Function
 
Function Tr_isDataFlavorSupported( _
    aFlavor as com.sun.star.datatransfer.DataFlavor) as Boolean
  If aFlavor.MimeType = "text/plain;charset=utf-16" Then
    Tr_isDataFlavorSupported = true
  Else
    Tr_isDataFlavorSupported = false
  End If
End Function


geting from the clipboard
Code:

Sub clipboard_2
  oClip = CreateUnoService( _
      "com.sun.star.datatransfer.clipboard.SystemClipboard")
  oTransfer = oClip.getContents()
 
  ' sequence of com.sun.star.datatransfer.DataFlavor
  aDataFlavors = oTransfer.getTransferDataFlavors()
  bType = False
  For i = 0 To UBound(aDataFlavors) Step 1
    aDataFlavor = aDataFlavors(i)
    If aDataFlavor.MimeType = "text/plain;charset=utf-16" Then
      bType = True
      Exit For
    End If
  Next
  If bType Then
    ' convert utf-16 to UNO string
    oConverter = CreateUnoService( _
        "com.sun.star.script.Converter")
    sData = oConverter.convertToSimpleType( _
        oTransfer.getTransferData(aDataFlavor), _
        com.sun.star.uno.TypeClass.STRING)
    msgbox sData
  End If
End Sub
[/code]
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group