Hi
I have been working some code to open an import dialog box so I can choose a file to be imported. Then i want the title of the imported file to be written to the field importname.
This is the latest version of my code but it does not save the filename to the field.
\procedure getname()
<$set name="open" value="[[">
<$set name="close" value="]]">
{{{ [[$:/Import]get[text]split<open>split<close>nth[2]] }}}
</$set>
</$set>
\end
<$messagecatcher tm-import-tiddlers=<<getname>>>
<$action-setfield importname=<<getname>>/>
<$browse/>
</$messagecatcher>
Help would be appreciated.
Cheers, Rob
When a procedure is used as a widget parameter, the parameter value is not “wikified”. Thus, the value is literally a copy of the procedure’s code, rather than the procedure’s rendered result. To capture the procedure’s result, you can use the $wikify widget.
Try this:
\procedure getname()
<$set name="open" value="[[">
<$set name="close" value="]]">
{{{ [[$:/Import]get[text]split<open>split<close>nth[2]] }}}
</$set>
</$set>
\end
<$wikify name="thisname" text="<<getname>>">
<$messagecatcher tm-import-tiddlers=<<thisname>>>
<$action-setfield importname=<<thisname>>/>
<$browse/>
</$messagecatcher>
</$wikify>
Let me know how it goes…
-e
Thanks for your reply @EricShulman
Surprisingly, that does NOT appear to work. The file is imported but its title does not appear to be written to a field.
Any ideas?
Cheers, Rob
My previous response was only focused on how to get the rendered output from <<getname>>.
However, there are several major reasons your overall approach doesn’t work:
-
The message catcher syntax requires a leading $ before the message name. Use $tm-import-tiddlers instead of tm-import-tiddlers
-
You need to invoke <<getname>> from within the $messagecatcher actions, so it can actually retrieve the desired importname value when catching the tm-import-tiddlers message.
-
$action-setfield can’t be used “on its own”. It has to be triggered by something like a $button widget or used within an actions="..." parameter of some other widget (such as the $messagecatcher widget).
-
Your getname() procedure assumes that $:/Import contains the result of processing the import action. However, when the $messagecatcher traps the tm-import-tiddlers message, the $:/Import tiddler has not been created yet, so there is nothing to extract a filename from.
-
Fortunately, there is another way to get the filename: The $messagecatcher handling automatically defines an event-param variable whose value comes from the tm-import-tiddlers message sent as a result of the $browse widget interaction. This event-param variable contains something like this:
[{"title":"filename.ext","text":"...base64 encoded binary...","type":"image/gif"}]
- You can extract the
filename.ext value using the following filter syntax:
{{{ [<event-param>split["title":"]nth[2]split["]nth[1]] }}}
- The
$messagecatcher widget “eats” the tm-import-tiddlers message, so it never triggers the $:/Import processing. To let the message handling continue onward, we can “re-send” the tm-import-tiddlers message, like this:
<$action-sendmessage $message="tm-import-tiddlers" $param=<<event-param>>/>
- The result of this
$action-sendmessage is that the $:/Import tiddler appears as usual, and you can proceed to actually import the chosen file.
Putting all of the above together, we get something like this:
\procedure getname()
<$let name={{{ [<event-param>split["title":"]nth[2]split["]nth[1]] }}}>
<$action-setfield importname=<<name>>/>
<$action-sendmessage $message="tm-import-tiddlers" $param=<<event-param>>/>
\end
<$messagecatcher $tm-import-tiddlers="<<getname>>"><$browse/></$messagecatcher>
where getname() now does the following:
- First, extract the
name from the <event-param> variable
- Next, set the
importname field in the current tiddler to the extracted <<name>> value.
- Then, re-invoke the
tm-import-tiddlers message to trigger the $:/Import tiddler.
The result is that when you press the browse button and select a file, the filename is first saved in the importname field of the current tiddler, and then the $:/Import tiddler is displayed to complete the import process. Note that if you cancel the import process, the importname field will still contain the filename that you chose in the “browse file” dialog, even though the file wasn’t actually imported.
Q.E.D.
enjoy,
-e
Many thanks for the explanation @EricShulman. Clearly I need to learn more about trigger mechanisms and their timings.
That script worked as expected. Thankyou.
I need to remember not to change the name of the tiddler in the import dalog box or there will be a mismatch with the title stored in importname
Cheers, Rob