Extract values from tags

Hi,

I’m using values inside tags in a time recording file. I’m trying to run through the file and then create lines that are suitable for our timesheet system.

Sample line:

- 2020-07-23 15:00 | Milling project meeting @start(2020-07-23 15:00) @company(Milling) @done(2020-07-23 16:30) @P(3064085)

I would like to be able to extract the data to look something like this

Description;start;end;company i.e.
2020-07-23 15:00 | Milling project meeting;2020-07-23 15:00;2020-07-23 16:30;Milling

I cant find the tags in the record but I cant find a way to get the value from the tag. Is there a way? or do I have to do some form of string manipulation?

Thanks,
Neels

You can read tag values directly from the scripting interface – no need to do your own string parsing.

It might be easiest if you showed us a fragment of where you have got to so far in your script but broadly, the method to use is:

item.getAttribute()

with at least an attribute name argument where the name is data- followed by the tag name:

e.g. something like:

const dateString = someItem.getAttribute('data-start');

Or fleshing it out slightly more, perhaps something along the lines of:

Click to expand JS snippet
(() => {
    'use strict';

    const main = () => {
        const
            tp3 = Application('TaskPaper'),
            ds = tp3.documents;
        return 0 < ds.length ? (
            ds.at(0).evaluate({
                script: tp3Context.toString(),
                withOptions: {
                    optionName: 'someValue'
                }
            })
        ) : 'No documents open in TaskPaper.'
    };

    const tp3Context = (editor, options) => {
        const someNode = editor.selection.startItem;
        return ['start', 'company', 'done'].map(
            tagName => someNode.getAttribute(
                'data-' + tagName
            )
        ).join('; ')
    }

    return main();
})();
2 Likes

Thanks!! Quite new to JS so I will reply in a few day when I’ve tried it.

1 Like

You can DM me here, if you like, with a slightly larger sample of input and output, so that I can tailor it a bit better.