Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to copy the address of a hyperlink in Mathematica using the keyboard. I found code for copying hyperlink addresses in ContextMenus.tr:

MenuItem["&Copy Address", FrontEndExecute[
 {FEPrivate`FrontEndExecute[FrontEnd`CopyToClipboard[
  CurrentValue[{ButtonBoxOptions, ButtonData}]]]
 }
], MenuEvaluator->None]

but using this to make a keyboard shortcut in KeyEventTranslations.tr as follows doesn't seem to work.

Item[KeyEvent["c", Modifiers -> {Control, Option}], FrontEndExecute[
 FrontEnd`CopyToClipboard[ CurrentValue[{ButtonBoxOptions, ButtonData}]]]
]

(KeyEvent["c", Modifiers -> {Control, Option}] didn't seem to work at all as a shortcut combination, but other combinations that worked for other actions didn't work for copying the hyperlink address.) One problem is that I'm not sure whether the hyperlink has to be selected or what.

share|improve this question
4  
Maybe I do not understand what you want to do: Could you explain why you don't select that link and press Ctrl+c and then Ctrl+v? –  user21 Mar 7 '12 at 8:08
    
Well, there's more than way of inserting a hyperlink into some text. For example, one can create a hyperlink with Hyperlink and then copy and paste it into the text, or one can select some text and create a hyperlink using the menu. In those two cases, Ctrl+c, Ctrl+v either copies a large TagBox expression or just the text of the hyperlink, not the address (URI). I now see that one possible solution might be to copy the TagBox expression and then use another program to replace the clipboard contents with the hyperlink address, which is inside the expression. –  Andrew MacFie Mar 7 '12 at 14:20
1  
It's possible, if one has no other option, to Show Expression (Ctrl+Shift+e) and copy the address from there. –  Andrew MacFie Mar 7 '12 at 14:23
    
I notice that in your MenuItem you write FEPrivate`FronEndExecute while in the KeyEvent you just write FrontEndExecute. Maybe that difference is why the menu item works, but the keyboard shortcut doesn't? –  celtschk Mar 8 '12 at 17:17
2  
The code doesn't work for two reasons. One is that this is not quite the code that is used in the context menu, and the mistranslation here mixes things running in the FE's packet system (FrontEnd`CopyToClipboard) with things which don't run in the packet system (CurrentValue). That would be trivial to work around, but there's a deeper problem, which is that CurrentValue will only return the correct thing if exactly the right thing is selected. It works in context menus because the menu system automatically sets the right target. I'm still pondering alternative solutions. –  John Fultz Mar 9 '12 at 7:55
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.