create new tiddler using a template with unique name

154 views
Skip to first unread message

Mohammad

unread,
Mar 26, 2019, 7:32:48 AM3/26/19
to TiddlyWiki
There are two methods to create new tiddler

* tm-new-tiddler message 
* action-createtiddler

If I want to create a new tiddler as below with UNIQUE title
  1. use a template
  2. set a tag
  3. use a base title
How can I do this? the first item is possible by tm-new-tiddler message  and the last item is possible by action-createtiddler?

--Mohammad

Mohammad

unread,
Mar 26, 2019, 7:36:35 AM3/26/19
to TiddlyWiki
Further input

The below code creates a unique title and accepts fields like tag, etc ... but not template!
I know I can using a more complex code to use template! but I am sure there is much simple way to
* use template
* have unique title
* open it in edit mode

but how?

\define newTiddler(title, fields)
<$set name="tempTid" value=<<qualify "temp/$(currentTiddler)$">> >
<$action-createtiddler 
$basetitle=<<__title__>> 
$savetitle=<<tempTid>>
$fields$/>
<$action-sendmessage $message="tm-edit-tiddler" $param={{{[<tempTid>get[text]]}}} />
</$set>
\end

<$button>Test
<<newTiddler test>>
</$button>

Jed Carty

unread,
Mar 26, 2019, 7:39:53 AM3/26/19
to TiddlyWiki
I don't know of any way to do this currently, but this comes up often enough that I think I should just make a javascript macro that uses the built-in method for creating unique titles that can use a custom base title. If it isn't hard I will post it here in a few minutes.

Jed Carty

unread,
Mar 26, 2019, 8:03:36 AM3/26/19
to TiddlyWiki
Expanding on what you made you can use a template and have everything but the guarantee of a unique title like this:

\define newTiddler(title:'New Tiddler', fields)
<$set name="tempTid" value=<<qualify "$title$">> >
<$button>
<$action-sendmessage $message="tm-new-tiddler" $param='HelloThere' title=<<qualify """$title$""">>  $fields$/>
New Tiddler
</$button>
</$set>
\end


<<newTiddler "bob" "bob=joe eddie=george">>

If you click the button twice you get the same name because the qualify macro uses its place in the widget tree to generate the value, so the button will give the same value every time.

Mohammad

unread,
Mar 26, 2019, 8:03:57 AM3/26/19
to TiddlyWiki
Hi Jed,
 Please share your solution! By the way I have already developed the below scripts, but when is used inside list widget causes the stack error!

\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

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

Jed Carty

unread,
Mar 26, 2019, 8:06:25 AM3/26/19
to TiddlyWiki
Sorry, my previous post didn't have a way to set the template or show adding a tag. Here is the updated version, the title is still not always going to be unique, I will work on that part for a bit now.

\define newTiddler(template:"" title:'New Tiddler', fields)
<$set name="tempTid" value=<<qualify "$title$">> >
<$button>
<$action-sendmessage $message="tm-new-tiddler" $param="""$template$""" title=<<qualify """$title$""">>  $fields$/>
New Tiddler
</$button>
</$set>
\end


<<newTiddler "HelloThere" "bob" "bob=joe eddie=george tags='HELLO'">>


Mohammad

unread,
Mar 26, 2019, 8:12:04 AM3/26/19
to TiddlyWiki
Thanks Jed,
 Yes, I need to have a unique title each time I click the button to prevent overwriting the existing one!
I appreciate if you have a look at the second script I sent!

--Mohammad

Jed Carty

unread,
Mar 26, 2019, 8:25:58 AM3/26/19
to TiddlyWiki
The list widget is always going to give trouble there. I am not sure how to fix that recursion problem.

But I do have a solution here:

First the javascript macro that returns a unique name using a basename the same way that tm-new-tiddler does:

