Add new tags to an existing Tiddler

Hi everyone,
I’m trying to add new tags to existing Tiddlers that already have other tags, using an action triggered by a button. However, the new tags are not being added to the Tiddlers.
I would appreciate any help to correct the code.
Thanks in advance
Rosi

<!-- Define temporary tiddler titles -->
\function f.tempSwot()    $:/temp/rosi/new-tiddler-swot
\function f.tempIndicImpact()  $:/temp/rosi/new-tiddler-indicimpact

<!-- Function to "get" multiple tags --> 
\function f.getNewTags() [<f.tempSwot>get[text]] [<f.tempIndicImpact>get[text]] [<currentTiddler>get[tags]trim[]]

<!-- Helper procedures -->
\procedure deleteTempTiddlers()
<$action-deletetiddler $filter="[prefix[$:/temp/rosi/]]" />
\end

\procedure newTagsActions()
<$action-addTags
  tiddler = <<currentTiddler>>
  tags= <<f.getNewTags>>
  >
  
</$action-addTags>
<!-- delete all temporary tiddlers  -->
  <<deleteTempTiddlers>>

\end


<!-- Filtro para selección todos los Tiddler con el tag 'Constraint' -->
<$list filter="[all[current]tag[Constraint]]">
  <div class="tw-costs-edit-fields" style="margin-top: 1em; padding: 0.5em; border-left: 4px solid #ccc;">

    <!-- Dropdown para seleccionar una etiqueta sobre el tipo de elemento del SWOT -->
    Choose a SWOT element type: 
    <$select tiddler=<<f.tempSwot>> field="text" default="">
      <option disabled>Choose an option</option>
      <option value="">-none-</option>
      <option value="Strength">Strengths</option>
      <option value="Weakness">Weaknesses</option>
      <option value="Threat">Threats</option>
      <option value="Opportunity">Opportunity</option>
    </$select>

    Choose an element type: 
    <$select tiddler=<<f.tempIndicImpact>> field="text" default="">
      <option disabled>Choose an option</option>
      <option value="">-none-</option>
      <option value="Indicator">Indicators</option>
      <option value="ExpectedImpactPositive">Expected Impact (Positive)</option>
      <option value="ExpectedImpactNegative">Expected Impact (Negative)</option>
    </$select>

    <!-- Botón para crear el tiddler con ese nombre -->
    4. Press the button
    <$button actions=<<newTagActions>> 
    >
      Create new tag
    </$button>

There are several problems:

First, f.getNewTags() needs to join the tags together into a single, space-separated tags value, like this:

\function f.getNewTags()
[<f.tempSwot>get[text]]
[<f.tempIndicImpact>get[text]]
[<currentTiddler>get[tags]trim[]]
+[join[ ]]
\end

Note that while your $select list items currently do not contain any spaces, if you ever want to have a tag value that does contain spaces, you will need to handle this in f.getNewTags(), like this:

\function f.getNewTags()
[<f.tempSwot>get[text]format:titlelist[]]
[<f.tempIndicImpact>get[text]format:titlelist[]]
[<currentTiddler>get[tags]trim[]]
+[join[ ]]
\end

The format:titlelist[] operator ensures that values containing spaces are enclosed in doubled square brackets (e.g., [[This has spaces]]).

The next problem is that the TWCore doesn’t have an $action-addtags widget. Instead, you can use $action-setfield, like this:

\procedure newTagsActions() <$action-setfield tags=<<f.getNewTags>>/><<deleteTempTiddlers>>

Alternatively, you can completely eliminate the need for f.getNewTags() and deleteTempTiddlers() and just use:

\procedure newTagsActions()
<$action-listops $tags="[<f.tempSwot>get[text]] [<f.tempIndicImpact>get[text]]"/>
<$action-deletetiddler $filter="[prefix[$:/temp/rosi/]]"/>
\end

Note that $action-listops is smart about making changes to the tags LIST field. It knows how to add specified tag values without altering any existing tags.

Finally, your $button for performing the action says actions=<<newTagActions>> but your procedure is named newTagsActions. Change your button to:

<$button actions=<<newTagsActions>>>Create new tags</$button>

-e

2 Likes

Super help and explanation!!
Thanks so much
Rosi

I will.just add you can also use tm-add-tag and other messages with the action send message widget. if you do so wrap the actions in a $fieldmangler widget.

  • if you use this approach you don’t need to store the current tags as they are maintained on add tag.

Wonderfully put together! (also, minor oopsie, I fat fingered the controls and maay have unmarked this as the solution, I’ve remarked it as solved but in case anyone gets any weird messages, it was me :sweat_smile:)