Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Accessing the contents of the clipboard

  • To: mathgroup at smc.vnet.net
  • Subject: [mg85212] Accessing the contents of the clipboard
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Sat, 2 Feb 2008 03:27:14 -0500 (EST)

Is there a *reliable* way to get the text that was copied to the
clipboard?

First I experimented with creating a hidden notebook and pasting to
it, like this:

nb = CreateDocument[Null, Visible -> False];
FrontEndExecute@FrontEndToken[nb, "PasteSpecial"]

NotebookGet[nb] (* extract the data somehow ... *)

NotebookClose[nb]

The problem is that I would like to access the exact string that was
copied, but NotebookGet returns a complicated BoxData structure that
may contain many things, even when the clipboard only had plain text
on it.  I do not know how to convert it back to the *same* string that
was copied, because in edge cases Mathematica might have interpreted
the text ... what if the text was "\(k\_1\)"? (Unlikely, I know, but I
would like to avoid unpleasant surprises.)


Then, in final desperation, I turned to the JLink documentation, and
using this page I put together something:
http://blogs.sun.com/JavaFundamentals/entry/transferring_text_through_the_clipboard

But I would like to avoid using JLink very much, because I do NOT know
Java (this is the first Java program I have ever written, if it can be
counted as a Java program at all ...), and I do not know what are the
things that can go wrong ...

So I am looking for better, preferably "pure Mathematica" suggestions.

<< JLink`

LoadJavaClass["java.awt.Toolkit"]

LoadJavaClass["java.awt.datatransfer.DataFlavor"]

dtk = Toolkit`getDefaultToolkit[]

clipboard = dtk@getSystemClipboard[]

clipdata = clipboard@getContents[clipboard]

clipdata@getTransferData[DataFlavor`stringFlavor]


  • Prev by Date: MathLink with MS Visual Studio 2008
  • Next by Date: Re: Accessing the contents of the clipboard
  • Previous by thread: Re: MathLink with MS Visual Studio 2008
  • Next by thread: Re: Accessing the contents of the clipboard