It is easy to insert a Hyperlink to a notebook file, or to an internet address, in the middle of a text style cell (Ctrl+Shift+H or menu: Insert->hyperlink).

Trying to create a hyperlink to a file extension different than a notebook (e.g. a PDF file), after waiting for a while, returns nothing (...should it return an error message?).

Converting a hyperlink (e.g. for a website) into InputForm, Raw InputForm or FullForm doesn't reveal any secrets (returns a string). One needs to go to the cell expression level to see what makes it different.

So, what is the best way of adding a "SystemOpen link" in the middle of a text paragraph?

  • $\begingroup$ If your intent is to open a PDF file, do you expect to open it in a Mathematica window, or do you expect to open it PDF reader app? A Mathematica hyperlink will attempt to do the first and will sometimes succeed. If it fails, you will get an error message in the Message window $\endgroup$
    – m_goldberg
    Commented Dec 17, 2012 at 21:35
  • $\begingroup$ ButtonBox["Inserting links",BaseStyle->"Hyperlink",ButtonData->{"http://mathematica.stackexchange.com/questions/16493/how-to-insert-a-link-to-a-generic-file-in-the-middle-of-a-text-style-cell-parag",None}, ButtonFunction->(SystemOpen[First@#2]&), Evaluator->Automatic, ButtonNote->"Link"]//DisplayForm EDIT: oh my this got ugly when inserted in a comment, either way I can only use it to open "http://" stuff but not "file://" stuff, but I can't do that with SystemOpen normally either (linux, kde) $\endgroup$
    – ssch
    Commented Dec 17, 2012 at 21:49
  • $\begingroup$ @m_goldberg I expect it to open the system predefined app for that specific file format (SystemOpen). I mentioned PDF as an example, but it can be a DOC, DGN, etc. Imagine a slideshow or a book where you want to call from the middle of the text another type of file. Now that you mention it, we could even extend this need to the possibility of choosing a program different than the OS predefined one. But it would already be very nice to have the same functionality that every MS Office app has, or even better, that can be done in a simple HTML file. $\endgroup$
    – P. Fonseca
    Commented Dec 17, 2012 at 21:54
  • $\begingroup$ Hm, pdf files works with file:// both with SystemOpen and with the button $\endgroup$
    – ssch
    Commented Dec 17, 2012 at 21:55
  • $\begingroup$ @ssch the "ugly" code seems to work. It is not the most practical thing, but I can copy the output to the middle of a text style cell. $\endgroup$
    – P. Fonseca
    Commented Dec 17, 2012 at 22:01

3 Answers 3

Row[{"An example of a button inside text that opens the file ", 
  Button[Style["spikey2.png", "Hyperlink"], 
   SystemOpen["C:\\Program Files\\Wolfram Research\\
  spikey2.png"], Appearance -> None], " using ", 
   Hyperlink["SystemOpen", "paclet:ref/SystemOpen"], "."}]

enter image description here


 StringForm["An example of a button inside text that opens
    the file `1` using `2`.", 
 Button[Style["spikey2.png", "Hyperlink"], 
   SystemOpen["C:\\Program Files\\Wolfram Research\\
   ExampleData\\spikey2.png"], Appearance -> None], 
   Hyperlink["SystemOpen", "paclet:ref/SystemOpen"]]

Suppose your pdf is next to the notebook, then you could do, simply:

     "This is ",
        RowBox[{"man", ".", "pdf"}],
            NotebookDirectory[], "man.pdf"}]],
        Method->"Preemptive"], TraditionalForm]]],
     " next to this notebook."
    }], "Text"]

and a button to do so could be

     BoxData[ButtonBox["SuperLink", RuleDelayed[ButtonFunction, NotebookWrite[SelectedNotebook[], 
          With[List[Set[$CellContext`file, SystemDialogInput["FileOpen", "*"]], Set[$CellContext`descr, InputString["add description here"]]], 
               Cell[BoxData[FormBox[ButtonBox[$CellContext`descr, Rule[BaseStyle, "Hyperlink"], RuleDelayed[ButtonFunction, SystemOpen[$CellContext`file]], 
               Rule[Evaluator, Automatic], Rule[Method, "Preemptive"]], TraditionalForm]]]]]], Rule[Evaluator, Automatic]]]
     , FontFamily :> CurrentValue["PanelFontFamily"], FontSize :> CurrentValue["PanelFontSize"]]}, WindowSize -> {Fit, Fit}, 
   WindowMargins -> {{100, Automatic}, {Automatic, 20}}, WindowFrame -> "Palette", WindowElements -> {}, WindowFrameElements -> {"CloseBox", "MinimizeBox"}, 
   StyleDefinitions -> "Palette.nb"]]
  • $\begingroup$ Good enough. Now all I need is a palette bottom that calls the system choose file dialog box, and then places the link at the current input point (with a description like “add description here”) :) $\endgroup$
    – P. Fonseca
    Commented Dec 17, 2012 at 22:07

Another way to streamline the process of entering external hyperlinks is to use regular text wrapped in EventHandler.

To make it possible to type such links anywhere, I chose to use an input alias, which I first have to add to the options of the currently used notebook:

aliases = Options[EvaluationNotebook[], InputAliases];
newAliases = 
  Join[InputAliases /. 
    aliases, {"href" -> 
       RowBox[{"\[SelectionPlaceholder]", ",", "\[Placeholder]"}], 
  InputAliases -> newAliases];

externalLabelWithURI[text_, uri_] := 
     Style[HoldForm[text], Underlined, 
      FontColor -> Blue], {"MouseClicked" :> SystemOpen[uri]}]]];
SetAttributes[externalLabelWithURI, HoldAll]

Let's say you have the file test.pdf created by

Export["test.pdf", "Hello"];

Now you can just start typing in an input or text cell, until you want to enter the hyperlink:

This is ◻

where the square is the placeholder indicating the start of an inline cell (I pressed Ctrl-9). Next you enter eschrefesc and the line changes to

This is externalLabelWithURI[◻, ◻]

and your cursor should already be in the first placeholder.

  • Here, you enter the label text (no quotation marks needed because it is kept in HoldForm.
  • Next press Tab to get to the next placeholder
  • Enter the URI for the pdf file, which in our case is simply the filename (but in quotation marks this time):

This is externalLabelWithURI[an external PDF file, "test.pdf"]

  • Press Ctrl-. four times to expand the selection until the whole command above is highlighted
  • Press Command-Return to evaluate the selection.
  • The current line should now change to look like this:

This is an external PDF file

where the label text is blue and underlined. If you click on the label text, the external PDF file should open.

If you don't like the link style, you can now go into the label text (using the cursor, obviously, because clicking will activate the link) and change its font style or color, or even the label text, as if it were regular text (because it is). All this editing can be done without losing the link.


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.