Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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

3 Answers 3

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

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

and a button to do so could be

NotebookPut[Notebook[{Cell[
     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"]]
share|improve this answer
    
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
Row[{"An example of a button inside text that opens the file ", 
  Button[Style["spikey2.png", "Hyperlink"], 
   SystemOpen["C:\\Program Files\\Wolfram Research\\
  Mathematica\\8.0\\Documentation\\English\\System\\ExampleData\\
  spikey2.png"], Appearance -> None], " using ", 
   Hyperlink["SystemOpen", "paclet:ref/SystemOpen"], "."}]

enter image description here

or

 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\\
   Mathematica\\8.0\\Documentation\\English\\System\\
   ExampleData\\spikey2.png"], Appearance -> None], 
   Hyperlink["SystemOpen", "paclet:ref/SystemOpen"]]
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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