Simple check: if a tiddler-A exists do this if not then do that

773 views
Skip to first unread message

Mohammad

unread,
Aug 10, 2018, 1:00:42 PM8/10/18
to tiddl...@googlegroups.com
This is may be repetitive question!

I need a macro to conditionally does the following tasks
If tiddler-A exist
  navigate to it
else
  create tiddler-A
  put some text inside it and tag it with myTag
  navigate to it
end if

 I thought it may be possible using some list widgets

Mohammad

Mark S.

unread,
Aug 10, 2018, 1:21:06 PM8/10/18
to TiddlyWiki
You can create a tiddler name in a filter, and then check if it has an actual title field.

\define nontidexist(tiddler)
$tiddler$ does
not EXIST!
\end
\define tidexist(tiddler)
<$vars tiddler="""$tiddler$""">
<$list filter="[title[$tiddler$]] +[has[title]]" emptyMessage=<<nontidexist """$tiddler$""">>>
$tiddler$ EXISTS
. THEREFORE IT WIKIS.
</$list>
</
$vars>
\end

You can put in the actions you want for existing tiddlers inside the list widget and the actions you want for creating new tiddlers inside the nontidexist macro.

HTH
-- Mark

Mohammad

unread,
Aug 10, 2018, 2:34:04 PM8/10/18
to tiddl...@googlegroups.com
Wow! 
Thank you Mark!
It works and do the job like a charm!
For action command like create tiddler I call the macro using a button!

Much appreciated!

Mark, one more question is it possible to have a decision making structure like below:

If true-this
 macro-action-if-part
else
  macro-action-else-part
end if

Then the header of macro should be something like 
\define dcm(cond, ifpart-action, elsepart-action)

For checking the condition parameter to see if true or false, the setVars widget from Tobias Beer may be useful! If not we can first check the condition outside the macro and send the result (true/false) to it.


Cheers
Mohammad


Diego Mesa

unread,
Aug 10, 2018, 2:57:12 PM8/10/18
to TiddlyWiki
Hey Mohammad,

Just letting you know of Evan's excellent Condition Plugin:


which does that

Mohammad

unread,
Aug 10, 2018, 3:05:18 PM8/10/18
to TiddlyWiki
Hello Diego!
Thanks for the link! It is very close to what I want!
Lets give it a try! 

Best
Mohammad

Mark S.

unread,
Aug 10, 2018, 3:15:19 PM8/10/18
to TiddlyWiki
If by "condition" you mean filter, then this works:

\define if(filter,ifyes,ifno)
<$list filter="""$filter$ +[limit[1]]""" name=act emptyMessage=<<$ifno$>>>
<<$ifyes$>>
</$list>
\end
\define ifyes() YES!
\define ifno() NO!


<<if "[title[GettingStarted]] +[has[title]]" ifyes ifno>>

Well, it worked once ;-)  You would probably need to wrap this invocation in another macro in order to  pass the tiddler name as a parameter.

I always prefer core-based solutions if possible over 3rd party solutions, since core-based solutions are more likely to stand the rigors of time.

-- Mark

Mohammad

unread,
Aug 10, 2018, 3:36:08 PM8/10/18
to TiddlyWiki
Thank you Mark!
Yes, I prefer the same! the core-based solution!
Let me to play and experiment with what you suggest then I will back with results.
I am not sure if all conditions can be implemented using filter or not!
We may need to ask Jeremy to improve the core capability with more robust and simple to understand routine!

Mohammad

Mohammad

unread,
Aug 10, 2018, 3:39:41 PM8/10/18
to TiddlyWiki
Mark one question:
In both solution you proposed you put the filter and tiddler parameters inside triple quotes when you pass them again to another macro or widget!
Whats the reason?

Mohammad

On Friday, August 10, 2018 at 11:45:19 PM UTC+4:30, Mark S. wrote:

Mark S.

unread,
Aug 10, 2018, 6:06:36 PM8/10/18
to TiddlyWiki
I could give a sophisticated answer but really it's mostly because it's what I've seen in other people's code. Monkey see -- monkey code.

-- Mark

TonyM

unread,
Aug 10, 2018, 10:17:11 PM8/10/18
to TiddlyWiki
Mohammad,

I am not sure if this adds to the conversation but 
a tiddler existence test can be written in a filter as follows

[[other]has:field[text]] where the tiddler is called other and has content in the text field

or any other field you know has a value
[[other]has:field[fieldname]]

Interestingly fields other than text return with or without content as long as they exist, not the text field.

Variations
[[other]value[]] with be true if the value field exists on "other" AND is empty, you could put a value [[other]value[testvalue]]

[[other]text[]]  with be true if the value field exists on "other" AND is empty (if you expect the text to be empty this is a good existence test

Also
The quickest way to create a tiddler is to edit it, but it is manual
<$edit-text class='tc-edit-texteditor' tiddler=other field='value'/>
<$edit-text class='tc-edit-texteditor' tiddler=other field='text'/>

Or this
\define note-tiddler() $(currentTiddler)$/notes
<$edit-text class='tc-edit-texteditor' tiddler=<<note-tiddler>> field='text'/>

Anyway,

I am taking these ideas and experimenting further.

Including a ways to deal with tiddler renames. Not withstanding the value of the comments plugin in 5.1.18

Regards
Tony

Mohammad

unread,
Aug 12, 2018, 3:43:41 PM8/12/18
to TiddlyWiki
Thank you both (Mark and Tony!).
I understood that way I can pass complex names and values like myTids/itsname or name'extet 

Mohammad
Reply all
Reply to author
Forward
0 new messages