Global Macro to calculate age from Title

I have tiddlers whose titles are simply a year (e.g., “2020”). I created the following macro to calculate the age based off the title. For example, the macro below should return “20” on the “2020” page. (that is, 2020-2000=20):

\define showAge() {{{ [<currentTiddler>subtract[2000]] }}}

It works great locally, but why doesn’t this work as a global macro? I have tagged the global macro with $:/tags/Macro.

My ultimate goal is to be able to change the 2000 to an arbitrary field, but I first want to understand this basic step. From there, how would I take, for example, the “born” field from say a tiddler [[Person A]] and plug it into the “[2000]” spot?

Your insights are kindly appreciated.

MR

I’m seeing no trouble using

\define showAge() {{{ [<currentTiddler>subtract[2000]] }}}

It’s good to get in the habit of using procedure pragma, and the tag $:/tags/Global moving forward, as macro architecture slowly transitions to procedures.

And unless you want the showAge number to be a link (to a likely-missing tiddler with the title 20), you probably want to put the triple-curly-braces filter code within a text widget. So, this would be the tweak I recommend (using $:/tags/Global):

\procedure showAge() 
<$text text={{{ [<currentTiddler>subtract[2000]] }}}/>
\end

All that seems to be working fine, including on other tiddlers.

Of course, the procedure/macro <<showAge>> won’t show its results in the edit preview window, since the title of the tiddler isn’t technically the same while it’s in edit mode (so that the original can be reverted-to easily). Could that be what was tripping you up?

Assuming you’d use this within the tiddler for (Person A) that has a field born with the birth year, you can run with this:

\procedure showAge() 
<$text text={{{ [<currentTiddler>subtract{!!born}] }}}/>
\end

Similarly, maybe you’ll not be working only with currentTiddler title, but with some other important field, so you’d have

<$text text={{{ [{!!year}subtract{!!born}] }}}/>

(Again, this would all be fields internal to the tiddler where the procedure is being used.)

However… If you want to work with the birth year from Person A tiddler, but you’re invoking the procedure from some other location, then the question is: what’s the relationship between the tiddler where the procedure-call is invoked and the Person A tiddler?

Perhaps, for example, you want to use this procedure within a tiddler that corresponds to an event (or photo, etc.) from the life of Person A… So the connection to Person A is store in some field, and you need the procedure to navigate the right path from currentTiddler to the person tiddler where the birth year’s field value can be found.

1 Like

Springer,

Thank you! Since you said it was working, I tried it in a fresh tiddler and saw that it worked fine. I began deleting all related macros to start from scratch when I noticed that my deleted macro was still being called. It must have either remained in memory, or perhaps I had a conflicting macro with the same call. Anyway, I deleted everything, refreshed the page and started from scratch and got it to work.

Sigh, another lesson for this newbie. I should be glad, I guess that I did have the actual code right in the end.

Thank you for your help. It did lead me to the solution. Just knowing it should work fine got me to realize the issue must have been elsewhere. I first thought my version of Tiddlywiki was too old, but I eventually stumbled on the issue.

Also, thank you for the text widget reminder. I was so focused on getting it to work that I hadn’t noticed that it had turned it into a link.

And, yes, I must begin to delve into the latest changes to keep up on things. I appreciate the gentle nudge.

Best regards,

MR

2 Likes

For this, I see that I have to give some more thought about how it would play out. But I think you’ve given me some good clues on how to approach it. I will play around with it. Thank you.

1 Like

If you are looking at this, where it is primarily a filter that displays a result, with the features of 3.5.,x this can be made much simpler.

\function age()  [{!!year}subtract{!!born}] +[join[ ]]

Use <<age>>

The join retains more than the first result of the filter

The advantages include

  • Avoid the verbose text widget
  • Makes the function and output usable as a variable
  • The string output can be used in filters
  • Slight change to make it a custom function
  • Easier to read