Tip: Create Tiddler with unique title and sequence number

278 views
Skip to first unread message

Mohammad

unread,
Feb 22, 2019, 1:41:39 AM2/22/19
to tiddl...@googlegroups.com
Tiddlywiki 5.1.20pre

Assume you want to create tiddlers all tagged with say myTag and have unique title with ordinal number.
So

  1. tiddlers have title like myTiddler-1
  2. tiddlers all tagged with myTag
Thanks to Math operator in TW 5.1.20, this is quite simple to implement


\define newTitle() myTiddler-{{{[tag[myTag]count[]add[1]]}}} 
\define newTiddler()
<$wikify name="myTitle" text="<<newTitle>>" >
<$action-createtiddler $basetitle=<<myTitle>> tags="myTag" /> 
</$wikify>
\end

Then use it like below

<$button actions=<<newTiddler>>>
Create a new tiddler
</$button>



You can customize this code to do more interesting jobs! 

--Mohammad

Mohammad

unread,
Feb 22, 2019, 1:53:12 AM2/22/19
to TiddlyWiki
The post edited and the issue removed. For who receive this through email.
See the original link:

Thomas Elmiger

unread,
Feb 22, 2019, 5:16:35 AM2/22/19
to TiddlyWiki
Hi Mohammad

I see a risk here in the count part:

Say you have made tid-1, tid-2 and tid-3. Then you delete tid-2. The next generated number will be 3 and collide with existing tid-3.

Just a thought.

Cheers,
Thomas

TonyM

unread,
Feb 22, 2019, 5:48:31 AM2/22/19
to TiddlyWiki
Mohammed,

It is good to see what we will be able to do with the new opperators but the existing widgets can handle adding new tiddlers and incrementing n after the title.

Have you looked at that?

Tony

Mohammad

unread,
Feb 22, 2019, 8:19:54 AM2/22/19
to TiddlyWiki
Hello Thomas, that is quite right and the code needs to check if the tiddler is existed or not!
By the way, for processing in batch mode, when there is no risk of deletion this may be useful.

I will revise the code and send a new version.

--Mohammad

Mohammad

unread,
Feb 22, 2019, 8:24:38 AM2/22/19
to TiddlyWiki
Hi Tony,
 This can be used for logging process. Also you can control the title you create like Tid-001 Tid-003 and like that.

--Mohammad

Mohammad

unread,
Feb 22, 2019, 9:13:00 AM2/22/19
to tiddl...@googlegroups.com
Thomas,
 I checked the code, if you delete tid-2, it will try to create tid-3, but as there is a tid-3, it creates tid-3 1
SO there is no risk of overwriting.

I can add extra code to check the existence and if it is existed, the code increments the number to find the next empty number!


See below

\define newTitle() myTiddler-{{{[tag[myTag]count[]add[$(inc)$]]}}} 

\define newTiddler(n:0)
<$set name="inc" value={{{ [<__n__>add[1]] }}}>
<$wikify name="myTitle" text="<<newTitle>>" >
<$list filter="[<myTitle>] +[!has[title]]" 
emptyMessage=""" <$macrocall $name="newTiddler" n=<<inc>> /> """>
<$action-createtiddler $basetitle=<<myTitle>> tags="myTag" />
</$list>
</$wikify>
</$set>
\end

  • The list widget now checks if a tiddler with the new title is exist or not
  • If the tiddler is existed, then it increments by one and continues for the next sequence number
  • It will then create a tiddler with unique name

-- Mohammad

On Friday, February 22, 2019 at 1:46:35 PM UTC+3:30, Thomas Elmiger wrote:

Mohammad

unread,
Feb 22, 2019, 9:15:46 AM2/22/19
to TiddlyWiki
The post edited. See the original.


This is for who receives posts by email.


Mohammad

Mal

unread,
Feb 22, 2019, 10:24:50 PM2/22/19
to TiddlyWiki
Hi Mohammad,

An alternative approach - requires all numbers to be zero padded to fixed number of digits.  Assuming 3 digits:

\define newTitle() myTiddler-{{{[tag[myTag]removeprefix[myTiddler-]sort[]last[1]add[1]addprefix[0000]split[]last[3]join[]]}}}
\define newTiddler()
<$wikify name="myTitle" text="<<newTitle>>" >
<$action-createtiddler $basetitle=<<myTitle>> tags="myTag" />
</$wikify>
\end


This should always increment the title with the largest number and is not affected by deleting other tiddlers.

Regards,

Mal

Mohammad

unread,
Feb 23, 2019, 1:47:08 AM2/23/19
to TiddlyWiki
Thank you Mal,
This is much better than Tid-1, Tid2,...

Added to TW-Scripts.

-Mohammad

Mohammad

unread,
Feb 23, 2019, 2:10:20 AM2/23/19
to TiddlyWiki
Mal,

 Give a try to below code

\define newTitle() myTiddler-{{{[[0]count[]add[$(inc)$]addprefix[0000]split[]last[3]join[]]}}}


\define newTiddler(n:0)
<$set name="inc" value={{{ [<__n__>add[1]] }}}>
<$wikify name="myTitle" text="<<newTitle>>" >
<$list filter="[<myTitle>] +[!has[title]]"
emptyMessage
=""" <$macrocall $name="newTiddler" n=<<inc>> /> """>
<$action-createtiddler $basetitle=<<myTitle>> tags="myTag" />
</$list>
</
$wikify>
</$set>
\end


<$button actions=<<newTiddler>>>
Create a new tiddler
</$button>



Create 10 tiddlers, and then randomly delete some of them and then again click on the button!
It detects if previous ones are deleted and create with first free sequence number.


--Mohammad




On Saturday, February 23, 2019 at 6:54:50 AM UTC+3:30, Mal wrote:

Mal

unread,
Feb 23, 2019, 6:03:20 AM2/23/19
to TiddlyWiki
Mohammad,

Yes, that's nice - I see a bit of recursion going on here!  It keeps iterating until it finds a title that doesn't exist.  It might be a bit less efficient, but should not have to loop too many times if the total number of tiddlers is small.

By the way, I don't think you need the "count[]" in the newTitle macro filter.  Maybe "[0]count[]" could be replaced with "[1]"?

Regards,

Mal



Mohammad

unread,
Feb 23, 2019, 10:55:48 AM2/23/19
to TiddlyWiki
Mal,
 That is correct, my mistake!

Thank you
Mohammad
Reply all
Reply to author
Forward
0 new messages