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();
}