[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