A (failing) Stamp to Create and Link 'Tag' Notes

I have a bunch of notes with an attribute, ‘Tags’, being a comma-separated list of tags, sometimes quoted when containing spaces (eg, 'this, that, “the other” ').

I split and rejoin this with semicolons as part of the prototype’s action code, so there’s a semi-duplicated Attribute ‘tbTags’ which contains them in a manner that’s hopefully more native to Tinderbox.

I created a Stamp which should walk through the list of tags, create a note with the tag name in a ‘/Tags’ note (if it’s not there already), then link the original note to the existing or newly created tag. At one stage, I had this partly working, creating the relevant items in ‘/Tags’ before I swapped it over from being an action to being a rule and adding the tag linking.

Can anyone see what’s (hopefully) obviously wrong?

var tags = $tbTags.split(";");
tags.each(myTag) {
	if(!isDuplicateName(myTag)) {
		var tagNotes = create("/Tags",myTag);
		$Prototype(tagNotes) = "Tag";
	}
	// find notes to link to
	if(!linkedFrom("/Tags/"+myTag)) {
		linkFrom("/Tags/"+myTag, "uses tag");
	}		
}

I’m on 8.9.2. I’ve made pop-up messages about errors appear in the past, but this bit of code isn’t triggering any, which would at least give me something to work on!

var tags = $tbTags.split(";");

I don’t think you need to want this. I assume that $tbTags is a list; if so, each will iterate through each item in turn.

You also don’t need to check whether the link already exists, as linkFrom does this check itself.

One line declaration like this is not supported in v8.x (but will be in v9). You must use either:

var tags;
tags = $tbTags.split(";");

or

var tags($tbTags.split(";"));

But, as @eastgates notes, you already have a list and down need to pre-check for links, so this ought to work:

$tbTags.each(myTag) {
   if(!isDuplicateName(myTag)) {
      var tagNotes = create("/Tags",myTag);
      $Prototype(tagNotes) = "Tag";
   };
   // find notes to link to
   linkFrom("/Tags/"+myTag, "uses tag");	
};

As a general guidance, the best way to approach a complex stamp that’s not doing what you want is to break it up into small pieces — pieces too small to possible fail — and then to test each one.

2 Likes

Thanks @mwra and @eastgate - I stripped it right back to the minimum and rebuilt part by part. I know these are sensible practices, but sometimes things seem too obvious to need to apply them :slight_smile:

In the end, it pared right back to

 var tagNote;
 $tbTags.each(myTag) { 
	tagNote=create("/Tags/"+myTag);
	$Prototype(tagNote) = "Tag";
 	linkFrom("/Tags/"+myTag, "uses tag");
}

Fair to say there was a bunch of sanity checking I didn’t need to do, and sanity checking I did need to do that I didn’t think I needed to do.

In this rumoured Tinderbox 9, I’d love it were there to be a ‘log()’ function and a message log window. My scrappy debugging style would be enormously grateful.

For better or worse, log() is taken by logarithms. And the system log can be a bear to read.

Idea: make a note named log. When you want to log a diagnostic message, do this:

$Text(/log)=$Text(/log)+"hello, world\n");
3 Likes