Help with a Taskpaper action for Today project

I’ve been trying Drafts for a Taskpaper alternative for iOS. So far, I’m enjoying it but I would like to try to sort out my today view. I’m currently using the script below for the archive option where it grabs everything with a ‘done’ tag and then makes an archive folder and tags it with it’s existing project and then moving it to the bottom.
What I’m trying to to is have the script grab everything with the tag ‘today’, mark it with the project it’s in and then move it to a today project at the top of the list. Is this even possible and can anyone please help me?

Blockquote

// Archive @done tasks

// Set up variables.
var orig = draft.content;
var active = '';
var archive = '';
var project = '';
var archiveRE = /^Archive:$/m;
var projectRE = /^([^:]+):\s*$/;
var doneRE = /@done/;

// Save the Archive section if there is one.
var archStart = orig.search(archiveRE);
if (archStart >= 0) {
	archive = orig.substring(archStart, orig.length);
	archive = archive.replace(/\s*$/, '\n');
	archive = archive.replace(/^Archive:\n/, '');
} else {
	archStart = orig.length;
}

// Go through the unarchived tasks, line by line.
// Keep track of the current project and collect @done items.
orig = orig.substring(0, archStart).replace(/\s+$/, '')
var lines =orig.split('\n');
var len = lines.length;
var i;
for (i=0; i<len; i++) {
	var isProject = lines[i].match(projectRE);
	if (isProject) {
		project = isProject[1];
		active += lines[i] +  '\n';
	} else {
		if (lines[i].match(doneRE)) {
			// New archived lines go on top of existing archive
			// to match Mac TaskPaper behavior.
			archive = lines[i] + ' @project(' + project + ')\n' + archive;
		} else {
			active += lines[i] + '\n';
		}
	} 
}

// Replace the draft with the edited active and archive sections.
editor.setText(active + '\nArchive:\n' + archive);
editor.setSelectedRange(0, 0);

Could you put a ``` before and after the code? That will make it much easier to read :slight_smile:

You’re right that a lot of the Archive Done script can be reused. The main difference (apart from trivial changes to the regexes) between the Archive Done script and what you want to do is that the Archive section is at the bottom of the task list and you want the Today section to appear at the top. This means the logic used to find the end of the Today section will be more complicated, but it doesn’t seem insurmountable. Unless someone else finds a solution sooner, I’ll give it a try over the weekend.

Also: I’m assuming that when a task goes into the Today section, it is removed from its original section, not duplicated. Otherwise, you have two versions of the same task and have to keep them in sync. I don’t think that’s tenable in a plain text editor.

Thanks for the tip Rosemary, looks better now.

Thanks for trying to take a look drdrang, I appreciate it. My coding skills are pretty non existent, the best I can do is play around with bits and pieces and I can sometimes make them work, this one failed me.

Yes, you are correct in assuming that I would like the task to be removed from the original section, similar to the way the archive function works.

Thanks again.

The “weekend” lasted longer than I thought it would, but here’s the script. It goes into a single-step action I call “Collect Today.”

If there’s no Today section in the TaskPaper draft, it creates one at the top of the draft; if there is, it adds the new @today items at the end of it.

I’ve tested it reasonably well, I think, but because I’m not currently using TaskPaper, I couldn’t give it a lot of exposure to real-world conditions. Also, I can’t say it’s the most elegant script I’ve ever written. Good luck, and let me know how it works for you.

// Collect @today tasks into a Today section at the top

// Set up variables.
var orig = draft.content;
var active = '';
var today = '';
var archive = '';
var project = '';
var todayRE = /^Today:$/m;
var archiveRE = /^Archive:$/m;
var headerRE = /^[^:\n]+:\s*$/m;
var projectRE = /^([^:]+):\s*$/;
var tagRE = /@today/;

// Save the Archive section if there is one. It starts at the
// Archive heading and ends at the end of the document.
var archStart = orig.search(archiveRE);
if (archStart >= 0) {
	archive = orig.substring(archStart, orig.length);
	archive = archive.replace(/\s*$/, '\n');
	archive = archive.replace(/^Archive:\n/, '');
} else {
	archStart = orig.length;
}
orig = orig.substring(0, archStart).replace(/\s+$/, '')


// Save the Today section if there is one. It starts at the
// Today heading and ends at the beginning of the next project.
var todayStart = orig.search(todayRE);
if (todayStart >= 0) {
	var orig = orig.substring(todayStart + 7);
	var todayEnd = orig.search(headerRE);
	today = orig.substring(0, todayEnd);
	today = today.replace(/\s*$/, '\n');
	today = today.replace(/^Today:\n/, '');
} else {
	todayStart = 0;
	todayEnd = 0;
}
orig = orig.substring(todayEnd).replace(/^\s+/, '')

// Go through the unarchived tasks, line by line.
// Keep track of the current project and collect @today items.
var lines =orig.split('\n');
var len = lines.length;
var i;
for (i=0; i<len; i++) {
	var isProject = lines[i].match(projectRE);
	if (isProject) {
		project = isProject[1];
		active += lines[i] +  '\n';
	} else {
		if (lines[i].match(tagRE)) {
			// New today lines go at end of existing
			// Today section.
			today = today + lines[i] + ' @project(' + project + ')\n';
		} else {
			active += lines[i] + '\n';
		}
	} 
}

// Replace the draft with the edited active and archive sections.
editor.setText('Today:\n' + today + '\n' + active + '\nArchive:\n' + archive);
editor.setSelectedRange(0, 0);
1 Like

Thanks a lot, it looks like it’s working perfectly in my testing so far. I really appreciate your time here.
I’m interested that you said you are not using Taskpaper. Your post about your action group is what convinced me to go back to Taskpaper after playing around with Things again. Can I ask what you are using now?

Things :grin:

If I were still printing my task lists to keep on my desk, I’d probably still be using TaskPaper. But with increasing iPad use, it seemed better to go with an electronic-only system.

I do like Things, I have been using it for a while but with my work, I have to use a Windows machine that I can’t install software on so it sort of limits me. Having a Taskpaper doc in Dropbox that I download in the morning and leave on my desktop seems to be the best solution.
Anyway, I enjoy playing around with Drafts and seeing what I can get it to do. One of my plans for the new year is to learn some code so hopefully I’ll become a little more proficient and rely less on butchering someone else’s scripts.