CoCreate Modeling: Exchanging text via the Windows clipboard
We already discussed how Lisp code in CoCreate Modeling can communicate with
other components in various ways:
And now it's about time to explore yet another dimension of interprocess
communication - using COM objects.
A customer recently asked for help - he needed code which exchanges data with
other applications via the Windows clipboard. There is no officially supported
API for this kind of communication in the Integration Kit. However, that's not really
a problem because access of the clipboard is provided as a service by the
COM object "InternetExplorer.Application" which is available on pretty much any
Windows system. We can use Lisp to ask that COM object to help us with
clipboard communication.
This article
at the Windows Script Center website shows how to use the
InternetExplorer.Application
object from VBscript; all we need to do is to translate that example into
equivalent Lisp code - such as the following.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Description: Read text from and write text to the clipboard
;; using COM objects
;; Requires .NET API license
;; Author: Claus Brod
;; Language: Lisp
;;
;; (C) Copyright 2007 Claus Brod, all rights reserved
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(in-package :clausbrod.de)
(defun get-clipboard-object()
(let ((ie (.net:create-object "InternetExplorer.Application")))
(.net:invoke ie "Navigate" "about:blank")
(.net:get-property
(.net:get-property
(.net:get-property ie "Document")
"Parentwindow")
"ClipboardData")))
(defun text-to-clipboard(text)
(.net:invoke (get-clipboard-object) "SetData" "Text" text))
(defun text-from-clipboard()
(.net:invoke (get-clipboard-object) "GetData" "Text"))
So how does this code work?
First we need to instantiate and connect to
InternetExplorer.Application
; this is
the first thing which
(get-clipboard-object)
does. "Instantiation" means that an
Internet Explorer process is started in the background (unless an instance of IE is
already running).
Second, we ask the
InternetExplorer.Application
to open a blank document.
This is a prerequisite for the next step. From the document object that we created,
we can ask our way through to the
ClipboardData
object (the full object
path is
Document.Parentwindow.ClipboardData
). This is the job of the
get-clipboard-object
function.
And finally, the
ClipboardData
object has two member functions which are most useful to us:
GetData
and
SetData
.
GetData
can be used to retrieve data from the clipboard,
while
SetData
pushes data to the Windows clipboard. Both functions expect us to
specify the format of the data that we expect on the clipboard - in our case "Text".
Legal warning: You need a license to use the COM/.NET API. Please
contact
CoCreate support for details.
--
ClausBrod - 11 Apr 2007