"Move to Project": to beginning or end?

In the meanwhile, a script which moves the selected items to the end of the chosen project list.

(A variant of an earlier script, with an added destination option – see end of script)

(function (dctOptions) {
    'use strict';
// ver 0.2 option at end of script
//         for moving tasks to start or end of chosen project

    // TASKPAPER CONTEXT FUNCTIONS

    // PROJECTS IN ACTIVE DOCUMENT
    function tpProjectList(editor, options) {

        return (editor.selection.selectedItems.length > 0) ?
            editor.outline.evaluateItemPath(
                '//@type=project')
            .map(function (p) {
                var strName = p.bodyString;

                return {
                    id: p.id,
                    name: strName.slice(
                        0, strName.indexOf(':')
                    )
                }
            }) : [];
    }

    // MOVEMENT OF SELECTED ITEMS TO START OR END OF CHOSEN PROJECT
    function tpMoveSelected(editor, options) {

        var outline = editor.outline,
            lstSelns = editor.selection.selectedItems,
            lngSelns = lstSelns.length,
            blnStart = options.toStart;

        if (lngSelns > 0) {
            var oFirst = lstSelns[0],
                oLast = lngSelns > 1 ? lstSelns[lngSelns - 1] : oFirst,

                oProject = outline.getItemForID(
                    options.projectID
                ),
                oFirstChild = oProject.firstChild || (function () {
                    outline.groupUndoAndChanges(function () {
                        oProject.appendChildren(outline.createItem());
                    });
                    return oProject.firstChild;
                })();

            // If we have a non-circular destination, make the move ...
            if (oFirstChild && (lstSelns.indexOf(oFirstChild) === -1)) {
                outline.groupUndoAndChanges(function () {

                    if (blnStart) {
                        outline.insertItemsBefore(
                            lstSelns,
                            oFirstChild
                        );
                    } else {
                        oProject.appendChildren(lstSelns);
                    }

                });

                // make sure that source and target are both visible
                editor.hoistedItem = null;
                editor.itemPathFilter = '';

                // Select the newly moved items, for more visibility
                editor.moveSelectionToItems(
                    oFirst, 0,
                    oLast, oLast.bodyString.length
                );

                return lstSelns.length;
            } else return 0
        } else return 0;
    }

    // JAVASCRIPT FOR AUTOMATION CONTEXT

    // PROJECTS IN ACTIVE DOCUMENT ?
    var tp3 = Application("com.hogbaysoftware.TaskPaper3"),
        ds = tp3.documents,
        d = ds.length ? ds[0] : undefined,
        lstProjects = d ? d.evaluate({
            script: tpProjectList.toString(),
        }) : [];

    // USER CHOICE OF TARGET PROJECT ?
    if (lstProjects.length > 0) {
        var a = Application.currentApplication(),
            sa = (a.includeStandardAdditions = true, a),
            e = Application("SystemUIServer"),
            se = (e.includeStandardAdditions = true, e);

        var lstMenu = lstProjects.map(function (x) {
                return x.name;
            }),

            varChoice = (se.activate(),
                se.chooseFromList(lstMenu, {
                    withTitle: "Move items to " +
                    (dctOptions.toStart ? 'start' : 'end' ) +
                    " of chosen Project",
                    withPrompt: 'Choose:',
                    defaultItems: lstMenu[0],
                    okButtonName: 'OK',
                    cancelButtonName: 'Cancel',
                    multipleSelectionsAllowed: false,
                    emptySelectionAllowed: true
                })),

            lstChoice = varChoice ? varChoice : [];

        if (lstChoice.length > 0) {
            // MAKE THE MOVE IN TASKPAPER 3
            var dctChoice = lstProjects[lstMenu.indexOf(lstChoice[0])],
                lngMoved = d.evaluate({
                    script: tpMoveSelected.toString(),
                    withOptions: {
                        projectID: dctChoice.id,
                        toStart: dctOptions.toStart
                    }
                });

            // AND REPORT ON ANY RESULT
            if (lngMoved > 0) {
                sa.displayNotification('in TaskPaper 3 Preview', {
                    withTitle: lngMoved + ' selected item' + (
                        lngMoved > 1 ? 's' : ''
                    ) + " moved",
                    subtitle: "to " +
                        (dctOptions.toStart ? "start" : "end") +
                        " of '" + dctChoice.name + "'",
                    sound: 'glass'
                })
            }
            tp3.activate();
            return lngMoved;
        }
    }
})({
    toStart: false // if false, the items are moved to end of project list
});

2 Likes