欧博官网[Obs#51] QuickAdd全攻略(2):脚本撰写与宏使用要点
先复习QuickAdd的Capture、Template脚本的使用与撰写,欧博官网再说明宏操作的3个步骤。
1. Capture 范例1Capture: 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 APIjs 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插件),介绍如何输出条件式内容。
5. 宏宏操作由三个步骤组成:
撰写脚本 js
宏定义
宏使用
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. 宏定义【设置】→【插件选项】→【QuickAdd】→【Manage Macros】
输入宏名称(如「macro_notice」)后点击〔Add macro〕
点击添加宏的【Configure】
在【User Scripts】选用要使用的脚本后按〔Add〕
要被运行的命令会添加在上方
5.3. 宏使用【设置】→【插件选项】→【QuickAdd】
在QuicAdd Settings窗口里输入名称(如 「Macro: notice」),选用【Macro】后按〔Add Choice〕
在新添加的宏上点击右侧的【Configure】
选择要使用的宏(即 macro_notice)
6. Capture 使用宏在Capture format字段输入如下内容,会弹出窗口以选择要用那个函数:
{{MACRO:macro_notice}}直接使用指定的函数,此写法似乎会影响Templater的动态命令:
{{MACRO:macro_notice::notice2}}7. 范例档链接📝 [通用笔记样板 template-quickadd.md](https://gist.github.com/emisjerry/ba75ee14716df47bfb65b22336a292c8)
📝 [每日笔记样板 (使用Tasks插件) template-quickadd-daily.md]( https://gist.github.com/emisjerry/95809a56c3d5517a38d887581a2214c9)
📝 [Capture-format: Admonition:](https://gist.github.com/emisjerry/1b1e13acaee6d65162d3c5fe76925b94)
📝 [QuickAdd inline JavaScript test:](https://gist.github.com/emisjerry/7d4097da9cd20c73e953da1582ea1c53)
📝 [QuickAdd Decode URL]( https://gist.github.com/emisjerry/155b85175f2a24994a9f05b6d2f455c3 )
📝 [QuickAdd Get Random Picture](https://gist.github.com/emisjerry/354671b8d4284a5b0d7ede2414c64d94)
致谢:部份脚本系取材自对岸的BORDER网友的分享。❤️