From: Yuri Kandrashkin on
Hi,
Have a look on the code of the function CopyToStack if the Sidebar package
which can be download from http://sites.google.com/site/qstate/
It creates a button for the notebook selection which later can be send to
Clipboard.

2010/6/7 Istv=E1n Zachar <zac(a)freemail.hu>

> Yes, I'm aware of the method you suggested below, but since it does
> not satisfy my needs, I've come to the very same conclusion: a
> FrontEndToken must be used. Although I have no idea how to do that.
> What I know is that I don't want to manually select anything, just
> send the value of any named variable in memory to the clipboard. So no
> manual selection, no extra notebook appearing, not any visual feedback
> on the process. Actually ths functionality seems so obvious I wonder
> why there isn't any established method to cover it.
> i
>
>
>
Sincerely,
Yuri Kandrashkin, PhD


From: István Zachar on
Your 4 steps can be challenged easily one by one:

1. It can be another notebook, which is already opened, and I want to
put a large list after a new assigment (like "newVar = [paste value
here]"), I definitely don't want to use NotebookPut, or similar
constructs creating a NEW notebook object.
2. Your method suggests that the actual value of the variable is
onscreen. What if it is not displayed at all? What if it is extremely
long, and I didn't print it on screen, but still want to move it to
another application? Yes, still I can print it in Mathematica, and then copy
it, but this is obviously not the solution I'm looking for.
3. There is no current selection, only a variable, which is possibly
not even printed to screen.
4. The whole point of push to clipboard is not to deal with files at
all.

istvan


>
> The clipboard is meant to be used for things that have been selected
> (manually or programmatically) from some place (in Mathematica: a notebook).
> Any other unconventional use would not adhere to the standard GUI
> conventions (tautologically) and probably reflects a not very well
> thought out interface.
>
> In practice: ask yourself why do you want to copy a variable (I
> suppose you mean the value of a variable) to the clipboard? To paste
> it somewhere else I bet. OK, where to?
> 1- If it is another notebook, than you do not need the clip, just type
> the variable in the other notebook.
> 2- If it is another place in another program (Word, email,ect), just
> select what you want , type ctrl-C and than paste it in the other
> program.
> 3- If you get tired to type ctrl-C you can make a button which copies
> to the clip the current selection. But also in this case you need to
> have something selected, so that the operation makes sense.
> 4- You may also consider Saving/Exporting your variable in a specific
> format ,so it can be read into another program.
>
> So, you see, you only need the clip in case number 2 and 3. In these
> cases you need to select something which has been already calculated
> and is displayed on a notebook. The selection can be done
> programmatically, maybe using cell tags, but one way or the other you
> must select something.
> There is no direct way to place an expression on the clip, as far as I
> know.
> Of course, what you can always do, is a button which creates a
> temporary nb, places your variable there, selects it and copies it,
> than erases the nb. Perhaps the temporay nb could be more or less
> invisible (I am not sure about this).


From: David Bailey on
On 07/06/10 13:06, magma wrote:

>
> The clipboard is meant to be used for things that have been selected
> (manually or programmatically) from some place (in Mathematica: a notebook).
> Any other unconventional use would not adhere to the standard GUI
> conventions (tautologically) and probably reflects a not very well
> thought out interface.
>


I disagree with this philosophy, in that when you build an interface, a
"Copy" button copies whatever it is that the program is logically
manipulating at that point - which may not be what is on the screen.

There is some functionality in Mathematica to handle this, but you
probably need to experiment a bit to get the effect just right. Here is
an example to start Istvan off:

In[4]:= fred = {a, b, c}

Out[4]= {a, b, c}

In[6]:= NotebookWrite[ClipboardNotebook[], fred]

Global`a

Global`b

Global`c

David Bailey

http://www.dbaileyconsultancy.co.uk

From: David Bailey on
On 09/06/10 12:18, David Bailey wrote:
> On 07/06/10 13:06, magma wrote:
>
>>
>> The clipboard is meant to be used for things that have been selected
>> (manually or programmatically) from some place (in Mathematica: a notebook).
>> Any other unconventional use would not adhere to the standard GUI
>> conventions (tautologically) and probably reflects a not very well
>> thought out interface.
>>
>
>
> I disagree with this philosophy, in that when you build an interface, a
> "Copy" button copies whatever it is that the program is logically
> manipulating at that point - which may not be what is on the screen.
>
> There is some functionality in Mathematica to handle this, but you
> probably need to experiment a bit to get the effect just right. Here is
> an example to start Istvan off:
>
> In[4]:= fred = {a, b, c}
>
> Out[4]= {a, b, c}
>
> In[6]:= NotebookWrite[ClipboardNotebook[], fred]
>
> Global`a
>
> Global`b
>
> Global`c
>
> David Bailey
>
> http://www.dbaileyconsultancy.co.uk
>
I should just clarify, I performed a paste operation after In[6] - sorry!

David Bailey

ww.dbaileyconsultancy.co.uk

From: István Zachar on
Dear Derek (and all),

thanks for the code, I managed to streamline it to my taste and aim:

CopyToClipboard[expr_] := Module[{nb},
nb = CreateDocument[Null, Visible -> False, WindowSelected ->
True];
NotebookWrite[nb, Cell[ToBoxes(a)expr, "Output"], All];
FrontEndExecute[FrontEndToken[nb, "CopySpecial"]];
NotebookClose(a)nb
];

I was unaware of the fact that I can call for invisible notebooks. Now
this function does the copying without any obtrusive notebook
appearing onscreen.
I got rid of all the options and wrappers, but surely with time I will
expand this code to cover the functionality you encoded in it!
Thanks again,

