Action
Add Task(s) to a Taskpaper File Inbox in Dropbox
Adds the current draft task(s) to inbox section, at the start of the
PLEASE EDIT THE SCRIPT AND ENTER THE DROPBOX PATH AND FILE
© Rapid Definition 2020
Software is provided as is, with no support or warranty implied
Steps
-
script
// addToInbox.js // Takes a Draft and adds one or more tasks to a Taskpaper file. /* Pseudo Code: - Count number of tasks/lines from focused editor - getAndCleanTasksToAdd() - Open up file from Dropbox and read into memory - openAndCheckTaskpaperFile ( filePathAndName ) - Check for existence of inbox header - openAndCheckTaskpaperFile ( filePathAndName ) - Insert line(s) at 2nd line position of file (after inbox header) - Put selected text into 2nd line position of file. - Save file back to Dropbox - Archive draft containing task(s) */ // Set local varible to the Dropbox folder/filename where TaskPaper document is stored let filePathAndName = "/Taskpaper/<REPLACE WITH FILENAME>.taskpaper" let tasks = getAndCleanTasksToAdd() let textToBeWorkedUpon = openAndCheckTaskpaperFile(filePathAndName) let newFileContent = constructNewFileConent(textToBeWorkedUpon, tasks) overwriteTaskpaperFileToDropbox(filePathAndName, newFileContent ) function getAndCleanTasksToAdd () { // Take the current draft and split into tasks based on lines. Blank lines are removed. Returns an array with the individual tasks as elements. let newTaskText = editor.getText() let importedLines = newTaskText.split(/\r\n|\r|\n/) let numberOfLines = importedLines.length // remove blank lines let cleanLines = new Array() let numberOfRemovedLines = 0 for (i = 0; i < numberOfLines; i++) { // if line only has EOL character...then it is blank. if ( importedLines[i].length > 1 ) { cleanLines.push(importedLines[i]) numberOfRemovedLines++ } //end if } //end for return cleanLines } function openAndCheckTaskpaperFile ( filePathAndName ) { // Open the passed file, does a rudementary check for expected Taskpaper format (i.e. file starts with "Inbox:") and gets ready for tasks be added // Hardcoded file name for develpment/debugging purposes. // var filename = '/Taskpaper/Personal-test.taskpaper'; let d = Draft.create(); // Get the file from Dropbox and set draft to it. var db = Dropbox.create(); var fileContent = db.read(filePathAndName); //editor.setText(content); d.content = fileContent; // Check for existence of inbox header if (d.title != "Inbox:") { //alert ("This file looks good!") throw new Error("File " + filePathAndName + " doesn't seem to be expected Taskpaper format"); } console.log("File open, checked and ready for task(s) import") return d.content } function constructNewFileConent(textToBeWorkedUpon, tasks) { //creates the new Taskpaper file contents, based on the existing file text (textToBeWorkedUpon) and an array of new tasks (tasks) to add to the top of the existing file in order. Returns the new file content as a string. // Cut out the Inbox: prefix let newText = "Inbox:\n\t" //Iterate backward through the list of new task (to add them into the righ order they were created in), and add tasks directly after the prefix. for (i = tasks.length -1; i >= 0; i--) { newText = textToBeWorkedUpon.slice(0, 8) + "- " + tasks[i] + "\n\t" + textToBeWorkedUpon.slice(8) textToBeWorkedUpon = newText } return textToBeWorkedUpon } function overwriteTaskpaperFileToDropbox( filePathAndName, newFileContent ) { // Overwrites the Taskpaper file located at filePathAndName with the passed string (newFileContent) // create Dropbox object let db = Dropbox.create(); // setup variables let path = filePathAndName; let content = newFileContent; // write to file on Dropbox let success = db.write(path, content, "overwrite", true); if (success) { // write worked! var dbContent = db.read(path); if (dbContent) { // read worked! if (dbContent == content) { console.log("File contents match!"); } else { console.log("File did not match"); context.fail(); } } else { // read failed, log error console.log(db.lastError); context.fail(); } } else { // write failed, log error console.log(db.lastError); context.fail(); } } // © Rapid Definition 2020 // Software is provided as is, with no support or warranty implied
Options
-
After Success Archive Notification Info Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.