# How to insert a link to a generic file, in the middle of a text style cell/paragraph?

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?

-
 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 – m_goldberg Dec 17 '12 at 21:35 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-s‌​tyle-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) – ssch Dec 17 '12 at 21:49 @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. – P. Fonseca Dec 17 '12 at 21:54 Hm, pdf files works with file:// both with SystemOpen and with the button – ssch Dec 17 '12 at 21:55 @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. – P. Fonseca Dec 17 '12 at 22:01

Row[{"An example of a button inside text that opens the file ",
SystemOpen["C:\\Program Files\\Wolfram Research\\
Mathematica\\8.0\\Documentation\\English\\System\\ExampleData\\
spikey2.png"], Appearance -> None], " using ",


or

 StringForm["An example of a button inside text that opens
the file 1 using 2.",
SystemOpen["C:\\Program Files\\Wolfram Research\\
Mathematica\\8.0\\Documentation\\English\\System\\
ExampleData\\spikey2.png"], Appearance -> None],

-

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

   Cell[TextData[{
"This is ",
Cell[BoxData[
FormBox[
ButtonBox[
RowBox[{"man", ".", "pdf"}],
ButtonFunction:>SystemOpen[
FileNameJoin[{
NotebookDirectory[], "man.pdf"}]],
Evaluator->Automatic,
" next to this notebook."
}], "Text"]


and a button to do so could be

NotebookPut[Notebook[{Cell[
With[List[Set[$CellContextfile, SystemDialogInput["FileOpen", "*"]], Set[$CellContextdescr, InputString["add description here"]]],
Cell[BoxData[FormBox[ButtonBox[$CellContextdescr, Rule[BaseStyle, "Hyperlink"], RuleDelayed[ButtonFunction, SystemOpen[$CellContextfile]],
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"]]

-
 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”) :) – P. Fonseca Dec 17 '12 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[{"externalLabelWithURI[",
RowBox[{"\[SelectionPlaceholder]", ",", "\[Placeholder]"}],
"]"}]}];
SetOptions[EvaluationNotebook[],
InputAliases -> newAliases];

externalLabelWithURI[text_, uri_] :=
NotebookWrite[SelectedNotebook[],
ToBoxes[EventHandler[
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[◻, ◻]

• 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
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.