Istvan

On Jun 8, 1:06 pm, Derek Yates <yat...(a)mac.com> wrote:
> Hi Istvan,
>
> I have also wanted to do what you wish to. magma suggested in a post
> to just copy and paste, however, I frequently need to export data to
> Excel. Unfortunately, copy and paste does not work well for this, and
> doing this via a file is very time-consuming. The only way I found to
> copy and paste a matrix properly to Excel was the following:
> Print[Grid[matrix]]. Then select the printed output as Plain Text and
> then paste it in Excel. After doing this a few times, I decided to
> write some code and attach it to a button. You asked for code that
> does not open up a new notebook. I could not find a way to do that
> (other than writing into the existing notebook). So I open a hidden
> notebook, paste the expression there, copy it (via a FrontEnd Token)
> and then close the notebook. I doubt there is a way for the kernel to
> access the clipboard directly (it could after all be running remotely,
> so it would need to ask the front-end to do the work in any case).
>
> I attach a slightly simplified (and untested) version of my code
> below. It consists of two functions. One to attach to the button,
> which evaluates and then sends whatever is selected to the main
> function which does the copying. You may well want to simplify the
> functions and change them for your own purposes. There are two things
> that I do not know how to fix: (1) for large matrices, the function is
> slow. (2) if you use CellContext to localise notebooks, then the code
> may not always work. I have caught some of the cases (like having a
> Notebook option that changes the CellContext, but do not cater for
> styles or individual cells that have set the CellContext.
>
> The codes works for expressions which are not matrices too, but does
> extra processing when you pass it a matrix or vector (assuming you set
> option Format->True).
>
> Regards,
>
> Derek
>
> CopyAs::usage =
> "Option for ClibboardCopy. Possible values are \"PlainText\", \
> \"InputText\", \"CopyAsTeX\", \"MathML\", \"CellExpression\", \
> \"NotebookExpression\", \"MGF\", \"EMF\", \"WAV\"";
>
> Options[ClipboardCopy] = {CopyAs -> "PlainText",
> Transpose -> Automatic, Style -> "Output", Format -> False};
> ClipboardCopy::usage =
> "ClipboardCopy[expression] will evaluate expression and place it on
> \
> the clipboard in the format specified by the option CopyAs. Works \
> well for pasting vectors and matrices to Excel if you use CopyAs\
> \[ThinSpace]\[Rule]\[ThinSpace]\"PlainText\" and Format\[ThinSpace]\
> \[Rule]\[ThinSpace]True";
>
> ClipboardCopy[expr_, opts : OptionsPattern[]] :=
> With[{
> nb = CreateDocument[Null, Visible -> False,
> WindowSelected -> True,
> FilterRules[Flatten[{opts, Options[ClipboardCopy]}],
> Options[CreateDocument]]
> ],
> rules = {
> n_Real :>
> NumberForm[n, DigitBlock -> \[Infinity],
> ExponentFunction -> (Null &)],
> \[CenterEllipsis] -> "..."
> },
> transposefunc = Switch[OptionValue(a)Transpose,
> Automatic, (If[Length[#[[1]]] > 50 && Length[#[[1]]] > Length[=
#],
> Transpose[#], #] &),
> True, Transpose,
> _, (# &)
> ],
> cellstyle = OptionValue(a)Style,
> celloptions = Sequence[ShowCellBracket -> False]
> },
> NotebookWrite[nb,
> Replace[expr, {
> Except[_Cell | {__Cell}] :>
> Cell[BoxData(a)ToBoxes[
> Switch[{OptionValue(a)Format, expr},
> {True, _?MatrixQ}, Grid[transposefunc[expr /. rules]]=
,
> {True, _?VectorQ}, Grid[transposefunc[{expr} /. rules=
]],
> {True, {{__?AtomQ} ..}},
> Grid[transposefunc[
> PadRight[expr /. rules, Automatic, ""]]],
> _, ExpressionCell[expr]
> ]
> ], cellstyle, celloptions],
> cell_Cell :> Append[cell, celloptions],
> celllist : {__Cell} :> (Append[#, celloptions] &) /@ celllist
> }], All];
> FrontEndExecute[
> FrontEndToken[nb, "CopySpecial", OptionValue(a)CopyAs]];
> NotebookClose[nb]
> ]
>
> ClipboardPalette::usage = "ClipboardPalette[] creates a palette with \
> a button which will ClipboardCopy the selection."
>
> ClipboardPalette[] :=
> (CreatePalette[
> With[{prefunc =
> Quiet(a)Check[ToExpression[#], #, ToExpression::notstrbox] &=
},
> DynamicModule[{transposesetting},
> Row[{
> Row[{"Transpose: ",
> SetterBar[
> Dynamic[transposesetting], {Automatic, True, False=
}]}],
> Button["Copy for Excel",
> ClipboardCopy[
> Replace[prefunc[NotebookRead(a)InputNotebook[]],
> Grid[x_, ___] :> x], CopyAs -> "PlainText",
> Transpose -> transposesetting, Format -> True]],
> Button["Copy as Bitmap",
> ClipboardCopy[prefunc[NotebookRead(a)InputNotebook[]],
> CopyAs -> "MGF", Transpose -> transposesetting,
> Format -> False]],
> Button["Copy as Metafile",
> ClipboardCopy[prefunc[NotebookRead(a)InputNotebook[]],
> CopyAs -> "EMF", Transpose -> transposesetting,
> Format -> False]]
> }]
> ]
> ], WindowTitle -> None, Saveable -> False,
> CellContext :>
> Dynamic[OptionValue[Options[InputNotebook[], CellContext],
> CellContext]]
> ];)