Script help sending lines to Things

I created an action a few weeks ago that takes lines in Drafts that have a specific hashtag and compiles them into a single draft. This works, but I decided I’d rather send these lines to Things instead. I revised the script, but for some reason, it’s not sending any information to Things (Things opens, but no new tasks are created).

You’ll see some debugging lines in there: the console log commands near the TJSTodo.create() line all show that the correct information is being stored in those values. But the alert to display the value of arrTasks shows that the array is empty (or more precisely, full of empty objects).

Any ideas where I’m going wrong?

// Hashtagged tasks to Things

// Global variables. 
// Set the "tag" to whatever hash tag you want to use

let tag = "#week";

// find all drafts in the "Projects" workspace archive. Set your workspace and query accordingly (you might want drafts in the inbox, or all, or whatever)

let ws = Workspace.find("Projects");
let dList = ws.query("archive");
var arrTasks = [];
// Loop through each draft

for (let z in dList)
{
    
    // Split drafts into lines

    let d = dList[z].content;
    let lines = d.split("\n");
    
    

    function filterItems(arr, query) {
        return arr.filter(function(el) {
          return el.toLowerCase().indexOf(query.toLowerCase()) !== -1
        })
      }

    // Look for #week tag in each line

    for (let line of lines)
    {    
        if (line.indexOf('- [x] ') !== -1)
        {

        }
        else
        {
            // Grab the draft content
            let fullContent = dList[z].content;
            
            // If the line includes the #week tag, add to array for sending to Things
            if (line.includes(tag))
            {   
                line = line.replace(/- /,'');
                                let fullTask = fullContent.split('\n- ')
                                            .find(task => task.includes(line));
                let notesArray = fullTask.split('\n');
                notesArray.shift();
                let newArray = filterItems(notesArray,"> ");
                let notes = newArray.join('\n');

                var todo = TJSTodo.create();
                todo.title = line;
                todo.notes = notes + "\n\n" + dList[z].permalink;
                todo.list = dList[z].processTemplate("[[title]]");
                todo.tags = ["week"];

                console.log(JSON.stringify(todo.title));
                console.log(JSON.stringify(todo.notes));
                console.log(JSON.stringify(todo.list));
                console.log(JSON.stringify(todo.tags));

                arrTasks.push(todo);
            }
        }
    }   
}
alert(JSON.stringify(arrTasks));
var container = TJSContainer.create(arrTasks);
var cb = CallbackURL.create();
cb.baseURL = container.url;
var successT = cb.open();
if (successT) {
    console.log("Project created in Things");
    app.openURL("things:///show?id=anytime&filter=week");
}
else {
    context.fail();
}

a little more debugging:

var todo = TJSTodo.create();
                todo.title = line;
                todo.notes = notes + "\n\n" + dList[z].permalink;
                todo.list = dList[z].processTemplate("[[title]]");
                todo.tags = ["week"];

                alert(JSON.stringify(todo.title));
                //console.log(JSON.stringify(todo.notes));
                //console.log(JSON.stringify(todo.list));
                //console.log(JSON.stringify(todo.tags));

                alert(JSON.stringify(todo));
                arrTasks.push(todo);
                alert(JSON.stringify(arrTasks));

The first alert (alert(JSON.stringify(todo.title));) correctly displays the task title. However, the next two alerts display blank objects ({}, and then [{}]).

This exact code works in other scripts I use, so I’m not sure why todo isn’t getting pushed to the variable correctly. It is initialized at the top of script as var arrTasks = [];

I would not expect stringify’ing a TJSTodo object to have any keys necessarily, I don’t think that’s weird. It’s not a native JS object.

What’s a sample draft text you are running this on? What is in the cb.callbackResponse object after returning from Things? What is the cb.status?

Ok. I’ve checked the output of ‘cb.url’ and it looks like drafts is sending what I expect. The issue must be the data being read by Things (I suspect hash marks in the list titles) but I will need to do more debugging. Just got off track trying to peer into the TJS containers. Thanks for the reminder to look at the callback data.

For future visitors–the code pasted above is fine, provided you take out all the hash tags. That’s what was making Things balk at creating tasks.[edit: actually, Things was making tasks with all my failed attempts–it was just putting them in the Inbox instead of Anytime, where I expected to see them.] Here’s a working version:

// Hashtagged tasks to Things

// Global variables. 
// Set the "tag" to whatever hash tag you want to use

let tag = "#week";

// find all drafts in the "Projects" workspace archive. Set your workspace and query accordingly (you might want drafts in the inbox, or all, or whatever)

let ws = Workspace.find("Projects");
let dList = ws.query("archive");
var arrTasks = [];
// Loop through each draft

for (let z in dList)
{
    
    // Split drafts into lines

    let d = dList[z].content;
    let lines = d.split("\n");
    
    

    function filterItems(arr, query) {
        return arr.filter(function(el) {
          return el.toLowerCase().indexOf(query.toLowerCase()) !== -1
        })
      }

    // Look for #week tag in each line

    for (let line of lines)
    {    
        if (line.indexOf('- [x] ') !== -1)
        {

        }
        else
        {
            // Grab the draft content
            let fullContent = dList[z].content;
            
            // If the line includes the #week tag, add to array for sending to Things
            if (line.includes(tag))
            {   
                line = line.replace(/- /,'').replace('#week','');
                                let fullTask = fullContent.split('\n- ')
                                            .find(task => task.includes(line));
                let notesArray = fullTask.split('\n');
                notesArray.shift();
                let newArray = filterItems(notesArray,"> ");
                let notes = newArray.join('\n');

                var todo = TJSTodo.create();
                todo.title = line;
                todo.notes = notes + "\n\n" + dList[z].permalink;
                todo.list = dList[z].processTemplate("[[title]]").replace('# ','');
                todo.tags = ["week"];
                
                arrTasks.push(todo);
               
            }
        }
    }   
}

var container = TJSContainer.create(arrTasks);
var cb = CallbackURL.create();
cb.baseURL = container.url;

var successT = cb.open();
if (successT) {
    console.log("Project created in Things");
    app.openURL("things:///show?id=anytime&filter=week");
}
else {
    context.fail();
}
2 Likes