If I wanted to keep track of how many projects and tasks I complete a year and created $ProjectNumber and $TaskNumber attributes. Is there a way to get each new project to have its number added automatically and incremented +1 from the last $ProjecNumber? Then also apply this to the $TaskNumber. This would actually be useful in tracking projects on timesheets. Each task can have the parent $ProjectNumber added automatically too.
I’m at a loss in how to achieve this? Also the numbers need to be read only so they don’t accidentally get deleted. In terms of number I was thinking of the format P1 and T1 and then then number increments so it probably needs to be a string rather than a number.
I don’t think it’s an exact solution, but be aware of the ‘sequential’ option for Number-type attributes. The problem there is that whilst numbers are not re-used, the allocation goes to every note and in your case thus probably to notes that shouldn’t have a value for that attribute.
A hack comes to mind. Make a note whose role is to be the counter, with $MyNumber (or Number-type attribute of your choice holding the last issued number in the sequence. You then write you code such that the action that allocates a number reads the existing $MyNumber for the note and then also increments the 'counter 'by one. There will doubtless be some edge cases that come out of the woodwork but it might be worth a try. the counter note just need a suitable name you won’t mistake and can be stored off in a corner of the doc as once tested and working you’d never really need to see it - just access it via code from elsewhere in the doc.
In my own notes about Tinderbox, I have a similar situation – a container of user-reported issues. Each has its own number, because it’s sometimes easier to say “Issue 741” than to say “that peculiar thing X reported where Tinderbox gets kind of slow on Tuesdays when…”
Under the hood, we use the sandbox attribute $MyNumber to hold the issue number. The OnAdd action of the container is simply
and the $DisplayExpression, inherited from the prototype, is
$MyNumber+". "+$Name
Now this isn’t perfect; if you added new notes to the middle of the list, the numbering would be fouled up. But It’s quick and easy; I set this up without much thought back in 2013 and it’s been fine ever since.
There are lots of other ways to do this. Sequential attributes are guaranteed to be unique for every note, for example. $SiblingOrder is sometimes all you need. Or, you could use a configuration note to keep track of the issues.
OK, I’ve made a demo (link at end). But here’s the rough process…
Make the new Number-type attribute IssueNumber. Remember to set the type (default is string) and set the default to 0. (That should be done for you and is a glitch which will get fixed in due course).
Now add the notes. Set the $IssueNumber for ‘config’ to 1 as this makes the later $OnAdd code less complex as the counter object is preset to start from one1 rather than zero. I’ve also made things more flexible by moving ‘config’ out of the root and addressing it via title not path. Just ensure there is only note of that name and you can then put it anywhere you like without the Project’s on-add code failing. Add this code to $OnAdd for Project: