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[◻, ◻]
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.
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"]//DisplayFormEDIT: 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 withSystemOpennormally either (linux, kde) – ssch Dec 17 '12 at 21:49SystemOpen). 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:54file://both withSystemOpenand with the button – ssch Dec 17 '12 at 21:55