read text string system systems clipboard Josef Frysak How to read a string out of the systems clipboard?

Create an instance of the "SystemClipboard" service and its

"XSystemClipboard" interface. Next check the clipboard for a valid text

entry by searching the list of all supported data types. If the content

is a text string convert it to a simple string using the "Converter"

service.

For further details see http://wi.wu-wien.ac.at/rgf/diplomarbeiten/BakkStuff/2008/200809_Frysak/200809_Frysak_Automating_OOo_ooRexx_Nutshells.pdf.

-- try to get a script context, will be .nil, if script was not invoked by OOo x_ScriptContext = uno.getScriptContext() if (x_ScriptContext <> .nil) then do -- invoked by OOo as a macro -- get context x_ComponentContext = x_ScriptContext~getComponentContext -- get desktop (an XDesktop) x_Desktop = x_ScriptContext~getDesktop -- get current document x_Document = x_ScriptContext~getDocument end else do -- called from outside of OOo, create a connection -- connect to Open Office and get component context x_ComponentContext = UNO.connect() -- create a desktop service and its interface service = "{%see com.sun.star.frame.Desktop}" s_Desktop = x_ComponentContext~getServiceManager~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}~createInstance(service) x_Desktop = s_Desktop~{%see com.sun.star.frame.XDesktop%XDesktop} -- get the last active document x_Document = x_Desktop~getCurrentComponent() end -- this is the text we want to write cliptext = "" -- create the clipboard service controlling the clipboard of -- the operating system x_MultiServiceFactory = x_ComponentContext~getServiceManager()~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory} clipboard = "{%see com.sun.star.datatransfer.clipboard.SystemClipboard}" s_Clipboard = x_MultiServiceFactory~createInstance(clipboard) x_Clipboard = s_Clipboard~{%see com.sun.star.datatransfer.clipboard.XClipboard%XClipboard} -- get the clipboard content x_Transferable = x_Clipboard~getContents() /* check if the clipboard object contains text information: get a list of all supported data types and search for a valid entry (utf-16) */ flavorslist = x_Transferable~getTransferDataFlavors() flavorslistlength = bsf('arrayLength', flavorslist) counter = 1 found = false; do while (counter <= flavorslistlength) & (found = false) found = (flavorslist[counter]~bsf.getFieldValue("MimeType") = "text/plain;charset=utf-16") counter = counter + 1 end -- if it contains a valid entry: if found then do counter = counter - 1 -- create a data type converter service s_Converter = x_MultiServiceFactory~createInstance("{%see com.sun.star.script.Converter}") x_TypeConverter = s_Converter~{%see com.sun.star.script.XTypeConverter%XTypeConverter} -- transform data (into simple text) content = x_Transferable~getTransferData(flavorslist[counter]) -- read clipboard as string stringtype = bsf.getConstant("{%see com.sun.star.uno.TypeClass}", "STRING") cliptext = x_TypeConverter~convertToSimpleType(content, stringtype) end -- add the current date to the previously recieved text cliptext = cliptext || DATE("E",,,".") -- and write down this information at the current textcursor position x_TextDocument = x_Document~{%see com.sun.star.text.XTextDocument%XTextDocument} x_Text = x_TextDocument~getText s_CurrentController = x_TextDocument~getCurrentController() x_TextViewCursorSupplier = s_CurrentController~{%see com.sun.star.text.XTextViewCursorSupplier%XTextViewCursorSupplier} x_CurrentCursor = x_TextViewCursorSupplier~getViewCursor() x_TextCursor = x_Text~createTextCursorByRange(x_CurrentCursor~getStart()) x_SimpleText = x_Text~{%see com.sun.star.text.XSimpleText%XSimpleText} x_SimpleText~insertString(x_TextCursor, cliptext, .false) ::requires UNO.CLS
Initial version