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[
{FEPrivateFrontEndExecute[FrontEndCopyToClipboard[
CurrentValue[{ButtonBoxOptions, ButtonData}]]]
}


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[
FrontEndCopyToClipboard[ 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.

• 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 Mar 7 '12 at 14:20
• It's possible, if one has no other option, to Show Expression (Ctrl+Shift+e) and copy the address from there. – Andrew Mar 7 '12 at 14:23
• I notice that in your MenuItem you write FEPrivateFronEndExecute 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
• 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 (FrontEndCopyToClipboard) 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

What will trigger it

select a hyperlink and use a shortcut, in case of wrong selection you will get Beep[]

What will happen

If[
MatchQ[#, {_}],
CopyToClipboard[First[#]],
Beep[]
] &[
Cases[
CurrentValue["SelectionData"],
_[ButtonData, {x_, _}] :> x,
Infinity
]
]


How to implement it

1. With joker.m from the Shortcuts package

Analogous steps like in Wrapping highlighted text in chosen brackets. Is there no keyboard command for this?

2. The second solution is your initial approach, editing KeyEventTranslations.tr

At the moment of loading those files the FrontEnd doesn't know @:>_ and all other fancy syntactic features. So we have to put there crude expressions:

 Item[
KeyEvent["r", Modifiers -> {Control}],
KernelExecute[
Function[
If[
MatchQ[Slot[1], List[Blank[]]],
CopyToClipboard[First[Slot[1]]],
Beep[]
]
][
Cases[
CurrentValue["SelectionData"],
RuleDelayed[
Blank[][ButtonData, List[Pattern[x, Blank[]], Blank[]]],
x
],
Infinity
]]
],


This works well.

1. Short 2.

Or just put there

 KernelExecute[ Needs["MyShortcuts"]; MyShortcutsCopyAdress[] ]


after saving MyShortcuts.m with this definition somewhere on \$Path.

Shortcuts` package uses this approach.