Creating templates on the Mac

Hi there- I’m relatively new to Drafts, so this is probably a n00b question - apologies!

I want to have a template for meeting notes. So I created a note that has a “template” tag. However, when I run the action “New Draft From Template” I get the following script error:
Script Error: TypeError: undefined is not an object (evaluating ‘workspace.tagFilter = “template”’)
Line number: 6, Column 11

Any advice?

Thanks!

Although inelegant, I believe you getting this error because that example action utilizes Workspaces to find your available templates - and Workspaces are a Drafts Pro feature.

Ohh… tricksy way to finally push me over into the pro camp! :wink:

Thanks. Hopefully you can find a way to update that error message because I thought I was doing something wrong. Maybe make “New Draft from Template” disabled as an action until you upgrade to pro. Or before executing the action check to see if the person has pro, and if not throw up a prompt to get them to upgrade.

Agreed. That is a bad experience as-is.

Bad experience aside, I just bought the subscription.

3 Likes

I’m having a similar issue with Templates. Drafts cannot find any templates I’ve created (3). They are simple like below:

Meeting Notes

[[Date]]

They are tagged as a template and saved in the archive folder. Drafts reports an error that no templates exist with instructions on how to create a template. I am a pro user with an active subscription. Any ideas?

Can you share a screenshot of your template tag filter results for the all folder?

E.g.

Also, can you share a link to your action just to verify the code you are running vs the code listed in the Action directory for Greg’s standard release version?

With both of these we should be able to review it from both ends (find and data) and perhaps even replicate your issue.

Here is the code and a screenshot of a template. Very simple I thought!

// select from a list of drafts tagged "template" and create new draft based on selection.

let f = () => {
	// create temp workspace to query drafts
	let workspace = Workspace.create();
	if (!workspace) {
		return false;
	}
	workspace.tagFilter = "template";
	workspace.setAllSort("name", false, true);
	// get list of drafts in workspace
	let drafts = workspace.query("all");
	
	// check if we found any valid templates
	if (drafts.length == 0) {
		alert("No templates found. To make templates available to this action, create a draft with the template content and assign it the tag \"template\".");
		return false;
	}
	
	// prompt to select
	let p = Prompt.create();
	p.title = "New Draft with Template";
	p.message = "Select a template. A new draft will be created based the template selected.";
	
	let ix = 0
	for (let d of drafts) {
		p.addButton(d.title, ix);
		ix++;
	}
	
	if (!p.show()) {
		return false;
	}
	
	// get the selected template draft
	let selectedIndex = p.buttonPressed;
	let template = drafts[selectedIndex];

	// create new draft and assign content/tags/syntax
	let d = Draft.create();
	for (let tag of template.tags) {
		if (tag != "template") {
			d.addTag(tag);
		}
	}
	d.languageGrammar = template.languageGrammar;
	d.content = d.processTemplate(template.content);
	d.update();
	// load new draft
	editor.load(d);
	editor.activate();
	
	// look for <|> to set cursor location
	let loc = d.content.search("<|>");
	if (p != -1) {
		editor.setText(editor.getText().replace("<|>", ""));
		editor.setSelectedRange(loc, 0);
	}
	
	return true;
}

if (app.isPro) {
	if (!f()) {
		context.cancel();
	}
}
else {
	alert("Drafts Pro features required to use this action.")
}

Superficially, it looks like that setup is correct. Could you try removing and re-adding the “template” tag from that draft? Being careful to type it. Seems possible there is a hidden character or something in the template tag on that draft so it’s not coming up as an exact match.

Done that already. Actually I re-installed the action. So I will re-install the template tag. I’ve tried several different templates to no avail.

Did not work.

image

Something in my setup? I can’t think of anything.
image

My Version

It is easier just to share the link to the action, but if you do share code, please remember to place it between two sets of triple back ticks to keep the formatting, and not change straight quotes to smart quotes, etc.

Everything looks ok at first sight.

