3
$\begingroup$

I'm trying to map a keyboard shortcut to google search the contents of the cell I am typing. Here is my code so far:

MenuItem["Google",
       FrontEndExecute[{FrontEnd`SelectionMove[FrontEnd`EvaluationNotebook[], Cell, FrontEnd`InputCell],
       KernelExecute[
        $myContent = FrontEndExecute[FrontEnd`NotebookRead[FrontEnd`EvaluationNotebook[]]];
        $myContent = $myContent[[1,1]];
        googleSearch[$myContent];
    ]}],
    MenuKey["l", Modifiers -> {"Control", "Shift"}], MenuEvaluator -> "Local"]

googleSearch[search_String] := SystemOpen["https://www.google.com/#q="<>URLEncode[search]]

Note: I don't want to have to select the cell with my mouse first. I'm thinking of using the FE token "ExpandSelection".

$\endgroup$
3
$\begingroup$

I was right using ExpandSelection twice does the trick, but not the prettiest answer, but anyway, just add this to MenuSetup.tr:

MenuItem["Google",
FrontEndExecute[{
FrontEndTokenExecute["ExpandSelection"],
FrontEndTokenExecute["ExpandSelection"],
KernelExecute[
    $myContent = FrontEndExecute[FrontEnd`NotebookRead[FrontEnd`InputNotebook[]]];
    googleSearch[$myContent[[1]]];
]}],
MenuKey["l", Modifiers -> {"Control", "Shift"}], MenuEvaluator -> "Local"],
$\endgroup$

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.