Script error

I had been using this nifty pair of scripts that format from TaskPaper to FoldingText “syntax” (and back). I haven’t had a call to use either for a while, but when I did today, the clipboard contained only the following:

TypeError: editor.selectedRange is not a function. (In 'editor.selectedRange()', 'editor.selectedRange' is undefined)

evaluateScript@file:///Applications/TaskPaper.app/Contents/Resources/bircheditor.js:40:26288

Use the Help > SDKRunner to debug

I’m at a loss for where to try to fix this. I don’t think it’s something with the AppleScript since was working fine for quite a while. Any suggestions would be greatly appreciated.

The AppleScript code is here:

property pTitle : "Copy from TaskPaper in FOLDINGTEXT format "
property pVer : "0.1"
property pAuthor : "Rob Trew @complexpoint on Twitter"
property pRepo : "https://github.com/RobTrew/txtquery-tools"
property pLicense : "MIT"
property pstrJS : "
	
	function(editor, options) {
		// HOW MANY PRECEDING TABS OR HASHES FOR THIS LINE IN FT ?
		function FTPrefix(oNode) {
			var oParent=oNode.parent, lngLevel=1, lngProjLevel=1, blnFound=false,
			strType=oNode.type(), blnProj = (strType == 'project'), strPrefix;
			blnFound = blnProj;
			while (oParent) {
				lngLevel++;
				if (blnFound) lngProjLevel ++;
				else blnFound = (oParent.type() == 'project');
				oParent = oParent.parent;
			}
			if (blnProj) strPrefix = '\\n' + Array(lngLevel).join('#') + ' ';
			else strPrefix = Array(lngLevel-lngProjLevel).join('\\t');
			return strPrefix;
		}
		// GET THE SELECTED LINES
		var lstNodes = editor.selectedRange().nodesInRange(),
				lstLines=[], varNode, strLine, rgxEndColon = /^(.*):(.*?)$/;
		// AND GIVE AN FT PREFIX (HASHES OR TABS) TO EACH ONE
		lstNodes.forEach(function (oNode) {
			strLine = oNode.line().trim();
			
			// REMOVING THE COLON FROM PROJECTS (BUT LEAVING TRAILLING TAGS)
			if (oNode.type() == 'project')
				strLine=strLine.replace(rgxEndColon, '$1$2');
			lstLines.push([FTPrefix(oNode),strLine].join(''));
		});
		return lstLines.join('\\n');
	}
"
on run
	set varResult to missing value
	tell application "TaskPaper"
		set lstDocs to documents
		if lstDocs ≠ {} then
			tell item 1 of lstDocs to set varResult to (evaluate script pstrJS)
			set the clipboard to varResult
		end if
	end tell
	return varResult
end run

Not well tested, but I’ve updated the script to run:

property pTitle : "Copy from TaskPaper in FOLDINGTEXT format "
property pVer : "0.1"
property pAuthor : "Rob Trew @complexpoint on Twitter"
property pRepo : "https://github.com/RobTrew/txtquery-tools"
property pLicense : "MIT"
property pstrJS : "
	
	function(editor, options) {
		// HOW MANY PRECEDING TABS OR HASHES FOR THIS LINE IN FT ?
		function FTPrefix(oNode) {
			var oParent=oNode.parent, lngLevel=1, lngProjLevel=1, blnFound=false,
			strType=oNode.getAttribute('data-type'), blnProj = (strType == 'project'), strPrefix;
			blnFound = blnProj;
			while (oParent) {
				lngLevel++;
				if (blnFound) lngProjLevel ++;
				else blnFound = (oParent.getAttribute('data-type') == 'project');
				oParent = oParent.parent;
			}
			if (blnProj) strPrefix = '\\n' + Array(lngLevel).join('#') + ' ';
			else strPrefix = Array(lngLevel-lngProjLevel).join('\\t');
			return strPrefix;
		}
		// GET THE SELECTED LINES
		var lstNodes = editor.selection.selectedItems,
				lstLines=[], varNode, strLine, rgxEndColon = /^(.*):(.*?)$/;
		// AND GIVE AN FT PREFIX (HASHES OR TABS) TO EACH ONE
		lstNodes.forEach(function (oNode) {
			strLine = oNode.bodyString;
			
			// REMOVING THE COLON FROM PROJECTS (BUT LEAVING TRAILLING TAGS)
			if (oNode.getAttribute('data-type') == 'project')
				strLine=strLine.replace(rgxEndColon, '$1$2');
			lstLines.push([FTPrefix(oNode),strLine].join(''));
		});
		return lstLines.join('\\n');
	}
"
on run
	set varResult to missing value
	tell application "TaskPaper Preview"
		set lstDocs to documents
		if lstDocs ≠ {} then
			tell item 1 of lstDocs to set varResult to (evaluate script pstrJS)
			set the clipboard to varResult
		end if
	end tell
	return varResult
end run
1 Like

Thanks for doing that – sorry I got there late

1 Like

That works! Thanks Jesse!