Tags to Wiki-Style Cross-Links - can this be done?
Objective: Run an action that will, based on selected “tag” - gather all Drafts titles in “Wiki-Style Cross-Linking” format and add these wikilink titles to the clipboard.
Looks like this:
Invoke action (Let’s call it Tags to Wikilinks)
Pop up calls for tag
Enter desired tag
Action collects all Drafts assigned to desired tag
Converts said tagged Drafts titles to Wikilink format
Places wikilinks in clipboard, 1 per line
Example:
3 Drafts with the tag: “coffee”
Drafts with tag “coffee” are:
Starbucks cafe latte
Nespresso espresso
Learn to make a longo
Tag “coffee” is entered at the start of the action.
Output is:
[[Starbucks cafe latte]]
[[Nespresso espresso]]
[[Learn to make a longo]]
Not bad to do. Below example shows the query and construction of the result. It has a hard coded tag name, but could be extended to get that tag value from a prompt:
// tag to lookup. could be gathered from prompt...
let tag = "my-tag";
// query for drafts with that tag...
let taggedDrafts = Draft.query("", "all", [tag]);
// setup var to store titles
let titles = [];
// loop over drafts, adding each title to results
for (let d of taggedDrafts) {
titles.push(d.processTemplate("[[display_title]]"));
}
// turn titles into wiki-linked titles
titles = titles.map(t => `[[${t}]]`);
// create result string and put it in clipboard
let results = titles.join("\n");
app.setClipboard(results);
Seems after much research and attempts I’ve not been able to dynamically assign the tag via prompt as directed:
get that tag value from a prompt:
Wondering how I capture the tag value and pass it into the rest of the script?
Here’s how far I got:
// tag to lookup. could be gathered from prompt...
let tag = draft.getTemplateTag('[[prompt_text]]');
// query for drafts with that tag...
let taggedDrafts = Draft.query("", "all", [tag]);
// setup var to store titles
let titles = [];
// loop over drafts, adding each title to results
for (let d of taggedDrafts) {
titles.push(d.processTemplate("[[display_title]]"));
}
// turn titles into wiki-linked titles
titles = titles.map(t => `[[${t}]]`);
// create result string and put it in clipboard
let results = titles.join("\n");
app.setClipboard(results);
Note, no brackets in the tag name. Alternately, you can actually just evaluate the tag with the template processor, in which case you would want the tag delimiters, like:
let tag = draft.processTemplate("[[prompt_text]]")