Changing custom fields in other tiddlers

50 views
Skip to first unread message

cmari

unread,
May 31, 2008, 3:11:34 PM5/31/08
to TiddlyWiki
Hi,
This question is somewhat related to an earlier question about setting
default values for custom fields, but I wasn't clever enough to be
able to translate the answer Eric gave in that case to what I want to
do. I'd like to be able to change the values and/or names of custom
fields in other tiddlers - ideally even in many tiddlers at the same
time. But I can't figure out how to do this for multiple tiddlers,
particularly when some titles contain parentheses, which seems to
cause problems for the CheckboxPlugin. Any help with either of the
approaches I've tried (or some even better method!), would be greatly
appreciated! My two strategies so far:

1. CheckboxPlugin. I tried something like:
<<forEachTiddler
where
'tiddler.tags.contains("MyTag")'
write
'"[_(locations@+tiddler.title+)]" +tiddler.title+"\n"'>>

Unfortunately, that doesn't work when there are parentheses in tiddler
titles (and I couldn't figure out how to rewrite it using <<checkbox)

2. Alternatively, the following method looks even better because it
changes fields directly, without requiring any checkboxes - plus it
works for tiddler titles with parentheses. But the only way I can
make it work is to insert the exact title of the tiddler I want to
affect (in place of "my specific tiddler title"):
<<forEachTiddler
where
'tiddler.tags.contains("MyTag")'
script '{{store.setValue("my specific tiddler
title","myfieldname","myfieldvalue")}}'>>
What I'd really like is for this to affect all tiddlers tagged with
MyTag, regardless of their titles.

Thanks for any help anyone can offer!
cmari

Eric Shulman

unread,
May 31, 2008, 5:27:58 PM5/31/08
to TiddlyWiki
> 2. Alternatively, the following method looks even better because it
> changes fields directly, without requiring any checkboxes - plus it
> works for tiddler titles with parentheses. But the only way I can
> make it work is to insert the exact title of the tiddler I want to
> affect (in place of "my specific tiddler title"):
> <<forEachTiddler
> where
> 'tiddler.tags.contains("MyTag")'
> script '{{store.setValue("my specific tiddler
> title","myfieldname","myfieldvalue")}}'>>
> What I'd really like is for this to affect all tiddlers tagged with
> MyTag, regardless of their titles.


Although <<forEachTiddler>> can be very useful for invoking bits of
javascript to process selected tiddlers, you could also use 'pure'
javascript, supported by InlineJavascriptPlugin to accomplish the same
activity, like this:

<script>
var tids=store.getTaggedTiddlers("MyTag");
for (var t=0; t<tids.length; t++) {
store.setValue(tids[t].title,"myfieldname","myfieldvalue");
}
</script>

This approach is, in my opinion, much more straightforward and easier
to understand, and eliminates the overhead needed by the
<<forEachTiddler>> internal processing, as well as the need for any
special quoting syntax.

enjoy,
-e

ref: http://www.TiddlyTools.com/#InlineJavascriptPlugin

Ken Girard

unread,
Jun 2, 2008, 2:27:19 PM6/2/08
to TiddlyWiki
Very nice, Eric.
Using that and RenameTagsPlugin (for quick tag removal) I can convert
a TW from being overly tag heavy to using custom fields combined with
just a couple of choice tags for a nicer set up.

Ken Girard

wolfgang

unread,
Jun 2, 2008, 4:40:41 PM6/2/08
to TiddlyWiki
> Very nice, Eric.
> Using that and RenameTagsPlugin (for quick tag removal) I can convert
> a TW from being overly tag heavy to using custom fields combined with
> just a couple of choice tags for a nicer set up.

Indeed very practical.
However, I would need some help from Eric to merge this script with
the following RenameTag bookmarklet - with which a tag can also be
removed:

<script label="RenameTag" title="Renames all occurrences of a tag">
if(window.version&&window.version.title=='TiddlyWiki')
var oldTag=prompt('Enter the tag to rename','oldTag');
var newTag=prompt('Rename tag '+oldTag+' to:','newTag');
var t=store.getTaggedTiddlers(oldTag);store.suspendNotifications();
for(var i=0;i<t.length;i++)
{t[i].tags.remove(oldTag);t[i].tags.pushUnique(newTag);}
refreshDisplay();
</script>


All I could (copy and paste) till now was:

<script label="TagToField" title="Change all occurrences of a tag to a
custom field">
if(window.version&&window.version.title=='TiddlyWiki')
var oldTag=prompt('Enter the tag to add a custom field','oldTag');
var tids=store.getTaggedTiddlers(oldTag);
for (var t=0; t<tids.length; t++) {
store.setValue(tids[t].title,"field","myfieldvalue");
}
refreshDisplay();
</script>

If there would be a prompting box for the new field value and the
deletion of the old tag - all in one - that would of course be the
best.

Regards,

W.

Michael.Tarnowski

unread,
Jun 2, 2008, 7:11:03 PM6/2/08
to TiddlyWiki
Hi TW community,
I'am looking for a method to add certain named customs fields - which
do not exist in my TW - with pre-defined default/standard values to
predefined tagged tiddlers.
Is there any work-around to do this?
Cheers Michael

wolfgang

unread,
Jun 2, 2008, 11:27:49 PM6/2/08
to TiddlyWiki
Hi Michael,

> I'am looking for a method to add certain named customs fields - which
> do not exist in my TW - with pre-defined default/standard values to
> predefined tagged tiddlers.

the script of Eric, with InlineJavascriptPlugin installed, already
does what you want:

> <script>
> var tids=store.getTaggedTiddlers("MyTag");
> for (var t=0; t<tids.length; t++) {
> store.setValue(tids[t].title,"myfieldname","myfieldvalue");
> }
> </script>

You only have to type in MyTag, myfieldname and myfieldvalue manually,
and on clicking done it does this job. And then every time you open
the tiddler with this script afterwards.

regards,

W.

Ken Girard

unread,
Jun 4, 2008, 5:22:24 PM6/4/08
to TiddlyWiki
Another possibility is to use the built in newTiddler macro as it now
supports customfields.
See http://tiddlywiki.org/wiki/NewTiddler_%28macro%29 for more info.

newTiddler
label:"New Tiddler"
text:"Hello world."
template:"CustomTemplate"
tag:"test"
tag:"an example"
accessKey:"1"
focus:"tags"
fields:"myfieldname:myfieldvalue;myfield2:myvalue2;"

This should give you a button to create a new tiddler with the fields
filled in, tags applied, and some text.... once you create a
customized template with the customfields built into it.

Haven't tried it yet but you should be able to do the same with
HideWhen templates. [Cool.... something new to try....]

Ken Girard

cmari

unread,
Jun 4, 2008, 6:54:57 PM6/4/08
to TiddlyWiki
Thanks to Eric for a script that did exactly what I wanted! Since
there's never an end to these things, I of course now have another
question: If one can use

store.getTaggedTiddlers("MyTag");

can one do something similar to to get tiddlers that have the field
"myfield" ?

In the hope that if I spare Eric from having to repeat himself he'll
have a bit of free mental space for my new question (!), I'll copy in
what I learned from him in another thread about adding custom fields
using the newTiddler macro. I've been doing exactly what Ken
suggests. But it requires a few extra quotation marks:

fields:'myfieldname:"myfieldvalue" myfield2:"myvalue2"'

cmari

Eric Shulman

unread,
Jun 5, 2008, 12:38:13 AM6/5/08
to TiddlyWiki
> can one do something similar to to get tiddlers that have the field
> "myfield" ?

<script>
var tids=store.getTiddlers("title");
for (var t=0; t<tids.length; t++) {
if (store.getValue(tids[t].title,"myfield")!==undefined)
store.setValue(tids[t].title,"myfield","myfieldvalue");
}
</script>

What it does:
* retrieve ALL tiddlers in the document (include those tagged with
"excludeLists")
* attempt to GET the desired field value
* if the field value is not undefined, then the field exists for that
tiddler
* in which case, SET the field to some new value

enjoy,
-e


Reply all
Reply to author
Forward
0 new messages