How to run script after template has been created?

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

The code works fine except for the problem of in order to move the cursor to the point where I want it the template has to already exist.

This code creates an embedded section before the frontmatter.

My process is from the Plan of the Week I click the link for the day and then I apply the daily template for that grade. I teach 6 - 12 so I have a G6, G7, G8 … etc daily template. There is an attendance section with student names that makes it necessary to have so many templates. I think that a script that creates all templates from scratch would get me past this but I’m not at that level.

The problem I’m trying to solve is how do I get this code or what is a solution to place an embed string in the Plan of the Week section of my Grade Daily file. This code places it before the frontmatter and doesn’t move it after the file creation.

I’m simply trying to get this to put this line in my file after this header

Plan of the Day

![[G6 Lesson Plans for the week of 2023-04-10#Monday]]

Automatically so I don’t have to cut and paste it.

Things I have tried


tags: [ My_School 2022_2023 6th_Grade LP8 ]
Title: <% tp.file.title %>
Type: Daily
Date: <% tp.file.creation_date() %>
modification date: <% tp.file.last_modified_date(“dddd Do MMMM YYYY HH:mm:ss”) %>
<% await tp.file.move(“My School/6th Grade/G6 Lesson Plans/G6 Daily/G6 D LP8/” + tp.file.title ) %>

<% tp.file.title %>

Date: <% tp.file.creation_date() %>

[[Agenda Daily]] - [[Agenda Weekly]]

Chapter Synopsis

Plan of the Day

<%*
// Define the input string to search
const inputString = tp.file.title;
await tp.file.cursor(20)

// Define a regular expression pattern to match dates in the format "YYYY-MM-DD" 
const datePattern = /\d{4}-\d{2}-\d{2}/; 

// Define a list of day of week names 
const dayOfWeekNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"]; 

// Use the regular expression to search for a date in the string 
const match = inputString.match(datePattern);

  if (match) { 
  // Extract the matched date 
  const date = new Date(match[0]); 

  // Calculate the Monday of the week containing the date 
  const dayOfWeekIndex = date.getDay(); 
  const daysUntilMonday = dayOfWeekIndex === 0 ? 6 : dayOfWeekIndex - 0; const mondayDate = new Date(date); mondayDate.setDate(date.getDate() - daysUntilMonday); 

  // Format the Monday date as "YYYY-MM-DD" 
  const searchDate = mondayDate.toISOString().slice(0, 10); 

  // Construct the message 
  const message = ![[G6 Lesson Plans for the week of ${searchDate}#${dayOfWeekNames[dayOfWeekIndex]}]];

tp.file.cursor(20)
await tp.file.cursor_append(message)

//tp.file.cursor(10)
tp.file.cursor_append("   "+   tp.file.title + "   ")

}
%>

Running it this way places the embed before the frontmatter but writes the embed perfectly.

I’ve tried splitting the code into two files and inserting the code as a template and that doesn’t work either. I don’t know how to write functions in templater or designate this code to run after the instance of the file has been created and the template posted. I believe ultimately that is what I have to do is to run this after the template instance has been created.

I don’t have an answer to your question, but you may want to read the tips for formatting code to make your post easier to read.


tags: [ My_School 2022_2023 6th_Grade LP8 ]
Title: <% tp.file.title %>
Type: Daily
Date: <% tp.file.creation_date() %>
modification date: <% tp.file.last_modified_date(“dddd Do MMMM YYYY HH:mm:ss”) %>
<% await tp.file.move(“My School/6th Grade/G6 Lesson Plans/G6 Daily/G6 D LP8/” + tp.file.title ) %>

<% tp.file.title %>

Date: <% tp.file.creation_date() %>

[[Agenda Daily]] - [[Agenda Weekly]]

Chapter Synopsis

Plan of the Day

<%*
// Define the input string to search 
    const inputString = tp.file.title; 
    await tp.file.cursor(20)

    // Define a regular expression pattern to match dates in the format "YYYY-MM-DD" 
    const datePattern = /\d{4}-\d{2}-\d{2}/; 

    // Define a list of day of week names 
    const dayOfWeekNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"]; 

    // Use the regular expression to search for a date in the string 
    const match = inputString.match(datePattern);

      if (match) { 
      // Extract the matched date 
      const date = new Date(match[0]); 

      // Calculate the Monday of the week containing the date 
      const dayOfWeekIndex = date.getDay(); 
      const daysUntilMonday = dayOfWeekIndex === 0 ? 6 : dayOfWeekIndex - 0; const mondayDate = new Date(date); mondayDate.setDate(date.getDate() - daysUntilMonday); 

      // Format the Monday date as "YYYY-MM-DD" 
      const searchDate = mondayDate.toISOString().slice(0, 10); 

      // Construct the message 
      const message = ![[G6 Lesson Plans for the week of ${searchDate}#${dayOfWeekNames[dayOfWeekIndex]}]];

    tp.file.cursor(20)
    await tp.file.cursor_append(message)

    //tp.file.cursor(10)
    tp.file.cursor_append("   "+   tp.file.title + "   ")

}
%>

Thank you I hope this helps people trying to help me.

<%*
const fs = require("fs");
const path = require("path");

// Define the input string to search
const inputString = tp.file.title;

// Define a regular expression pattern to match dates in the format "YYYY-MM-DD"
const datePattern = /\d{4}-\d{2}-\d{2}/;

// Define a list of day of week names
const dayOfWeekNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

// Use the regular expression to search for a date in the string
const match = inputString.match(datePattern);

if (match) {
  // Extract the matched date
  const date = new Date(match[0]);

  // Calculate the Monday of the week containing the date
  const dayOfWeekIndex = date.getDay();
  const daysUntilMonday = dayOfWeekIndex === 0 ? 6 : dayOfWeekIndex - 0;
  const mondayDate = new Date(date);
  mondayDate.setDate(date.getDate() - daysUntilMonday);

  // Format the Monday date as "YYYY-MM-DD"
  const searchDate = mondayDate.toISOString().slice(0, 10);

  // Construct the message
  const message = `![[G6 Lesson Plans for the week of ${searchDate}#${dayOfWeekNames[dayOfWeekIndex]}]]`;

  const filePath = tp.file.path();
  tp.file.close();
  const fileContents = fs.readFileSync(filePath, "utf-8");
  const splitIndex = fileContents.indexOf("## Plan of the day") + 1;
  const newFileContents = fileContents.slice(0, splitIndex) + "\n" + message + "\n" + fileContents.slice(splitIndex);
  fs.writeFileSync(filePath, newFileContents);
}
%>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.