Make a tiddler called $:/macros/OokTech/uniqueTitle.js give it the tag $:/tags/Macro, a field called module-type with the value macro and set the type to application/javascript then paste this code into the text field, you will have to save and reload your wiki before the macro can work (in bob you can just refresh the page, you don't have to restart the server).

/*\
title: $:/macros/OokTech/uniqueTitle.js
type: application/javascript
module-type: macro
Macro to return a formatted version of the current time
\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro
*/

exports.name = "uniqueTitle";

exports.params = [
{name: "baseName"},
{name: "options"}
];

/*
Run the macro
*/
exports.run = function(baseName, options) {
if (!baseName) {
baseName = $tw.language.getString("DefaultNewTiddlerTitle")
}
return this.wiki.generateNewTitle(baseName, options)
};

})();

Then make a tiddler tagged with $:/tags/Macro and put this in it:

\define newTiddler(template:"" title:'New Tiddler' fields buttonText:"New Tiddler")
<$button>
<$action-sendmessage $message="tm-new-tiddler" $param="""$template$""" title=<<uniqueTitle """$title$""">>  $fields$/>
$buttonText$
</$button>
\end

<<newTiddler "HelloThere" "bob" "bob=joe eddie=george tags='HELLO'" Potatoe>>

The inputs are, in order:

<<newTiddler template title fields buttonText>>

Where template is the tiddler you want to use as a template, if any, title is the base name you want to use, fields is a string with 'field=value' the same as you would put in the $action-setfield widget, it has to be in quotes like this "field1=value1 field2=value2" finally, buttonText is the text to display on the button itself.

This should fit all of the requirements you listed.


Mohammad

unread,
Mar 26, 2019, 9:08:09 AM3/26/19
to TiddlyWiki
Thanks Jed!
It works as I asked above!

Best
Mohammad

Mohammad

unread,
Mar 26, 2019, 9:08:23 AM3/26/19
to TiddlyWiki
added to TW-Scripts


On Tuesday, March 26, 2019 at 4:55:58 PM UTC+4:30, Jed Carty wrote:

Jed Carty

unread,
Mar 26, 2019, 7:26:34 PM3/26/19
to TiddlyWiki
The javascript macro part of this got merged into the core, but it is called unusedtitle instead of uniquetitle because that is more accurately what it does.

If you want to use this macro before the next release you should change the line

exports.name = "uniqueTitle";
to
exports.name = "unusedtitle";

and then use the macro like
<<unusedtitle baseName>>
so that you don't have to change anywhere in your wiki if you upgrade to the next version.

Mohammad

unread,
Mar 27, 2019, 3:26:10 AM3/27/19
to TiddlyWiki
Hi Jed
Many thanks for all your efforts.
This is really useful.

One suggestion, where we use this feature? When we need to create a toddler using tm-new-tiddler. So, is it possible to merge it into tm-new-tiddler, then it can have another parameter basetitle. This way we can use title as before and basetitle like action-createtiddler.

Mohammad

TonyM

unread,
Mar 27, 2019, 10:07:25 PM3/27/19
to TiddlyWiki
Mohammad,

Without delving in the detail of answers in this thread. When you ask about a template what do you mean? It is simple to set the text field of a new tiddler to the content of another tiddler if that is all you wanted. You can then use action-createtiddler and get the unique tiddler guarantee, you could also hide the new tiddler button if the provided new tiddler title already exists and force the user to select the unique name and use tm-new-tiddler message.

But I previously found using a template with action-createtiddler required a process that created the field=value pairs eg text="""text content""" afield="a value" etc... and substituted these into the action-createtiddler widget.

Regards
Tony

Mohammad

unread,
Mar 28, 2019, 12:57:55 AM3/28/19
to TiddlyWiki
Tony,
 By template, I mean the one you use with $param in tm-new-tiddler! and by basetitle I mean a title I push!
These two are NOT possible with current situation! What Jed did is my answer!

Thank you

Mohammad

unread,
Mar 28, 2019, 12:59:22 AM3/28/19
to TiddlyWiki
Tony!




But I previously found using a template with action-createtiddler required a process that created the field=value pairs eg text="""text content""" afield="a value" etc... and substituted these into the action-createtiddler widget.

Yes, you are right! It can be very useful in many situation.

--Mohammad 
Reply all
Reply to author
Forward
0 new messages