skip to Main Content

I am creating a user script for use in a QuickAdd plugin Macro in Obsidian. The Macro has a single step and simply runs the user script (JavaScript that uses the CommonJS standard). One of the steps in the user script is to create a new note in Obsidian with a Templater template. The problem I’m having is the script cannot access the templater.current_functions_object unless I manually create a new note with a Templater template first. I am guessing it gets loaded into memory after it is manually executed one time. I would like to run my Macro and have it automatically create the new note with a Templater template without the need for the manual step if possible.

Q: Is there a way to make sure the templater.current_functions_object is avaiable in the user script without a need to manually create a new note with a Templater template first?

I created a simple test script below to help figure this out.

When I run a test Macro with the following user script, I get ‘Unable to access the tp current functions object.’ with a fresh launch of Obsidian. If I manually create a new note in Obsidian with a Templater template, run the test Macro again, then it works and I get ‘Found the tp current functions object!’. I’m attempting to run this without requiring the manual step.

async function testTemplaterPlugin(params) {
  const tp = params.app.plugins.plugins['templater-obsidian'].templater.current_functions_object;

  if(tp) {
    console.log("Found the tp current functions object!");
  } else {
    console.log("Unable to access the tp current functions object.");
  }
}

module.exports = testTemplaterPlugin;

Any help would be very much appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Not sure why, but within a QuickAdd user script in Obsidian the Templater 'current_functions_object' will remain undefined until Templater is used at least once. I did find a workaround in this forum that takes care of my issue and removes the manual step.

    To fix, simply add a startup template in Templater settings (the startup script won't output anything), relaunch Obsidian and now the 'current_functions_object' should always be defined and the script works.

    enter image description here

    I am going to pick this as the solution since it fixed my issue.


  2. Initialization Script: If you're using Templater with a software that allows initialization scripts (such as OBS Studio), you can include the necessary code to initialize Templater and ensure that templater.current_functions_object is available when your user script runs. This way, the Templater functions object will be accessible without requiring manual intervention.
    
    Check for Templater Initialization: In your user script, you can include a check to ensure that Templater is initialized before accessing templater.current_functions_object. If Templater is not initialized, you can programmatically initialize it within your script before using any Templater functions.
    
    Include Templater Initialization in Your Workflow: If your workflow involves using Templater frequently, you can include the step of initializing Templater at the beginning of your process. This can be done manually or through automation, depending on your specific workflow requirements.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search