sqeptyk

add-task-trigger

Jun 9th, 2026
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | Help | 0 0
  1. <%*
  2. // ============================================================
  3. // TASK COMPLETION TRIGGER — Add New Mapping
  4. // by sqeptyk
  5. // ============================================================
  6. //
  7. // Run this template to add a new tag → template mapping
  8. // to task-trigger-listener.md without editing it manually.
  9. //
  10. // IMPORTANT: Update LISTENER_PATH below to match where you
  11. // placed task-trigger-listener.md in your vault.
  12. // ============================================================
  13. const LISTENER_PATH = "Templates/task-trigger-listener.md";
  14. // -- PROMPTS -------------------------------------------------
  15. const tag = await tp.system.prompt("Tag (include #, e.g. #mytag):", "");
  16. if (!tag || tag.indexOf("#") !== 0) {
  17. new Notice("Task Trigger: Tag must start with #. Aborted.");
  18. return;
  19. }
  20. const template = await tp.system.prompt("Template path (no .md, e.g. Templates/MyTemplate):", "");
  21. if (!template) {
  22. new Notice("Task Trigger: Template path required. Aborted.");
  23. return;
  24. }
  25. // -- INJECT --------------------------------------------------
  26. const listenerFile = app.vault.getAbstractFileByPath(LISTENER_PATH);
  27. if (!listenerFile) {
  28. new Notice("Task Trigger: Listener not found at: " + LISTENER_PATH);
  29. return;
  30. }
  31. const content = await app.vault.read(listenerFile);
  32. if (content.indexOf('"' + tag + '"') !== -1) {
  33. new Notice("Task Trigger: Tag " + tag + " already exists. Aborted.");
  34. return;
  35. }
  36. const marker = " // Add or remove entries as needed";
  37. const insertIndex = content.indexOf(marker);
  38. if (insertIndex === -1) {
  39. new Notice("Task Trigger: Could not find insertion point. Aborted.");
  40. return;
  41. }
  42. const newEntry = '\n "' + tag + '": { template: "' + template + '" },';
  43. const insertAt = insertIndex + marker.length;
  44. const updated = content.slice(0, insertAt) + newEntry + content.slice(insertAt);
  45. await app.vault.modify(listenerFile, updated);
  46. new Notice("Task Trigger: Added " + tag + " -> " + template);
  47. const activeLeaf = app.workspace.activeLeaf;
  48. if (activeLeaf) activeLeaf.detach();
  49. %>
Tags: Obsidian
Advertisement
Add Comment
Please, Sign In to add comment