Can you try this version of the action? I’ve added some extra alert-based debugging to see if it can produce any extra info of use in figuring out what is going on.

Here are the screenshots:

image

image

image

No new Draft created.

TY for the time you are investing.

Trying to Share Action:

drafts5://action?data=%7B%22uuid%22:%226E302BD4-B390-4560-B048-9527E00187B8%22,%22steps%22:%5B%7B%22platforms%22:3,%22data%22:%7B%22script%22:%22%5C/%5C/%20select%20from%20a%20list%20of%20drafts%20tagged%20%5C%22template%5C%22%20and%20create%20new%20draft%20based%20on%20selection.%5Cn%5Cnlet%20f%20%3D%20()%20%3D%3E%20%7B%5Cn%5Ct%5C/%5C/%20create%20temp%20workspace%20to%20query%20drafts%5Cn%5Ctlet%20workspace%20%3D%20Workspace.create();%5Cn%5Ctif%20(!workspace)%20%7B%5Cn%5Ct%5Ctreturn%20false;%5Cn%5Ct%7D%5Cn%5Ctworkspace.tagFilter%20%3D%20%5C%22template%5C%22;%5Cn%5Ctworkspace.setAllSort(%5C%22name%5C%22,%20false,%20true);%5Cn%5Ct%5C/%5C/%20get%20list%20of%20drafts%20in%20workspace%5Cn%5Ctlet%20drafts%20%3D%20workspace.query(%5C%22all%5C%22);%5Cn%5Ct%5Cn%5Ct%5C/%5C/%20check%20if%20we%20found%20any%20valid%20templates%5Cn%5Ctif%20(drafts.length%20%3D%3D%200)%20%7B%5Cn%5Ct%5Ctalert(%5C%22No%20templates%20found.%20To%20make%20templates%20available%20to%20this%20action,%20create%20a%20draft%20with%20the%20template%20content%20and%20assign%20it%20the%20tag%20%5C%5C%5C%22template%5C%5C%5C%22.%5C%22);%5Cn%5Ct%5Ctreturn%20false;%5Cn%5Ct%7D%5Cn%5Ct%5Cn%5Ct%5C/%5C/%20prompt%20to%20select%5Cn%5Ctlet%20p%20%3D%20Prompt.create();%5Cn%5Ctp.title%20%3D%20%5C%22New%20Draft%20with%20Template%5C%22;%5Cn%5Ctp.message%20%3D%20%5C%22Select%20a%20template.%20A%20new%20draft%20will%20be%20created%20based%20the%20template%20selected.%5C%22;%5Cn%5Ct%5Cn%5Ctlet%20ix%20%3D%200%5Cn%5Ctfor%20(let%20d%20of%20drafts)%20%7B%5Cn%5Ct%5Ctp.addButton(d.title,%20ix);%5Cn%5Ct%5Ctix++;%5Cn%5Ct%7D%5Cn%5Ct%5Cn%5Ctif%20(!p.show())%20%7B%5Cn%5Ct%5Ctreturn%20false;%5Cn%5Ct%7D%5Cn%5Ct%5Cn%5Ct%5C/%5C/%20get%20the%20selected%20template%20draft%5Cn%5Ctlet%20selectedIndex%20%3D%20p.buttonPressed;%5Cn%5Ctlet%20template%20%3D%20drafts%5BselectedIndex%5D;%5Cn%5Cn%5Ct%5C/%5C/%20create%20new%20draft%20and%20assign%20content%5C/tags%5C/syntax%5Cn%5Ctlet%20d%20%3D%20Draft.create();%5Cn%5Ctfor%20(let%20tag%20of%20template.tags)%20%7B%5Cn%5Ct%5Ctif%20(tag%20!%3D%20%5C%22template%5C%22)%20%7B%5Cn%5Ct%5Ct%5Ctd.addTag(tag);%5Cn%5Ct%5Ct%7D%5Cn%5Ct%7D%5Cn%5Ctd.languageGrammar%20%3D%20template.languageGrammar;%5Cn%5Ctd.content%20%3D%20d.processTemplate(template.content);%5Cn%5Ctd.update();%5Cn%5Ct%5C/%5C/%20load%20new%20draft%5Cn%5Cteditor.load(d);%5Cn%5Cteditor.activate();%5Cn%5Ct%5Cn%5Ct%5C/%5C/%20look%20for%20%3C%7C%3E%20to%20set%20cursor%20location%5Cn%5Ctlet%20loc%20%3D%20d.content.search(%5C%22%3C%7C%3E%5C%22);%5Cn%5Ctif%20(p%20!%3D%20-1)%20%7B%5Cn%5Ct%5Cteditor.setText(editor.getText().replace(%5C%22%3C%7C%3E%5C%22,%20%5C%22%5C%22));%5Cn%5Ct%5Cteditor.setSelectedRange(loc,%200);%5Cn%5Ct%7D%5Cn%5Ct%5Cn%5Ctreturn%20true;%5Cn%7D%5Cn%5Cnif%20(app.isPro)%20%7B%5Cn%5Ctif%20(!f())%20%7B%5Cn%5Ct%5Ctcontext.cancel();%5Cn%5Ct%7D%5Cn%7D%5Cnelse%20%7B%5Cn%5Ctalert(%5C%22Drafts%20Pro%20features%20required%20to%20use%20this%20action.%5C%22)%5Cn%7D%22,%22allowAsync%22:%22false%22%7D,%22type%22:%22script%22,%22isEnabled%22:true,%22uuid%22:%2278F03EDD-98DD-4AD0-A436-AC3B69BFB327%22%7D%5D,%22backingPlatforms%22:3,%22shortName%22:%22Template%22,%22shouldConfirm%22:false,%22disposition%22:3,%22keyCommand%22:%7B%22optionKey%22:false,%22input%22:%22%22,%22controlKey%22:false,%22commandKey%22:false,%22type%22:%22action%22,%22discoverabilityTitle%22:%22New%20Draft%20with%20Template%20(copy)%22,%22shiftKey%22:false%7D,%22logLevel%22:1,%22groupDisposition%22:0,%22notificationType%22:1,%22tintColor%22:%22blue%22,%22actionDescription%22:%22Prompt%20to%20select%20from%20a%20list%20of%20existing%20drafts%20with%20the%20tag%20%E2%80%9Ctemplate%E2%80%9D%20assigned,%20then%20create%20a%20new%20draft%20with%20the%20content%20of%20that%20template.%5Cn%5CnTo%20create%20templates%20available%20in%20this%20action,%20simply%20type%20your%20template%20in%20a%20draft,%20and%20assigned%20the%20tag%20%E2%80%9Ctemplate%E2%80%9D%20to%20that%20draft.%20It%20can%20be%20in%20the%20archive%20or%20inbox.%20The%20template%20can%20contain%20%5BDrafts%20tags%5D(https:%5C/%5C/reference.getdrafts.com%5C/topics%5C/templates.html)%20to%20insert%20time%20stamps%20or%20other%20dynamic%20data%20in%20the%20template.%20Also,%20any%20tags%20other%20than%20%E2%80%9Ctemplate%E2%80%9D%20assigned%20to%20the%20template%20draft%20will%20be%20assigned%20to%20the%20new%20draft%20automatically.%5Cn%5CnIf%20the%20template%20contains%20the%20string%20%60%3C%7C%3E%60,%20the%20%60%3C%7C%3E%60%20will%20be%20removed,%20and%20the%20initial%20cursor%20position%20in%20the%20new%20draft%20placed%20at%20that%20location.%5Cn%5Cn%3E%20Note:%20This%20action%20used%20functionality%20which%20requires%20a%20Pro%20subscription.%22,%22keyUseIcon%22:true,%22icon%22:%22page-add%22,%22visibility%22:480,%22backingIsSeparator%22:false,%22groupUUID%22:%2216EB0AFE-98EB-4FF1-A9A2-D27916970CFF%22,%22assignTags%22:%5B%5D,%22name%22:%22New%20Draft%20with%20Template%20(copy)%22%7D

