For a similar macro which copies the contents of the template to clipboard (rather than creating a new document with it), you can use a lightly modified version of the code in the Keyboard Maestro Execute JavaScript for Automation action:
( Or, of course, use something like Typinator )
JavaScript for Automation source:
// CHOOSE A TEMPLATE AND COPY ITS TEXT TO CLIPBOARD
// 1. CREATE A FOLDER CONTAINING TWO OR MORE MODEL TASKPAPER DOCUMENTS
// 2. SPECIFY THE PATH OF THE FOLDER IN THE templatePath VARIABLE
// 3. RUN MACRO ( requires TaskPaper 3 https://www.taskpaper.com/ )
(function (dctOptions) {
'use strict';
// expandTilde :: String -> FilePath
function expandTilde(strPath) {
return strPath.charCodeAt(0) === 126 ? ObjC.unwrap(
$(strPath)
.stringByExpandingTildeInPath
) : strPath;
}
// pathExistsAndisFolder :: String -> (Bool, Int)
function pathExistsAndisFolder(strPath) {
var ref = Ref();
return $.NSFileManager.defaultManager
.fileExistsAtPathIsDirectory(
strPath, ref) ? {
'exists': true,
'isFolder': ref[0] === 1
} : {
'exists': false
};
}
// listDirectory :: FilePath -> [FilePath]
function listDirectory(strPath, fm) {
var fm = fm || $.NSFileManager.defaultManager;
return ObjC.unwrap(
fm.contentsOfDirectoryAtPathError(strPath, null))
.map(ObjC.unwrap);
}
// fileType :: FilePath -> UTC String
function fileType(strPath) {
var error = $();
return ObjC.unwrap(
$.NSWorkspace.sharedWorkspace
.typeOfFileError(strPath, error)
);
}
// readFile :: FilePath -> IO String
function readFile(strPath) {
var ref = Ref();
return ObjC.unwrap(
$.NSString.stringWithContentsOfFileEncodingError(
strPath, $.NSUTF8StringEncoding, ref
)
);
}
// MAIN
ObjC.import('AppKit');
var strFolder = dctOptions.templateFolder,
strPath = strFolder ? expandTilde(strFolder) : undefined;
if (strPath && pathExistsAndisFolder(strPath)
.isFolder) {
var lstMenu = listDirectory(strPath)
.filter(function (x) {
return fileType(
strPath + '/' + x
) === "com.hogbaysoftware.taskpaper.document";
});
if (lstMenu.length > 0) {
var ui = Application("com.apple.systemuiserver"),
sa = (ui.includeStandardAdditions = true, ui);
sa.activate();
var varResult = sa.chooseFromList(lstMenu, {
withTitle: dctOptions.withTitle || "",
withPrompt: dctOptions.withPrompt ||
"Templates in folder:\n\n" + strFolder +
"\n\nChoose:",
defaultItems: dctOptions.defaultItems ||
lstMenu[0],
okButtonName: dctOptions.okButtonName ||
"Copy file contents",
cancelButtonName: dctOptions.cancelButtonName ||
"Cancel",
multipleSelectionsAllowed: dctOptions.multipleSelectionsAllowed ||
false,
emptySelectionAllowed: dctOptions.emptySelectionAllowed ||
false
}),
strFile = (varResult ? varResult[0] : undefined);
if (strFile) {
var tp3 = Application("com.hogbaysoftware.TaskPaper3"),
strTemplatePath = strPath + '/' + strFile,
strText = readFile(strTemplatePath);
sa.setTheClipboardTo(
strText
);
tp3.activate();
return strText;
}
}
} else return "Folder not found:\n\t" + strPath;
})({
templateFolder: Application("com.stairways.keyboardmaestro.engine")
.variables['templatePath'].value(), // e.g. '~/TaskPaper Templates',
withTitle: 'Copy template contents to clipboard'
});