Create a tiddler if not existed: Issue with tiddler its title contains space like "Hello there"

57 views
Skip to first unread message

Mohammad

unread,
Sep 28, 2018, 9:24:17 AM9/28/18
to tiddl...@googlegroups.com
I have a macro as below. When you call it with a tiddler name, it will check to see if the tiddler exists
  • If exist it will navigate to that tiddler
  • If not, it will call another macro to create it and then navigate to it

\define create-note(tname)
  <$set name="myTid" filter="[[$tname$]addsuffix[-math]]">
    <$list filter="[title<myTid>] +[has[title]]" 
        variable="newTid" 
        emptyMessage=<<tidller-not-exist $tname$>>
    >
        <$action-navigate $to=<<newTid>> />
    </$list>
  </$set>
\end
\define tiddler-not-exist(parent)
  <$action-setfield 
    $tiddler= {{{$(myTid)$}}}
    text="Hi, this is a new note"
    tags="notes"
  />
    <$action-navigate $to={{{$(myTid)$}}} />
\end



The problem is the macro fails when you pass a name with space as parameter to it. It create the tiddler and overwrite the existed one!

  • Works
<$button> Hi
<<create-note "Hi">>
</$button>

  • Fails
<$button> Hello there
<<create-note "Hello there">>
</$button>



Jed Carty

unread,
Sep 28, 2018, 10:27:29 AM9/28/18
to TiddlyWiki
There are a few things.

First, the working version:

\define create-note(tname)
  <$set name="myTid" filter="[[$tname$]addsuffix[-math]]" select=0>
    <$list filter="[title<myTid>] +[has[title]]" 
        variable="newTid" 
        emptyMessage=<<tiddler-not-exist>>
    >
        <$action-navigate $to=<<newTid>> />
    </$list>
  </$set>
\end
\define tiddler-not-exist()
  <$action-setfield 
    $tiddler=<<myTid>>
    text="Hi, this is a new note"
    tags="notes"
  />
    <$action-navigate $to=<<myTid>>/>
\end

<$button>
Test
<<create-note "Bob Joe">>
</$button>

what I changed:

- A typo in the emptyMessage macrocall
- replace {{{myTid}}} with <<myTid>> and add select=0 to the set widget to prevent it from adding [[ and ]] around the output. Using {{{$(myTid)$}}} evaluates it as a space separated list so any title with a space would be two (or more) separate things, not a single title.
- remove parent as an input to tiddler-not-exist and remote the unneeded input from the emptyMessage macrocall because it isn't used

Alternately, this could have fixed it:
- Using """$(myTid)$""" to make a string instead of {{{$(myTid)$}}} to evaluate a filter. This is one place that would break if you had a space, {{{bob joe}}} is returned as a list with two entries, "bob" and "joe" where """bob joe""" is a string and can be used as a single title. I am using """ because that lets you put " and ' into the title if you want to for some reason and (hopefully) """ is less common in titles.

Mohammad

unread,
Sep 28, 2018, 2:46:39 PM9/28/18
to tiddl...@googlegroups.com
Hello Jed,

 Thank you very much! It works like a charm!
I would also appreciate your explanation on how {{{}}} works and why """" is used here!

- Mohammad
Reply all
Reply to author
Forward
0 new messages