2
$\begingroup$

Into a text cell of my current (previously saved) notebook, I want to place a hyperlink to a particular file target.html in a particular subdirectory subdir of the notebook's directory.

I don't see a way to do this directly using the menu item Insert > Hyperlink.

However, if I edit the cell as an expression, I can form the following:

Cell[TextData[{
   "Look at this ",
   ButtonBox["page", BaseStyle->"Hyperlink",
      ButtonData:>{
         URL[
              StringJoin["file://", 
                  FileNameJoin[{NotebookDirectory[], "subdir/target.html"}]
                  ]
            ], 
         None}],"..."
     }], "Text"]

(Above, I've hand-formatted the display to correctly display the structure, I hope.)

However, once I convert that expression back into a text cell in the notebook and click the hyperlink, the indicated file does not open in the default browser.

What's wrong and how should it be fixed?

$\endgroup$
5
  • $\begingroup$ Follow those steps and put whatever you want to the Hyperlink. mathematica.stackexchange.com/a/37518/5478 $\endgroup$
    – Kuba
    Commented May 10, 2016 at 22:03
  • $\begingroup$ related q/a: how to open Hyperlinked files by default software? $\endgroup$
    – kglr
    Commented May 11, 2016 at 2:36
  • $\begingroup$ @Kuba:I presume you refer to your own answer in the post cited. For argument "testlink", I use "file://"<>FileNameJoin[{NotebookDirectory[],"subdir/target.html"}]. But when I click the resulting hyperlink, I get error "The front end failed to execute the command file File:///Users/murray/dir/subdir/target.html", where dir is the notebook's directory. $\endgroup$
    – murray
    Commented May 11, 2016 at 21:02
  • $\begingroup$ @murray What about SystemOpen @ FileNameJoin[... only? I'm not quite sure where is this file:// part from and whether it should work or not. $\endgroup$
    – Kuba
    Commented May 11, 2016 at 21:09
  • $\begingroup$ I think I've traced the source of the difficulty to the notebook's directory name have a space within it. if I remove that space, then the method works, namely, to evaluate in-line a Hyperlink["test","file:///"<>FileNameJoin[{NotebookDirectory[],"subdir/target.html"}]]. (And this has nothing to do with the target file being in a subdirectory of the notebook's directory.) The issue is that the expression generates a link with an embedded space. How to fix? $\endgroup$
    – murray
    Commented May 11, 2016 at 21:40

1 Answer 1

1
$\begingroup$

Just use Rule (->) instead of RuleDelayed (:>)

Cell[TextData[{
   "Look at this ",
   ButtonBox["page", BaseStyle->"Hyperlink",
      ButtonData->{
         URL[StringJoin["file://", 
                  FileNameJoin[{NotebookDirectory[], "subdir/target.html"}]
                  ]
            ], 
         None}],"..."
     }], "Text"] //CellPrint

Mathematica graphics


Original post:

Using Button:

Row[{Style["Look at this ", "Panel", 16], 
  Button[Dynamic@Style["page", 16, If[CurrentValue["MouseOver"], 
         "HyperlinkActive", "Hyperlink"]], 
   SystemOpen[FileNameJoin[{NotebookDirectory[], "subdir/target.html"}]], 
   Appearance -> None], " ..."}]

Mathematica graphics

Using Hyperlink:

Row[{Style["Look at this ", "Panel", 16], 
  Hyperlink[Style["page", FontFamily->"Panel", 14], 
   "file://" <> FileNameJoin[{NotebookDirectory[], "subdir/target.html"}]], 
  " ..."}]

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ Neither of those input cells produce an output that is, or is within a Text cell. I need the hyperlink within an existing Text cell (and, in fact, one that could have a fair amount of text already in it). $\endgroup$
    – murray
    Commented May 11, 2016 at 20:46
  • $\begingroup$ @murray, ah i see. I will update in a moment. $\endgroup$
    – kglr
    Commented May 11, 2016 at 21:09
  • $\begingroup$ I cannot effectively create a Cell expression and apply CellPrint to it: I already have the text cell and need to add a hyperlink to it. $\endgroup$
    – murray
    Commented May 11, 2016 at 21:45
  • $\begingroup$ @murray, i used CellPrint just to show how the cell will look like when you uncheck the Show Expression in the Cell menu. The only change you need to make in your code is to change ButtonData:>... to ButtonData->...`. $\endgroup$
    – kglr
    Commented May 11, 2016 at 21:53

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.