Basic script to add selected text to TaskPaper 3 “Inbox”

Here’s a start:

I think the TaskPaper part is good. But the “Get Selected Text” part seems inconsistent. I’m just running it from ScriptEditor… and that might be the problem. I think when Script Editor runs a script, it first deselects all text. So I think this might run OK from a keyboard shortcut.

var TaskPaper = Application('TaskPaper')
var SystemEvents = Application('System Events') 

// Get Selected Text (not quite working)
SystemEvents.keystroke('c', { using: 'command down' });
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var selectedText = app.theClipboard()

// Send it to TaskPaper Inbox
TaskPaper.documents[0].evaluate({
	script: TPContext.toString(),
	withOptions: {text: selectedText }
});

function TPContext(editor, options) {
	var outline = editor.outline;
	var inbox = outline.evaluateItemPath("//Inbox:")[0];
	var items = ItemSerializer.deserializeItems(options.text, outline, ItemSerializer.TEXTMimeType);
	
	if (!inbox) {
		inbox = outline.createItem("Inbox:");
		outline.root.insertChildrenBefore(inbox, outline.root.firstChild);
	}
	
	inbox.insertChildrenBefore(items, inbox.firstChild);
}
2 Likes