How to have your code and action it too - batch operations on multiple tiddlers, refactoring your wiki

Folks - not so well known is how to undertake a batch operation on multiple tiddlers. This may be needed for a one off refactoring of tiddlers, when you introduce a new method to an evolving wiki, or perhaps even to fix a mistake without reverting to a backup.

Below I have some macros to make this design workflow very simple.

The basic trick for batch operations is to put a list inside a button, and within the list generate an action widget for each tiddler.

Let us first “learn how to fish”. Here is an example which on tiddlywiki.com would on click set a field “HelloThere” to yes for all tiddlers tagged HelloThere, and optionally, if you remove the comments also remove the tag;

HelloThere to field button

<$button tooltip="warning message">
<$list filter="[tag[HelloThere]]">
   <$fieldmangler>
   <$action-setfield $field="HelloThere" $value="yes"/>
   <!--$action-sendmessage $message="tm-remove-tag" $param="HelloThere"/-->
   </$fieldmangler>
</$list>
Set ~HelloThere tag to field ~HelloThere="yes"
</$button>
  • Fieldmangler is needed if using tm-remove-tag

However the above is prone to error because you never get to see which tiddlers your filter will effect and instead you need to first test the list outside a button etc…

Then rather than write the above button we could have written this instead;
"Lets us just catch a fish".

Using this macro “batch Function” is helpful when building such buttons batch-functions.json (677 Bytes)

\define batch-function(function)
<$set name=function-code filter="[[$function$]getvariable[]]">
<h3>Batch Function $function$</h3>
<$codeblock code=<<$function$>> />
<h3>$function$ Output</h3>
<<$function$>>
<h3>Action</h3>
<$button tooltip="Trigger the actions in the above code" actions=<<$function$>> >Action $function$ now</$button>
</$set>
\end

Let’s return to a simplified rename-hellothere-function

\define rename-hellothere-function()
<$list filter="[tag[HelloThere]]">
   <$link/><br>
   <$action-setfield $field="HelloThere" $value="yes"/>
</$list>
\end

<<batch-function rename-hellothere-function>>
  • notice how it lists both the link to each tiddler resulting from the filter AND the action widgets?
  • For clarity I removed the “remove tag code”
  • Notice how it is now a macro called rename-hellothere-function?
  • Then we call the new macro using the batch-function macro
    <<batch-function rename-hellothere-function>>

You will then see the following output;
Snag_1924d374

With the use of the batch-function macro you get to;

  • Write less code
  • See the code passed to the function to review;
  • See the list of affected tiddlers
  • Have a button to press to trigger the action widgets for all of those tiddlers.
  • You can easily modify the simple rename-hellothere-function to
    • show more info for each item
    • add the remove the tag code
    • add a <$action-confirm $message="message to user to confirm"> inside the list for each item, or outside the list for once per button click.

If you want to make this button permanent for reuse you can add the following macro;

  • Included in above package
\define function-button(function button-name:"button-name")
<$button tooltip="Trigger the actions in $function$" actions=<<$function$>> >$button-name$</$button>
\end

And use <<function-button rename-hellothere-function>> on the same “function”.

In closing this example also demonstrates that;

  • Including actions in a $list are ignored unless there is a trigger such as a button
  • Including display output is ignored when triggering the actions generated in a button,
    including within lists and other structures.

In closing, this would have helped me a lot years ago had it being available, so I hope it helps some of you now.

6 Likes

I’m grateful for careful treatment of these issues, with examples. For novices browsing this thread and looking for a ready-made solution for some of these “How do I…?” batch-transformation questions, I have found Mohammad’s TW-Commander to be greatly valuable: Commander Plugin 2.1.2 — bulk operations on tiddlers

5 Likes

I just stumbled across this. Thank you for a great resource. Bookmarked!

2 Likes

Thanks for sharing.

In

<$codeblock code=<<$function$>> />
<h3>$function$ Output</h3>
<<$function$>>

What does <<$function$>> mean in two place?

In <$codeblock code=<<$function$>> /> seems it just regarded as a text, and displayed by codeblock.

In $function$ the macro (or function?) name is displayed (or transcluded?)

Then in <<$function$>> it regarded as a macro call to rename-hellothere-function?

Seems $function$ and <<$function$>> are new features in 5.3.0

  • No this is simple use of substitution - before the code is rendered the $parameter$ is replaced with its value, allowing you to show it as code, or call it as a macro
    • This allows you to write macros that call other macros even widgets using a parameter to set the macro or widget name.
  • It is co-incidental I used the name function. Functions in TW 5.3.0 are filters, or custom operators if beginning .
  • There will be otherways to do what I did here, now in 5.3.0
1 Like

And I just used it for the first time today. This was very helpful. Thank you @TW_Tones!

2 Likes