用Templater Hotkeys來建立Obsidian自動化腳本,比QuickAdd方便許多。步驟如下:
- 安裝並啟用Templater外掛
- 新增Templater腳本的.md檔
- 到Templater選項裡新增Templater Hotkeys,讓腳本檔變成命令
- 為腳本命令設置快捷鍵或用Ctrl/Cmd+P執行命令
本次介紹4個範例,並說明如何使用Dataviewjs查詢命令代碼。
[!tip]+ 建議
- 所有腳本檔皆以
Cmd-
開頭,與一般的模板檔區隔- 腳本檔執行發生錯誤時,將之複製成.js檔,刪除開頭的
<%*
與結尾的%>
後,用VS Code檢視
1. 範例1-選取YAML欄位設定
- 功用:在YAML區插入筆記種類
<%*
let list = {
"Fleeting Note": "fleeting note",
"Literature Note": "literature note" ,
"Permanent Note": "permanent note",
"Structure Note": "structure note",
"Meeting Note": "meeting note",
"Daily Note": "daily note"
};
let keys = Object.keys(list);
let key = await tp.system.suggester(keys, keys);
if (!key) return;
let editor = app.workspace.activeLeaf.view.editor; // 取編輯器物件
let position = editor.getCursor(); // 取游標物件
let text = editor.getLine(position.line); // 取游標行的內容
if (text.length > 0) text += "\n";
text = text + "type: " + list[key];
editor.setLine(position.line, text);
-%>
2. 範例2-由選單插入Callout區塊
- 功用:用選單插入Callout區塊
<%*
let list = {
"ℹ info" : "info,資訊",
"✏ note" : "note,筆記",
"📒 summary" : "summary,彙總",
"🔥 tip" : "tip,技巧",
"☑ check" : "check,查核",
"❔Help" : "help,說明",
"⚠ Warning" : "warning,警告",
"❌ Fail" : "fail,失敗",
"⚡Danger" : "danger,危險",
"🪲 Bug" : "bug,錯誤",
"📋 Example" : "example,範例",
"✍ Quote " : "quote,引用",
"😝 LOL " : "LOL,哈哈",
"📕 Reference " : "REF,參考"
};
let keys = Object.keys(list);
key = await tp.system.suggester(keys, keys);
let value = list[key];
let index = value.indexOf(",");
let text = value.substring(index+1);
value = value.substring(0, index);
if (key) return ">[!" + value + "]+ " + text + "\n> ";
%>
3. 範例3-段落形成畫布卡片
- 功用:將文字段落產生成畫布的卡片
<%*
let sCanvasFolder = "030-Inbox/";
//d = tp.file.content.split("\n\n");
let sSelection = tp.file.selection();
let aLines = sSelection.split("\n\n");
aNodes = [];
let iWidth = 320, iHeight = 160;
for (i = 0;i < aLines.length;i++){
a = {};
a.id = i;
a.x = (i%3) * (iWidth+20);
a.y = (parseInt(i / 3) % 3) * (iHeight+20);
a.width = iWidth;
a.height = iHeight;
a.type = "text";
a.text = aLines[i];
if (a.text.length > 0) {
aNodes.push(JSON.stringify(a));
}
}
let sJson = '{"nodes":[' + aNodes.join(",") + ']}';
let sBasename = sCanvasFolder + tp.file.title;
let sFullFilename = sBasename + ".canvas";
let sPath = app.vault.getAbstractFileByPath(sFullFilename);
// 檔名可能重覆,加上序號處理
let k = 1;
while(sPath){
sFullFilename = sBasename + " " + (k++) + ".canvas";
sPath = app.vault.getAbstractFileByPath(sFullFilename);
}
sPath = await app.vault.create(sFullFilename, sJson);
await app.workspace.activeLeaf.openFile(sPath);
return null; // 不回傳null或sSelection的話,原來的選取內容會被刪除
-%>
4. 用Dataviewjs列出所有命令代碼
- 用Dataviewjs列出所有命令以找到需要使用的命令代碼
```dataviewjs
const getNestedObject = (nestedObj, pathArr) => {
return pathArr.reduce((obj, key) =>
(obj && obj[key] !== 'undefined') ? obj[key] : undefined, nestedObj);
}
function getHotkey(arr) {
return arr.hotkeys ? [[getNestedObject(arr.hotkeys, [0, 'modifiers'])],
[getNestedObject(arr.hotkeys, [0, 'key'])]].flat(2).join('+').replace('Mod', 'Ctrl') : '–';
}
let cmds = dv.array(Object.entries(app.commands.commands))
.sort(v => v[1].id, 'asc');
dv.paragraph(cmds.length + " commands currently enabled.<br><br>");
dv.table(["Command ID", "Name in current locale", "Hotkeys"],
cmds.map(v => [
v[1].id,
v[1].name,
getHotkey(v[1]),
])
);
## 5. Templater Hotkeys命令選單
```js file:"Cmd-All.md"
<%*
let lstCmds = {
"YAML Type": "templater-obsidian:010-Templates/Cmd-YAML type.md",
"Week Review": "templater-obsidian:010-Templates/Cmd-Review.md" ,
"Callout": "templater-obsidian:010-Templates/Cmd-Callout.md",
"Prepend Line": "templater-obsidian:010-Templates/Cmd-Prepend Line.md",
"Text to Canvas": "templater-obsidian:010-Templates/Cmd-Insert into canvas.md"
};
let keys = Object.keys(lstCmds);
let key = await tp.system.suggester(keys, keys);
if (!key) return;
let editor = app.workspace.activeLeaf.view.editor;
let position = editor.getCursor();
let text = editor.getLine(position.line);
app.commands.executeCommandById(lstCmds[key]);
-%>
6. 教學影片
##
您可能也會有興趣的類似文章
- [Obs#56] 快速新增靈感/閃念筆記(Fleeting Note)的3種方法 (0則留言, 2021/10/10)
- Obs131|Obsidian使用Dataviewjs動態查詢的嘗試 (0則留言, 2023/05/27)
- [OBS#32] templater: 無限擴充可能的第三方樣板外掛 (0則留言, 2021/04/10)
- Obs124|跳,跳,跳乎伊勇!Obsidian用書籤和標籤快速在檔案間跳轉 (0則留言, 2023/04/23)
- Obs130|用Dataviewjs將Dataview表格產生成Markdown格式,並複製到剪貼簿的技巧 (0則留言, 2023/05/20)
- Obs128|Obsidian Dataview進度條與YAML欄位快速輸入的方法 (0則留言, 2023/05/11)
- [OBS#20] templater: 無限擴充可能的第三方樣板外掛 (1則留言, 2020/11/26)
- [Obs#51] QuickAdd全攻略(2):腳本撰寫與巨集使用要點 (0則留言, 2021/09/18)
- Obs126|Obsidian 2023/04 7個新外掛介紹與評析 (0則留言, 2023/04/29)
- Obsidian (黑曜石)筆記軟體的基本操作指引 (0則留言, 2020/06/23)
- [Obs#105] Obsidian外掛 Local REST API 提供外部整合服務 (0則留言, 2022/11/06)
- Obsidian 1.0新手教學—打造個人知識管理利器,專屬的第二大腦 (2022年) (4則留言, 2022/10/22)
- [Obs#53] Text Transporter 文字運輸車:透過書籤在不同筆記之間搬運文字 (0則留言, 2021/09/25)
- [Obs#107] Obsidian全方位搜尋:OmniSearch外掛 (0則留言, 2022/11/20)
- Obs123-掌握Obsidian的神秘技巧:解密鮮為人知的操作方法 (0則留言, 2023/04/15)