1
$\begingroup$

For example, I have a Button

Button["http://www.wolfram.com", 
  NotebookLocate[{URL["http://www.wolfram.com"], None}], 
  Appearance -> None]

How to select text with the mouse?

$\endgroup$
  • $\begingroup$ I would tend to say "you can't do that with the mouse". $\endgroup$ – Öskå Aug 3 '14 at 9:58
  • $\begingroup$ It is not clear, what does the author want. $\endgroup$ – Alexei Boulbitch Aug 3 '14 at 10:41
  • $\begingroup$ @AlexeiBoulbitch I think the OP wants to be able to select the button's text with the mouse. $\endgroup$ – Öskå Aug 3 '14 at 11:04
  • 1
    $\begingroup$ I think that the only way is to press Ctrl+Shift+E and then select relevant piece of the code. $\endgroup$ – Alexey Popkov Aug 3 '14 at 11:09
  • 1
    $\begingroup$ @Öskå Of course not. Then you select/copy the whole button, open a notepad, paste the button there and select www this time from the notepad. Or even better: you just type "www" in whatever place you like. Done. The question is, therefore, what is the ultimate aim of such a selection. But that I have already written. $\endgroup$ – Alexei Boulbitch Aug 3 '14 at 13:49
4
$\begingroup$

You can use EventHandler with a modifier key to switch between Button and text-copiable styles:

With[{
      buttonFunc = Button[#, NotebookLocate[{URL[#], None}]] &,
      stringFunc = InputField[#, String, FrameMargins -> 20] &
     },
     DynamicModule[{f = buttonFunc},
                   EventHandler[
                                Dynamic[f["http://www.wolfram.com"]],
                                {
                                 "MouseEntered" :>
                                     If[CurrentValue["ShiftKey"],
                                        f = stringFunc,
                                        f = buttonFunc ]
                                }]]
    ]

switch styles with modifier key

The result will be an ordinary Button for normal mouse actions, but will turn into an editable InputField when mouseover the button with Shift key pressed.

$\endgroup$
  • $\begingroup$ Thank you! I think it is best solution. $\endgroup$ – Kypaku Aug 4 '14 at 17:11
  • $\begingroup$ @Kypaku Glad you found it useful :) $\endgroup$ – Silvia Aug 4 '14 at 17:26
2
$\begingroup$

From your example in seems you are trying to create a hyperlink. You can do that with Hyperlink:

Hyperlink["click me", "http://www.wolfram.com", ButtonNote -> "http://www.wolfram.com"]
click me

If you right-click on the output click me you should get a menu with two items:

  • Copy Hyperlink
  • Copy Address

The first command will copy the button text ("click me") while the second will copy the URL itself.

$\endgroup$
2
$\begingroup$

This is an addendum to Mr.Wizard'a answer.

First, I ask Mr.Wizard: were y0u not put off even a little by the code editor's bright red coloring of ButtonNote? According to the docs, that option has been superseded by StatusArea since V6. Your code still works, of couse, but to bring it up date, it can be rewritten as

Hyperlink[
  StatusArea["click me", "http://www.wolfram.com"], 
  "http://www.wolfram.com"]

Second, the context menu, which appears upon right-clicking the mouse, only seems to work properly if the link's target is a URL. When the target is a tagged cell in the current notebook, the second choice Copy Address acts the same as the first choice, Copy Hyperlink. This looks like a bug to me. This behavior can be confirmed by creating a cell with the tag "target" and evaluating

Hyperlink[
  StatusArea["click me", "target"], 
  {EvaluationNotebook[], "target"}]

Finally, the OP asked for a button. That is easily provided.

Hyperlink[
  StatusArea["click me", "Open http://www.wolfram.com in the default browser"], 
 "http://www.wolfram.com", 
  Appearance -> "DialogBox"]

button

$\endgroup$
  • 1
    $\begingroup$ Me, put off by a syntax warning? Surely you jest. If ButtonNote is deprecated someone forgot to tell the developers since it's used internally quite a bit. In fact it's what ToExpression thinks is the correct syntax for your example: Hyperlink[StatusArea["click me", "http://www.wolfram.com"], "http://www.wolfram.com"] // ToBoxes // (ToExpression[#, StandardForm, Hold] &) // InputForm :^) $\endgroup$ – Mr.Wizard Aug 4 '14 at 4:24
  • $\begingroup$ @Mr.Wizard. I'm aware of ButtonNote's use internally. I'm sure that's why your version of the code not only works now, but will continue to work. A portion of my intent in posting this addendum was to convey my feeling that Hyperlink is a just little screwed up. $\endgroup$ – m_goldberg Aug 4 '14 at 8:24
  • $\begingroup$ FYI: You already have my vote. I'm just having fun with this, as I think you know. :-) $\endgroup$ – Mr.Wizard Aug 4 '14 at 16:36

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.