[
{
"title": "PERRY MASON",
"tags": "#perrymason",
"type": "text/plain",
"text": "I was just explaining things to Mr Huxley. Don't let me interrupt. Now, these two goldfish are suffering from gill fever. How much longer would you say they can live? If it's really gill fever, an hour or two. Right, but watch.\n#PerryMason"
},
{
"title": "PERRY MASON",
"tags": "#perrymason",
"type": "text/plain",
"text": "Now, I spread a small amount of my preparation on this piece of screening and place it in the water. \n#PerryMason"
},
{
"title": "PERRY MASON",
"tags": "#perrymason",
"type": "text/plain",
"text": "And within a remarkably short time Kind of a miracle, isn't it, Mr Huxley? I wouldn't go so far as to say that, but it's certainly impressive. Would you be interested in merchandizing the preparation throughout the country? I may do better than that.\n#PerryMason"
}
]
Ideally I want to have a macro that can change it to this ...
[
{
"title": "PERRY MASON #0009",
"tags": "#perrymason",
"type": "text/plain",
"text": "I was just explaining things to Mr Huxley. Don't let me interrupt. Now, these two goldfish are suffering from gill fever. How much longer would you say they can live? If it's really gill fever, an hour or two. Right, but watch.\n#PerryMason"
},
{
"title": "PERRY MASON #0010",
"tags": "#perrymason",
"type": "text/plain",
"text": "Now, I spread a small amount of my preparation on this piece of screening and place it in the water. \n#PerryMason"
},
{
"title": "PERRY MASON #0011",
"tags": "#perrymason",
"type": "text/plain",
"text": "And within a remarkably short time Kind of a miracle, isn't it, Mr Huxley? I wouldn't go so far as to say that, but it's certainly impressive. Would you be interested in merchandizing the preparation throughout the country? I may do better than that.\n#PerryMason"
}
]
What I need is a macro that will...
(a) take a START NO., 9 in this case
(b) and for the first match of ..."title": "PERRY MASON"
... change it to ...
"title": "PERRY MASON #0009"
(c) Then increment the number by 1 for each repeat.
The leading Zeros are NOT essential but are helpful in sorting and finding things later.
Any help would be much appreciated.
Best wishes
Josiah
Just 2cents worth.
Tony
javascript:var ta= document.activeElement.contentWindow.document.querySelector('textarea');
var numbers= new Object();
var number= /^(.*?)\s*#(\d+)$/;
if(ta !== null && ta.tagName.toLowerCase() == 'textarea') {
var content= JSON.parse(ta.value);
for (var i=0; i<content.length; ++i) {
var title= content[i].title;
if(title === undefined) {
continue;
}
var hasnumber= number.exec(title);
if(hasnumber === null) {
if(numbers[title] === undefined) {
numbers[title]= "0001";
} else {
numbers[title]= ((("10"+numbers[title])*1+1)+"").replace(/^10?/, "")
}
content[i].title+= " #"+numbers[title];
} else {
numbers[hasnumber[1]]= hasnumber[2];
continue;
}
}
ta.value= JSON.stringify(content,0,2);
}
void(0);
Example
[
{
"title": "PERRY MASON"
},
{
"title": "PERRY MASON #0009"
},
{
"title": "PERRY MASON"
},
{
"title": "JAMES BOND #007"
},
{
"title": "JAMES BOND"
}
]
will become[
{
"title": "PERRY MASON #0001"
},
{
"title": "PERRY MASON #0009"
},
{
"title": "PERRY MASON #0010"
},
{
"title": "JAMES BOND #007"
},
{
"title": "JAMES BOND #008"
}
]
Titles are the primary key and should be unique so in this case you are doing so artificaly. Captions need not be unique.
I don't know how you captured this data in the first place
Does this have to be done in TW?
The need to add numbers to make titles unique suggests you have made some less than perfect choices.
TonyM wrote:Titles are the primary key and should be unique so in this case you are doing so artificaly.
In my view, having an import filter that creates titles with auto-incrementing numbers might be useful, though (similar to $basetitle here: https://tiddlywiki.com/#ActionCreateTiddlerWidget). Has anybody ever looked into this?
Just some observations.
In Twc I used to generate multiple task tiddlers for project initalisation and would use the project name as a suffix and the "new is new" plugin, the feature which is now built in, to increment duplicate names.
In tw5 there is a tools setting that influences import overwrites which will import duplicate tiddlers as a new name.
It would be nice if a suffix and import duplicate settings were built into the import facility.
But of course there are features in the bundler plugin that may help, have a close look there as you can log all tiddler imports for subsequent bundling.
Regards
Tony
Glad to hear that you found it useful. I hope more people will do too.
- Performance: To correctly pad, say 00001 or 00019 etc, you need to
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
counter="007"
"007"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"008"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"009"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"010"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"011"
counter="098"
"098"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"099"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"100"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"101"
counter="998"
"998"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"999"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"1000"
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
"1001"
Expanded to include Stephen's input. If you set $initval as 004 now, it will add suffixes as 005, 006...
plugin and demo.
Am Montag, 19. Februar 2018 16:16:14 UTC+1 schrieb Riz:- Performance: To correctly pad, say 00001 or 00019 etc, you need to
do it like this for example:
counter= ((("10"+counter)*1+1)+"").replace(/^10?/, "")
Example run from webconsole ...