Hope this is correct.

The screenshots do show something interesting, but it doesn’t give us an answer.

The first confirms there are no drafts that make it into the workspace, which we knew from earlier logic, but this confirms the number.

The second shows there is a tag match for your template draft shown earlier, and that it is matching to “template” as the workspace should be :face_with_raised_eyebrow:

The third one confirms is is an exact match on the tag and that there’s no punycode chicanery occurring.

The next thing I would suggest is modifying this line (in any version of the action).

workspace.tagFilter = "template";

Change the tag name to somethjng else and see if it will pick up any of your other tags.

No problem. Everyone’s pretty helpful in these parts, and you do have a rather curious issue.

The best way is to share as an unlisted action in the directory as described here.

Just a few taps to install, and small on the page :sunglasses:

Still not working. After modifying the code and removing the template tag:

This was after the first tag mentioning Templates=0. Then a second stating meeting not equal to template. I did change the code.


let f = () => {
	// create temp workspace to query drafts
	let workspace = Workspace.create();
	if (!workspace) {
	    alert("Failed to create workspace");
		return false;
	}
	workspace.tagFilter = "meeting";
	workspace.setAllSort("name", false, true);
	// get list of drafts in workspace
	let drafts = workspace.query("all");
    alert(drafts.length + " templates found");
	// check if we found any valid templates
	if (drafts.length == 0) {
	
        let adTemplate = Draft.queryByTitle("# Meeting Notes");

        adTemplate.forEach(dFound =>
        {
            alert("# Meeting Notes: TAGS = |" + dFound.tags.join("|") + "|");
            dFound.tags.forEach(strTag =>
            {
                if(strTag.toLowerCase() == "template") alert(strTag + " is equal to template");
                else alert(strTag + " is NOT EQUAL to template");
            
            });
        });


		return false;
	}
	
	// prompt to select
	let p = Prompt.create();
	p.title = "New Draft with Template";
	p.message = "Select a template. A new draft will be created based the template selected.";
	
	let ix = 0
	for (let d of drafts) {
		p.addButton(d.title, ix);
		ix++;
	}
	
	if (!p.show()) {
		return false;
	}
	
	// get the selected template draft
	let selectedIndex = p.buttonPressed;
	let template = drafts[selectedIndex];

	// create new draft and assign content/tags/syntax
	let d = Draft.create();
	for (let tag of template.tags) {
		if (tag != "template") {
			d.addTag(tag);
		}
	}
	d.languageGrammar = template.languageGrammar;
	d.content = d.processTemplate(template.content);
	d.update();
	// load new draft
	editor.load(d);
	editor.activate();
	
	// look for <|> to set cursor location
	let loc = d.content.search("<|>");
	if (p != -1) {
		editor.setText(editor.getText().replace("<|>", ""));
		editor.setSelectedRange(loc, 0);
	}
	
	return true;
}

if (app.isPro) {
	if (!f()) {
		context.cancel();
	}
}
else {
	alert("Drafts Pro features required to use this action.")
}```

Here is the link to the Action:

I modified the script further at:

                else alert(strTag + " is NOT EQUAL to meeting");```

and

```let d = Draft.create();
	for (let tag of template.tags) {
		if (tag != "meeting") {
			d.addTag(tag);

Still doesn’t recognize template with meeting tag.

Looking back, it is odd that the first debug action didn’t pick up the non-template tags on your original template draft.

You do look to be able to manually create a workspace which is based on the template tag judging from an earlier screenshot.

You mention you have tried creating other templates.

You have tried changing the action to use another tag name.

This is definitely strange behaviour. Behaviour I can’t reproduce. When faced with such things, I have to suggest, just to rule out random influence from somewhere, a device reboot. When things are strange, it can be worth trying a fresh start.

At this point I think it is worth a few minutes to give it a try.

Rebooted. No change in behavior. Will not recognize templates in the original or your DEBUG script.