How to open PDF file in browser (firefox) using static file path and tiddler's title?

Hello,

my goal is to open a PDF within my browser (firefox) via TiddlyWiki. The PDF is stored locally (static file path) and has the same name as the tiddler’s title. Here is my code:

\define filepath() file:///C:/Users/Patrick/MyFiles/{{!!title}}.pdf

<$button>
<$action-sendmessage $message="tm-open-external-window" $param=<<filepath>> windowName="_tiddlywiki"  windowFeatures="height=1000, width=1250, top=0, left=0"/>
Open ~TiddlyWiki - Action
</$button>

However, the output is this:

image

Even though the command <<filepath>> outputs the correct file path, it is not recognized by my code above.

I would be glad about any kind of help:)

OS: Windows 10, Browser: Latest Firefox, Tiddlywiki: 5.2.0-prerelease

Hello @TiddlyPatrick
Tobias Beer has a lot of solutions to a variety of questions on TiddlyWiiki.
I think this may help.

https://tobibeer.github.io/tb5/#External%20Image%20Path:Solutions%20[[External%20Image%20Path]]%20[[Embedded%20External%20PDF]]%20[[Local%20Folder%20Contents]]

Scot

Macros only do two things:

  1. replace instances of $arg$ with values passed into the macro
  2. replace instances of $(var)$ with values defined as variables outside the macro.

It is up to the “calling context” to determine what to do with the macro’s “return” value. When you display <<filepath>> as wikitext it is being “wikified” by the TWCore’s parser and {{!!title}} is replaced by the current tiddler’s title, as expected.

However, when you use <<filepath>> as the $param value in the <$action-sendmessage> widget, it is not “wikified”, and is simply passed along to the widget for further processing. Thus, the {{!!title}} syntax in the macro definition isn’t being replaced by the current tiddler’s title, giving the erroneous results you have observed.

Fortunately, for your specific use-case, the current tiddler’s title is always automatically defined as a variable, <<currentTiddler>>, which can be referenced inside the macro using $(currentTiddler)$, which IS replaced by the macro processing, as noted above.

Give this a try:

\define filepath() file:///C:/Users/Patrick/MyFiles/$(currentTiddler)$.pdf

Alternatively, you could leave the filepath() macro as-is, and use the $wikify widget to explicitly force the {{!!title}} reference to be parsed before passing it along as the $param value, like this:

\define filepath() file:///C:/Users/Patrick/MyFiles/{{!!title}}.pdf

<$button>
<$wikify name="thispath" text=<<filepath>> >
<$action-sendmessage $message="tm-open-external-window" $param=<<thispath>> windowName="_tiddlywiki"  windowFeatures="height=1000, width=1250, top=0, left=0"/>
Open ~TiddlyWiki - Action
</$wikify>
</$button>

enjoy,
-e

2 Likes

I’ve thrown out the concept before but it’s been a little while :slight_smile: An enhancement to the macro syntax that added wikification would probably solve a lot of these new-user needs. I suspect it’s a bad idea (I know the wikify widget has performance implications…), but just to throw it back out there:

Meaning:
<<<mymacro>>> would be the equivalent to:

<$wikify name="mymacro-wikified" text=<<mymacro>> >
<<mymacro-wikified>>
</$wikify>

Stobot, Totally agree with the approach, any acceptable delimiters. For inline wikify.

Even just a short cut as {{tiddler}} is a short form of "$transclude tiddler="but one that wikifies the content first, we need it inside filters to.

@TiddlyPatrick I have not used tm-open-external-window yet and there is a gap in the doco which never details the use of paramObject and the statement “Hashmap of Variables”. Is it working for you except for the problem Eric solved?

Thank you all for the help and sorry for the delayed response.

Since I am a noobie, I tested the solution by @EricShulman and created a summary (Version 3 and Version 4 are my favorites):

Summary

Note: The the tiddler has the same title as the PDF (without the ending .pdf).

Version 1: Using variable $(var)$ and absolute file path :+1: :green_circle: works perfectly!

\define filepath_v1() file:///C:/Users/Patrick/MyFiles/$(currentTiddler)$.pdf

<$button>
<$action-sendmessage $message="tm-open-external-window" $param=<<filepath_v1>> windowName="_tiddlywiki"  windowFeatures="height=1000, width=1250, top=0, left=0"/>
Open filepath_v1
</$button>

Version 2: Using transclusion {{!!title}} with $wikify widget and absolute file path :+1: :green_circle: works perfectly!

\define filepath_v2() file:///C:/Users/Patrick/MyFiles/{{!!title}}.pdf

<$button>
<$wikify name="thispath" text=<<filepath_v2>> >
<$action-sendmessage $message="tm-open-external-window" $param=<<thispath>> windowName="_tiddlywiki"  windowFeatures="height=1000, width=1250, top=0, left=0"/>
Open filepath_v2
</$wikify>
</$button>

Version 3: Same as Version 1 just with relative path according to docu under External Links. The PDF is in the folder assets :+1: :green_circle: works perfectly!

\define filepath_v3() ./assets/$(currentTiddler)$.pdf

<$button>
<$action-sendmessage $message="tm-open-external-window" $param=<<filepath_v3>> windowName="_tiddlywiki"  windowFeatures="height=1000, width=1250, top=0, left=0"/>
Open filepath_v3
</$button>

Version 4: Alternative to </$button> using rectangle shape with href
:+1: :green_circle: works perfectly!

\define filepath_v4() ./assets/$(currentTiddler)$.pdf

<div style="font-size:0.5em;text-align:left;margin:0em auto;">
<a href=<<filepath_v4>> class="tc-btn-big-green" style="border-radius:4px;background-color:#696969;" target="_blank" rel="noopener noreferrer">
{{$:/core/images/file}} ''Open File''
</a>
</div>

Rendered as:
image


Extra note:

  1. The same also works for images. Only the file ending under the command /define has to be changed to .png, .jpg, etc.

  2. Using \define filepath() $(currentTiddler)$.pdf automatically creates the full file path to the TiddlyWiki.html file adding $(currentTiddler)$.pdf to the file path.
    For example:
    define filepath() $(currentTiddler)$.pdf
    results in
    file:///C:/Users/Patrick/MyFiles/$(currentTiddler)$.pdf
    and if the PDF is in a subfolder
    define filepath() assets/$(currentTiddler)$.pdf
    results in
    file:///C:/Users/Patrick/MyFiles/assets/$(currentTiddler)$.pdf
    Both examples work.

  3. Using the latest Firefox version under:
    Windows: The file opens in a new window.
    MacOS: The file opens in a new tab using the same code (I do not know why).

2 Likes