10
$\begingroup$

Trying to work on this question I got distracted by my first naive attempt of copying the data and then typing

dat = ImportString[Paste[], "Table"]

ImportString::string: First argument Null is not a string. >>

ImportString[Null, "Table"]

I do know that typing dat = "" and then pasting within the quotation marks does work. Particularly nice is this other solution that unfortunately is limited to MS Windows .NET. But what I want is to understand what is going on, what is Paste giving if not a string, and how can I find out what the object is. As a bonus, making the output of Paste[] work within ImportString would be nice.

$\endgroup$
0

1 Answer 1

17
$\begingroup$

Paste is a command that, as a side-effect, inserts the contents of the clipboard into the current notebook selection. The return value is always Null, which means that Paste cannot be used for our purpose without some awkward notebook manipulation.

There is an undocumented way to access the clipboard: ClipboardNotebook.

NotebookGet[ClipboardNotebook[]] returns a notebook representation of the clipboard contents. When those contents are plain text, it returns a notebook that is structured like this:

    Notebook[{Cell["clipboard text", "Input", options]}, options]

We can recover and import the clipboard text from the first part of the first cell:

ImportString @ NotebookGet[ClipboardNotebook[]][[1, 1, 1]]

(* { {"2014-08-29 03:59:52 ", 27273, " Brown"}
   , {"2014-08-29 03:59:53 ", 27276, " Green"}
   , {"2014-08-29 03:59:55 ", 27276, " Brown"}
   , {"2014-08-29 03:59:57 ", 27303, " Red"}
   , {"2014-08-29 03:59:58 ", 27303, " Green"}
   , {"2014-08-29 03:59:59 ", 27303, " Brown"}
   , {"2014-08-29 04:00:04 ", 27317, " Brown"}
   , {"2014-08-29 04:00:07 ", 27331, " Blue"}
   , {"2014-08-29 04:00:07 ", 27334, " Blue"}
   , {"2014-08-29 04:00:08 ", 27331, " Red"}
   , {"2014-08-29 04:00:08 ", 27334, " Cyan"}
   }
*)

Bear in mind that the clipboard notebook structure is different for other types of data, such as images, formatted text, or expressions copied from notebooks. Also, as this is an undocumented feature its behaviour could change from release to release (but it has been pretty stable for a long time).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.