在 Obsidian QuickAdd 插件用户脚本中正确访问 Templater 对象

问题描述 投票:0回答:1

我正在创建一个用户脚本,用于 Obsidian 中的 QuickAdd 插件宏。该宏只有一个步骤,只需运行用户脚本(使用 CommonJS 标准的 JavaScript)。用户脚本中的步骤之一是使用 Templater 模板 在 Obsidian 中创建新注释。我遇到的问题是脚本无法访问 templater.current_functions_object ,除非我首先使用 Templater 模板手动创建新注释。我猜它在手动执行一次后就会加载到内存中。我想运行我的宏并让它使用 Templater 模板自动创建新笔记,如果可能的话,无需手动步骤。

问: 有没有办法确保 templater.current_functions_object 在用户脚本中可用,而无需先使用 Templater 模板手动创建新注释?

我在下面创建了一个简单的测试脚本来帮助解决这个问题。

当我使用以下用户脚本运行测试宏时,我收到“无法访问 tp 当前函数对象”。全新推出黑曜石。如果我使用 Templater 模板在 Obsidian 中手动创建一个新注释,再次运行测试宏,然后它就可以工作,并且我得到“找到 tp 当前函数对象!”。我正在尝试运行它而不需要手动步骤。

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;

任何帮助将非常感激。

javascript node.js obsidian
1个回答
0
投票
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.
© www.soinside.com 2019 - 2024. All rights reserved.