3
$\begingroup$

Please, consider the following code:

ColorSlider[
  Dynamic[x], ImageSize -> {800, 100}, AppearanceElements -> "Spectrum"
]

Button["get color",
 MessageDialog @ Dynamic[x];
 CopyToClipboard @ funct @ Dynamic[x];
]

Graphics[{Dynamic[x], Disk[]}, Frame -> True, FrameLabel -> Dynamic[x]]

To get the value of RGBColor in a string form, I have changed, in turn, the above funct with OutputForm, StringForm etc. obtaining always one of two results:

Dynamic[x] (* or *)

StandardForm  \!\(\*DynamicBox[ToBoxes[Global`x, StandardForm],
 ImageSizeCache->{380., {2., 8.}}]\)

To track the issue I tried

 Slider[Dynamic[z]]

 Print @ Dynamic[z];

 Button["copy",
     MessageDialog @ Dynamic[z];
     CopyToClipboard  @ Dynamic[z];
 ]

 Button["paste",
     Paste  @ Dynamic @ z;
 ]

and understood that CopyToClipboard get the whole DynamicBox, not its 'current content'. How can I get rid of the "wrapper" ?

Addendum

I just have realized taht the color value can be read by means of

InputField[Dynamic[x]]

but it's evidently an expedient and doesn' enlighten the broader subject.

$\endgroup$
  • 1
    $\begingroup$ So drop the Dynamic from the button. Dynamic is HoldAll so it stays this way untill it's displayed. Button is HoldRest so you will get the current value of x anyway. p.s. you can use Setting too. $\endgroup$ – Kuba Feb 24 '16 at 21:59
  • 2
    $\begingroup$ closely related: How to extract the numerical value of a dynamical variable $\endgroup$ – Kuba Feb 24 '16 at 22:01
  • $\begingroup$ Thanks for your reply ! Setting does perfectly the job. I'm going to study the other topics you mentioned. $\endgroup$ – mitochondrial Feb 24 '16 at 22:09
2
$\begingroup$

I haven't found better duplicate so here's what you need to know:

from Mr. Wizard's answer to Question about MapThread and Dynamic.

In short, Dynamic doesn't do anything until it is actually displayed on screen. Therefore, you are essentially doing this (note the String):

See also the linked topic.

Moreover, in your case Dynamic[x] takes the tour: Button -> Clipboard -> final destination. If you final destination is e.g. a MMA notebook, then the FrontEnd can handle this and will show you colored rectangle.

However, if you copy to let's say Notepad, you will get Dynamic[x], with no info about x because Dynamic is HoldAll.

As I've mentioned the workaround is to use Setting @ Dynamic @ x [1] or just x, the Button is HoldRest so you will copy the current value of x.

[1]: How to extract the numerical value of a dynamical variable

$\endgroup$
  • $\begingroup$ Thanks for the reply! I'm studying the sources you pointed at. Now I undestend the behaviour of this code: Row @ {Slider[Dynamic[x]], Dynamic[x], Button["raw", Dynamic[x] >>> "deleteme.txt"], Button["value", Setting @ Dynamic[x] >>> "deleteme.txt"] } $\endgroup$ – mitochondrial Feb 25 '16 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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