# How insert hyperlink to file with relative path?

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 ",
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?

• Follow those steps and put whatever you want to the Hyperlink. mathematica.stackexchange.com/a/37518/5478 – Kuba May 10 '16 at 22:03
• – kglr May 11 '16 at 2:36
• @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. – murray May 11 '16 at 21:02
• @murray What about SystemOpen @ FileNameJoin[... only? I'm not quite sure where is this file:// part from and whether it should work or not. – Kuba May 11 '16 at 21:09
• 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? – murray May 11 '16 at 21:40

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

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


Original post:

Using Button:

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


Using Hyperlink:

Row[{Style["Look at this ", "Panel", 16],

• 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). – murray May 11 '16 at 20:46
• 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. – murray May 11 '16 at 21:45
• @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->...`. – kglr May 11 '16 at 21:53