I’ve been working on adapting this code for a redo of the PopClip extension for TP3, but not having much luck parsing the input. Anyone see anything glaring that’s wrong in the input portion of the script? I can’t seem to pull the PopClip input which is stored in an env variable…
function run(input, parameters) {
ObjC.import('stdlib')
var selectedText = $.getenv('POPCLIP_TEXT')
var TaskPaper = Application('TaskPaper')
// 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);
return true;
}
}
Looking at it more closely, the default extension simply puts the selected text, without modification, through the default TaskPaper parser. In other words, if the selection is prefixed with a hyphen, then it will come in as a task, but not otherwise. An advantage of this approach would be that you could bring in several selected lines, and get proper indentation/nesting of them in TaskPaper.
(I think, however, that the default extension is configured at the moment (by a gate-keeping regular expression (?s)^[\n\r]*[^\n\r]{1,300}[\n\r]*$) to respond only to single line selections)
The existing extension is written in php + JS, but an AppleScript + JS equivalent of what it is doing would be something like the code below.
It is only using the {popclip text} field, but PopClip can make a number of other fields available.
See the full list at:
If any of these fields look useful, then various alternative extensions, or an extension with some options, could perhaps be sketched out.
on run
set strJS to "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);
}"
tell application "TaskPaper"
set docs to documents
if length of docs > 0 then
set doc to front document
else
set doc to make new document
end if
activate
tell doc
set varResult to (evaluate script strJS with options ¬
{|text|:"{popclip text}", urls:"{popclip urls}", titles:"{popclip url titles}"})
end tell
end tell
end run
Thank you for the explanation. Now that I understand how it’s supposed to work it all makes sense to me. Thanks for creating this PopClip extension. Very useful!
The current build of TaskPaper has the application id “com.hogbaysoftware.TaskPaper3.direct”
whereas inside the popclip extension bundle the .plist file and tp.php script are both looking for either “com.hogbaysoftware.TaskPaper3” or “com.hogbaysoftware.TaskPaper.mac”
The trick will be to alert the supplier of Popclip that there is a new id in the field, and ask them to update their extension so that it can find the TaskPaper app.