Problem with script, apparently after upgrading TP


#1

Hi Jesse,

I use a script which places a Due report (Markdown with tags) in the clipboard, so that it can be viewed/printed from a Marked2 clipboard preview. See
http://support.hogbaysoftware.com/t/creating-printable-taskpaper-3-reports/1488

However it no longer works as before, I just noticed. It now generates output like this:

"Due

2015–10 10

undefined

2016–01 17

undefined"

Hence, the “undefined” should not be there ­­­­­– instead there should be project names and tasks. My guess is that the upgrade contains a bug. Do you know what is happening?
Thank you,
Paul


#2

l’ll take a look at this too, over the weekend – I’m experimenting with a more flexible multi-document version of it, so its a good time for me to pull out that code and see what’s happening.


#3

Thank you so much.
Paul


#4

Ah ! Glanced at it and saw the problem.

My fault – there was a minor change in the API a few releases ago, and I hadn’t updated this script.

( Item.bodyDisplayString --> Item.bodyContentString )

Ver. 0.04 in the original post should now work, I think.


#5

Yes this works ­­­­­– fantastic! Many thanks for your help.

I have two small requests.

  • Would it be possible to change the way the date is depicted (e.g. “2016–04 29”) into “2016–04-29”, with “29” in the same color (hence not in red)? I have been trying to make some changes, but wasn’t able to figure out how to do this, as I have no experience with script programming.
  • Inserting a “:” between project name and task would be very welcome. I couldn’t figure out how to do this unfortunately.

Thank you, and of course I understand that you may not have time for this.
Paul


#6

No problem – I’ll have to leave it till the weekend now, but I’ll do that, and there should also be something a little easier to customize in the pipeline.


#7

wonderful ­­­­­– thank you


#8

Here is an edit which aims for

  • monochrome fully hyphenated date
  • a comma trailing project names

(to see the edited lines, search for the word delta in the script)

// DUE REPORT script for TaskPaper 3 ver 0.04 b

// Modified for date display
// (removed red color, despite May Day, restored hyphen before day)
// and insertion of comma after project 
// 2016-05-01

var strClip = (function () {
    'use strict';

    function fnTP3Context(editor, options) {

        // concatMap ::  [a] -> (a -> [b]) -> [b]
        function concatMap(xs, f) {
            return [].concat.apply([], xs.map(f));
        }

        function itemProject(item) {
            var p = item.parent;

            while (p) {
                if (p.getAttribute('data-type') === 'project') {
                    return p.bodyContentString;
                };
                p = p.parent;
            }
            return '';
        }

        function timePart(item, strTimeTag) {
            var strDate = item.getAttribute('data-' + strTimeTag);

            return strDate ? strDate.slice(10, 16)
                .trim() : '';
        }

        function otherTags(lstAttribNames, attribs, strGroup) {
            return concatMap(
                    lstAttribNames,
                    function (k) {
                        var strTag = k.indexOf(
                                'data-'
                            ) === 0 ? k.slice(5) :
                            '',
                            strValue = attribs[k];

                        // Non-grouping tag attributes included
                        return (
                            strTag && ['type', strGroup]
                            .indexOf(strTag) === -1
                        ) ? [
                            '@' + strTag + (strValue ? '(' + strValue + ')' : '')
                        ] : [];
                    }
                )
                .join(' ');
        }

        var outline = editor.outline;


        // READING OF SORT SPEC
        var lstOrder = options.orderBy.split(/\s+/),
            strSortBy = lstOrder[0].trim(),
            blnZA = (lstOrder.length > 1) && (lstOrder[1].trim()
                .substr(0, 4)
                .toLowerCase() === 'desc'),
            strSortAttrib = 'data-' + strSortBy;


        // READING OF GROUPING SPEC
        var lstGroup = options.groupBy.split(/\./),
            strGroup = lstGroup[0],
            strGroupAttrib = 'data-' + strGroup,
            strGroupFn = lstGroup.length > 1 ? lstGroup[1] : undefined;


        // FILTERED BY ...
        var lstFiltered = outline.evaluateItemPath(options.filter),

            // GROUPED BY ...
            dctGroups = lstFiltered.reduce(
                function (groups, dctItem) {
                    var v = dctItem.getAttribute(strGroupAttrib),
                        vGroup = strGroupFn ? eval(
                            '"' + v.toString() + '".' + strGroupFn
                        ) : v,
                        dctGroup = groups[vGroup] || {};

                    dctGroup[dctItem.id] = dctItem;
                    groups[vGroup] = dctGroup;

                    return groups;
                }, {}
            ),

            // GROUPS ORDERED BY
            lstGroups = Object.keys(dctGroups)
            .sort(blnZA ?
                function (a, b) {
                    return a === b ? 0 : (a < b ? 1 : -1);
                } : function (a, b) {
                    return a === b ? 0 : (a > b ? 1 : -1);
                });

        return "### <font color='silver'>" + strGroup.charAt(0)
            .toUpperCase() + strGroup.slice(1) + "</font>\n\n" +
            lstGroups.map(function (k) {
                var dctGroup = dctGroups[k];

                // return "#### <font color=gray>" + k.substr(0, 7) + // delta
                return "#### <font color=gray>" + k.substr(0, 10) +
                    // '</font>&nbsp;<font color=red>' + k.slice(8) + // delta
                    // (deleted)
                    '</font>\n' + Object.keys(dctGroup)
                    .map(function (id) {
                        var item = dctGroup[id];

                        var strTime = timePart(item, strGroup),
                            strProject = itemProject(item),
                            attribs = item.attributes,

                            strPrefixes = (strTime ?
                                '<font color=gray><b>' +
                                strTime +
                                '</font></b>\t' :
                                '') + (strProject ?
                                '<font color=gray>' +
                                //strProject + // delta
                                strProject + ':' +
                                '</font>&nbsp;' : '');

                        return strPrefixes +
                            // body text
                            item.bodyContentString
                            // Any remaining (non-grouping) tags
                            + ' <font color=gray>' + otherTags(
                                item.attributeNames,
                                attribs,
                                strGroup
                            ) + '</font>';
                    })
                    .sort()
                    .join('\n');
            })
            .join('\n\n');
    }

    var ds = Application("TaskPaper")
        .documents,
        varResult = ds.length ? ds[0].evaluate({
            script: fnTP3Context.toString(),
            withOptions: {
                filter: '//@due',
                groupBy: 'due.substr(0, 10)',
                orderBy: 'due'
            }
        }) : false;

    return varResult;
})();


var a = Application.currentApplication(),
    sa = (a.includeStandardAdditions = true, a);

sa.setTheClipboardTo(strClip);

strClip


#9

Thank you, this works perfectly!! I appreciate your help in this very much.
Paul


#10

@complexpoint: do you know how I can change the color of the cursor when I point it over regular text in a document (hence when it has the shape of a vertical bar)? I am not referring to “caret”, or the the cursor when it points at a tag; I figured out how to change their colors.
The reason I am asking is that I have black as background color, and white as textcolor, and I would like to have this little vertical bar to be a little bit more visible.
Paul


#11

In TaskPaper windows rather than Marked 2 windows, I imagine ?

Probably worth a broader question to the forum under a theme heading – I’m afraid I have only looked into styling enough to get a very plain amber-on-dark display – but there are a number of people who know a bit more, I think :slight_smile:


#12

Yes, in TP windows.
I will repost this as a separate question.
Thanks,
Paul