How to export a tiddler using a script

Hi there

I want to be able to export a tiddler containing geospatial data as a .tid file using a script triggered by a button.

I have done some searching but cannot find any useful information.

Is this possible?

Cheers, Rob

Give this a try:

<$let  tid="SomeTiddler"
	format="$:/core/templates/exporters/TidFile"
	target={{{ [<tid>slugify[]] [<format>get[extension]] +[join[]] }}}>
<$button>
	export <<tid>> to .TID file
	<$action-sendmessage $message=tm-download-file
		$param=<<format>> exportFilter=<<tid>> filename=<<target>>/>
</$button>

Notes:

  • tid is the title of the tiddler you want to export
  • format is the TWCore shadow that defines the format of the file content to output when exporting a TID file
    • if you wanted to create a JSON file, you would use “$:/core/templates/exporters/JsonFile”
  • target assembles the <tid> and <format> values to construct the desired filename
    • the tiddler title is “slugified” to remove or replace any characters that are not permitted in filenames
    • the appropriate filename extension (i.e. “.tid”) is defined by a field in the specified <format> tiddler
  • the tm-download-file message triggers the TWCore’s download saver for the specified tiddler, using the desired exporter format and target filename.

References:

enjoy,
-e

Addendum:

In order to handle a tiddler title that contains embedded spaces (e.g., “Some Tiddler” instead of “SomeTiddler”), we need to apply format:titlelist[] to the tiddler title. This adds [[ and ]] around the title so it will be treated as a single title rather than two separate titles ("Some" and “Tiddler”).

One subtle issue:

The tm-download-file message is processed asynchronously by the TWCore’s download saver handler. Because of this, we can’t simply pass exportFilter="[<tid>format:titlelist[]]" as a message parameter, since the <tid> variable will not be available (“in scope”) when the tm-download-file message is actually processed.

To work around this we compute a title variable ahead of time (title={{{ [<tid>format:titlelist[]] }}}) – which produces “[[Some Tiddler]]” as its output – and then pass that computed title variable as a message parameter (i.e, exportFilter=<<title>>)

Here’s the updated script code:

<$let  tid="Some Tiddler" title={{{ [<tid>format:titlelist[]] }}}
	format="$:/core/templates/exporters/TidFile"
	target={{{ [<tid>slugify[]] [<format>get[extension]] +[join[]] }}}>
<$button>
	export <<tid>> to .TID file
	<$action-sendmessage $message=tm-download-file
		$param=<<format>> exportFilter=<<title>> filename=<<target>>/>
</$button>

enjoy,
-e

Perfect!

I knew that if anyone had the answer it would be @EricShulman

I really appreciate the explanatory notes, even if I don’t understand all the nuances.

Thank you so much. Cheers, Rob