Sorting items with tags by due date?

Hi,
Does anyone know if/how one can sort tagged items? More precisely, I use tags with dates, such as @due(12-1-2015), and would like to:

  1. extract a list of all items tagged with @due (I do this by clicking on the tag);
  2. display these tagged items sorted by date;
  3. and if possible, print this sorted list.

Thank you,
Paul

Various ways …

If you are using OS X 10.10 (Yosemite), you could run this script in Script Editor, or assign it to a keystroke with something like Keyboard Maestro:

function run() {
	var blnDebug = 0;

	function fnFT(editor, opt) {
			
		// 	EVALUATE THE NODE PATH //@due TO HARVEST MATCHES
		var oTree = editor.tree(),
			lstNodes = oTree.evaluateNodePath('//@due'),
			lstDue=[], strList;
		
		// BUILD A LIST OF DATES FOLLOWED BY TEXT
		lstNodes.forEach(function (oNode) {
			lstDue.push(oNode.tag('due') + '\t' + oNode.text());
		});
		
		// SORT AND CONVERT TO STRING
		strList = lstDue.sort().join('\n');
		
		
		// PLACE IN CLIPBOARD
		Pasteboard.writeString(
			strList
		);
		
		return strList;
		
	}

	var docsFT = Application("FoldingText").documents(),
		oDoc = docsFT.length ? docsFT[0] : null,
		fnProcess, varResult;

	if (!oDoc) return null;

	fnProcess = (blnDebug ? oDoc.debug : oDoc.evaluate);

	varResult = fnProcess({
		script: fnFT.toString(),
		withOptions: {}
	});

	return varResult;
}

Just noticed that the date format you are using looks like MM-DD-YYYY which won’t work well with a simple string sort.

It does simplify things to go with the ISO pattern YYYY-MM-DD, which sorts in the way you would expect.

To use the MM-DD pattern, a custom sort function would need to be written.

Thank you very much!
I do not have any experience with scripts whatsoever, will have to figure out how this works.
I found the suggestions at http://support.foldingtext.com/t/how-to-install-a-script/75.
Following the instructions to try a script, I copied the script into the Apple script editor, but got a syntax error at the opening bracket “(” in the first line of the script, “function run() {”.
My lack of experience is probably the cause of forgetting some crucial steps in making script work.
(If the script works, I plan to install it (will then have to figure out how to save it in “script” format)).
Thank you,
Paul

Thank you – will incorporate this if I get the script installation working.

The Yosemite script editor understands Javascript like this, but expects Applescript by default.

It has a little pull-down at upper right (under ‘Record’) which enables you to specify Javascript:

Thank you, I managed to do this, and next I managed to follow the steps on
http://support.foldingtext.com/t/how-to-install-a-script/75

Trying out the script seemed to work, since it generated (recognizable) output below the script-editing field, though not yet in a useful format, at first sight at least, form looking at the little screen in the script editor. Next, I installed the script by saving the script into the indicated folder.

When I executed the script, nothing seemed to happen. I figured out that the output is written to the clipboard, and can be “written” to an empty FT document by pressing CMD-V. Perhaps this instruction could be added somewhere on the forum.

Thank you so much,
Paul

Good point – here’s a version which creates a fresh document containing the sorted due list.

function run() {
	var	blnDebug = 0;
	
	// create new doc from sorted list ? 
	var blnMakeDoc = 1; //(0 if not)
	

	function fnFT(editor, opt) {
			
		// 	EVALUATE THE NODE PATH //@due TO HARVEST MATCHES
		var oTree = editor.tree(),
			lstNodes = oTree.evaluateNodePath('//@due'),
			lstDue=[], strList;
		
		// BUILD A LIST OF DATES FOLLOWED BY TEXT
		lstNodes.forEach(function (oNode) {
			lstDue.push(oNode.tag('due') + '\t' + oNode.text());
		});
		
		// SORT AND CONVERT TO STRING
		strList = lstDue.sort().join('\n');
		
		
		// PLACE IN CLIPBOARD
		Pasteboard.writeString(
			strList
		);
		
		return strList;
		
	}

	var appFT = Application("FoldingText"),
		docsFT=appFT.documents(),
		oDoc = docsFT.length ? docsFT[0] : null,
		fnProcess, varResult, docResult;

	if (!oDoc) return null;

	fnProcess = (blnDebug ? oDoc.debug : oDoc.evaluate);

	varResult = fnProcess({
		script: fnFT.toString(),
		withOptions: {}
	});

	// If blnMakeDoc (at the top of this script) = 1
	// then create a new document to contain the result
	
	if (blnMakeDoc && varResult) {
		docResult = appFT.Document().make();
		docResult.textContents = "### Due\n" + varResult;
	}
	return varResult;
}



Works perfectly – thank you very much for your help!!
Best wishes,
Paul

Hi,
I found this script very useful. I was wondering if is it possible to modify the script in the way to extract all ‘due’ tags from multiple files at one run. I use one index file that contains links to all my projects files. Currently, I have over 20 files, each with a separate todo list. Extracting task list into one ‘due’ file would help to review all todos.
Maybe there is another way of doing that without the due date script?
Thanks
Marek

You can extract a tag+value report across several files by using the FoldingText CLI tool. See:

http://support.foldingtext.com/t/querying-grouping-sorting-across-several-text-files-with-the-foldingtext-command-line-interface/369