Loading a taskpaper list from Dropbox

Hi,

I have a very simple action, lightly modified from the TaskPaper group by @drdrang, to (1) load a taskpaper file from dropbox, (2) add a ‘taskpaper’ tag to the file, (3) set a ‘taskpaper’ workspace and (4) set the syntax highlighting for the draft to TaskPaper.

If I run the action from Drafts itself, everything works as expected in iOS and macOS. But I want to call this action from the iOS Drafts widget, and if I run the action from there: (1) the tag is not applied, and (2) the syntax highlighting doesn’t change. Any ideas?

Here’s the code:

// Get TaskPaper file for current month and set the draft to it.

let workspace = Workspace.find("TaskPaper");
app.applyWorkspace(workspace);

// Dropbox folder where task file is.
var path = '/work/SUBFOLDER/';

// Assemble filename from today's date.
var today = new Date();
var yr = today.getFullYear().toString();
var filename = path + 'FILENAME.' + yr + '.taskpaper';

// Get the file from Dropbox and set draft to it.
var db = Dropbox.create();
var content = db.read(filename);
editor.setText(content);

let syntax = Syntax.find("builtIn", "TaskPaper");
draft.syntax = syntax;
draft.addTag("taskpaper")
draft.update();

This script mixes and matches editor and draft objects in a somewhat problematic way that is not obvious when run in the app, but becomes a problem when run from the widget, because the draft object is not loaded in the editor when run from a widget (similar issue when run from Shortcuts or a URL scheme).

This would work reliably from the widget if the script created a new draft, modified it, then loaded it in the editor. Modifying just the end of the script:

// Get the file from Dropbox and set draft to it.
var db = Dropbox.create();
var content = db.read(filename);

let d = new Draft();
d.content = content;
let syntax = Syntax.find("builtIn", "TaskPaper");
d.syntax = syntax;
d.addTag("taskpaper")
d.update();
editor.load(d);

Thanks, that worked perfectly.

after you load the TP file into Drafts and you edit it could you save it back to TP?

Yes. It is just a text file at the end of the day. You just need to know where it should be saved back to.

You could store that as say YAML in the draft, or you could store it externally, which is the approach I used with my meta data functions.

Example

Deeper Explanation