# How to select the text on the button?

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?

• I would tend to say "you can't do that with the mouse". – Öskå Aug 3 '14 at 9:58
• It is not clear, what does the author want. – Alexei Boulbitch Aug 3 '14 at 10:41
• @AlexeiBoulbitch I think the OP wants to be able to select the button's text with the mouse. – Öskå Aug 3 '14 at 11:04
• I think that the only way is to press Ctrl+Shift+E and then select relevant piece of the code. – Alexey Popkov Aug 3 '14 at 11:09
• @Ö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. – Alexei Boulbitch Aug 3 '14 at 13:49

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 ]
}]]
]


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.

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

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:

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

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"]


• 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 :^) – Mr.Wizard Aug 4 '14 at 4:24
• @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. – m_goldberg Aug 4 '14 at 8:24
• FYI: You already have my vote. I'm just having fun with this, as I think you know. :-) – Mr.Wizard Aug 4 '14 at 16:36