11
$\begingroup$

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.

$\endgroup$
7
  • 4
    $\begingroup$ 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? $\endgroup$
    – user21
    Commented Mar 7, 2012 at 8:08
  • $\begingroup$ 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. $\endgroup$
    – Andrew
    Commented Mar 7, 2012 at 14:20
  • 1
    $\begingroup$ It's possible, if one has no other option, to Show Expression (Ctrl+Shift+e) and copy the address from there. $\endgroup$
    – Andrew
    Commented Mar 7, 2012 at 14:23
  • $\begingroup$ 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? $\endgroup$
    – celtschk
    Commented Mar 8, 2012 at 17:17
  • 3
    $\begingroup$ 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. $\endgroup$
    – John Fultz
    Commented Mar 9, 2012 at 7:55

1 Answer 1

3
$\begingroup$

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
        ]]      
    ], MenuEvaluator -> Automatic
],

This works well.

  1. Short 2.

    Or just put there

     KernelExecute[ Needs["MyShortcuts`"]; MyShortcuts`CopyAdress[] ]
    

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

    Shortcuts` package uses this approach.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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