[Obs#51] QuickAdd全攻略(2):脚本撰写与宏使用要点

tp.web.random_picture

1. Capture 范例1

Capture: Admonition的两个版本

1.1. Templater API

<%* let selection = window.getSelection(); let choice = await tp.system.suggester(   [ "✏️ note", " abstract", "ℹ️ info", " tip", "✅ success",    "❓ question", "⚠️ warning", "❌ fail", " bug", " example",    "✍️ quote", " comment", " LOL" ],    [ 0,1,2,3,4,5,6,7,8,9,10,11,12 ]   ); const admonitions = [    ["ad-note", "重点"], ["ad-abstract", "摘要"], ["ad-info", "资讯"], ["ad-tip", "技巧"], ["ad-success", "完成"],    ["ad-question", "问题"], ["ad-warning", "警告"], ["ad-fail", "失败"], ["ad-error", "错误"], ["ad-example", "范例"],    ["ad-quote", "引用"], ["ad-comment", "建议"], ["ad-LOL", "好笑"] ];  admonition = admonitions[choice][0]; title = admonitions[choice][1];  const nl = String.fromCharCode(10);  choice ="```" + admonition + nl +   //"collapse: on" + nl +   "title: " + title + nl + selection + nl +   "```"; //tR += choice; %> <% choice %> 

1.2. QuicAdd API

‘‘‘js quickadd //const selection = window.getSelection(); const selection = this.quickAddApi.utility.getSelectedText(); const aTexts =   [ "✏️ note", " abstract", "ℹ️ info", " tip", "✅ success",    "❓ question", "⚠️ warning", "❌ fail", " bug", " example",   "✍️ quote", " comment", " LOL" ]; const aValues = [ "0", "1", "2" , "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" ]; let choice = await this.quickAddApi.suggester(aTexts, aValues); //console.log("choice", choice);  const admonitions = [    ["ad-note", "重点"], ["ad-abstract", "摘要"], ["ad-info", "资讯"], ["ad-tip", "技巧"], ["ad-success", "完成"],    ["ad-question", "问题"], ["ad-warning", "警告"], ["ad-fail", "失败"], ["ad-error", "错误"], ["ad-example", "范例"],    ["ad-quote", "引用"], ["ad-comment", "建议"], ["ad-LOL", "好笑"] ];  admonition = admonitions[choice][0]; title = admonitions[choice][1];  // 在脚本里,换行符号和倒引号不要在字串里使用,改用String.fromCharCode()才不会出现解析错误 const nl = String.fromCharCode(10); const backQuotes = String.fromCharCode(96) + String.fromCharCode(96) + String.fromCharCode(96);  result = backQuotes + admonition + nl +   "title: " + title + nl + selection + nl + backQuotes;  return result; ‘‘‘ 

2. Capture 范例2

输入关键字以插入动态图片。

<%* let keywords = await tp.system.prompt("随机图片:输入关键字(以 , 分隔)"); keywords = keywords.replace(/ /g, ""); //console.log("keywords", keywords); %> <% tp.web.random_picture("1600x900", keywords) %> 

3. Capture 范例3

将编码后的网址转换回正常网址。

 ‘‘‘js quickadd selObj = window.getSelection(); text = selObj.toString(); //await this.quickAddApi.utility.getClipboard(); text = await decodeURIComponent(text) this.quickAddApi.utility.setClipboard(text); //console.log("main " + text); return text; ‘‘‘ 

4. Template 范例

以每日笔记为范例(使用Templater、Tasks外挂),介绍如何输出条件式内容。

  • https://gist.github.com/emisjerry/95809a56c3d5517a38d887581a2214c9

5. 宏

宏操作由三个步骤组成:

  1. 撰写脚本 js
  2. 宏定义
  3. 宏使用

5.1. 撰写脚本 js

在档案总管里储存库任何资料夹新增脚本档,如:my_script.js

async function notice1(params) {   //({ quickAddApi } = params);   //const quickAddApi = params.quickAddApi;   const text = await params.quickAddApi.inputPrompt("随意输入文字...");   new Notice(text, 5000);   return text; }  async function notice2(params) {   //({ quickAddApi } = params);   //const quickAddApi = params.quickAddApi;   const text = await params.quickAddApi.inputPrompt("随意输入文字2...");   new Notice(text, 5000);   return text; }  module.exports = { notice1,notice2 };  

5.2. 宏定义

  1. 【设定】→【外挂选项】→【QuickAdd】→【Manage Macros】
  2. 输入宏名称(如“macro_notice”)后点击〔Add macro〕
  3. 点击新增宏的【Configure】
    1. 在【User Scripts】选用要使用的脚本后按〔Add〕
    2. 要被执行的命令会添加在上方

5.3. 宏使用

  1. 【设定】→【外挂选项】→【QuickAdd】
  2. 在QuicAdd Settings视窗里输入名称(如 “Macro: notice”),选用【Macro】后按〔Add Choice〕
  3. 在新添加的宏上点击右侧的【Configure】
  4. 选择要使用的宏(即 macro_notice)

6. Capture 使用宏

在Capture format字段输入如下内容,会弹出视窗以选择要用那个函数:

{{MACRO:macro_notice}} 

直接使用指定的函数,此写法似乎会影响Templater的动态命令:

{{MACRO:macro_notice::notice2}} 

7. 范例档连结

8. 相关连结

9. 教学影片

##


想在手机阅读更多教学锦囊资讯?下载【香港硅谷】Android应用
分享到Facebook
技术平台: Nasthon Systems