Hello all!
I have a script that grabbed the title and link of the active page in Safari and then placed those into a Links: project in TaskPaper. If the Links: project doesn't currently exist, it is created before the data is added as a task.
It was a bit of a Frankenstein script (using AppleScript, JavaScript and a Keyboard Maestro variable). It worked, but my inner coder wanted to improve it. I wanted to rewrite it to make it tighter and to help me learn more JavaScript.
Now, with the help of the amazing @JMichaelTX and the genius Jesse at TaskPaper, it is a tight and pure JavaScript macro. I had many hurdles to get through this, and both J. Michael and Jesse were inspirational throughout. Kudos!
In case this is of interest to any TaskPaper users here, below is the code. Simply place it into a Execute JavaScript For Automation action in your macro
I love this script, and I get a little thrill each time I run it. Enjoy!
'use strict';
var app = Application.currentApplication()
app.includeStandardAdditions = true
var safariApp = Application("Safari");
var SafDoc = safariApp.documents[0];
var varTitle = SafDoc.name();
var varURL = SafDoc.url();
var varTask = "- " + varTitle + " → " + varURL;
function TaskPaperContext(editor, options) {
let outline = editor.outline
let project = outline.evaluateItemPath('/project Links')[0]
if (project == null) {
project = outline.createItem('Links:')
outline.root.insertChildrenBefore([project], outline.root.firstChild)
}
project.insertChildrenBefore(
outline.createItem(options.url),
project.firstChild
)
}
Application('TaskPaper').documents[0].evaluate({
script: TaskPaperContext.toString(),
withOptions: { url: varTask },
});