TaskWizard
TaskWizard v2
2025/02/28
環境: Obsidian > Templater
動作の流れ:
1. スクリプトを実行すると、検索キーワード入力ボックス が表示される
2. ユーザーがキーワードを入力すると、過去 30日分のデイリーノート を検索
3. キーワードを含む行をリストアップ(直近のタスクほど上に表示)
4. ユーザーが選択した行を 現在のカーソル位置に挿入し、末尾にカーソルを移動
History
2025/02/28 直近のタスクが上に表示されるように変更
2025/03/07 入力タスクの後に空行が入る問題を解消
TaskWizard.md
code:js
<%*
const maxDaysToSearch = 30; // 最大検索日数
const maxTasksToFind = 100; // 最大取得件数
// ユーザーに検索キーワードを入力させる
const keywordsInput = await tp.system.prompt("検索キーワードを入力(スペース区切りでAND検索)");
if (!keywordsInput) {
new Notice("検索キーワードが入力されませんでした。");
return;
}
const keywords = keywordsInput.split(/\s+/);
// 現在のカーソル位置を取得
const cursorPos = app.workspace.activeEditor.editor.getCursor();
const currentFile = app.workspace.getActiveFile();
if (!currentFile) {
new Notice("ファイルが開かれていません。");
return;
}
// 過去の日付を取得
let foundItems = [];
let checkedDates = new Set(); // 取得済みの日付を記録する
let today = new Date();
for (let i = 0; i < maxDaysToSearch && foundItems.length < maxTasksToFind; i++) {
// today から i 日前の日付を取得
let searchDate = new Date(today);
searchDate.setDate(today.getDate() - i); // i 日前を計算
let dateString = searchDate.toLocaleDateString('sv-SE'); // sv-SEロケールはYYYY-MM-DD形式の日付文字列を戻す
if (!checkedDates.has(dateString)) { // すでに検索済みの日付はスキップ
checkedDates.add(dateString);
const dailyNote = await tp.file.find_tfile(${dateString}.md);
if (dailyNote) {
const noteContent = await app.vault.read(dailyNote);
const lines = noteContent.split("\n").reverse(); // 直近のデータほど上に表示されるようにする
for (const line of lines) {
if (keywords.every(k => line.toLowerCase().includes(k.toLowerCase()))) {
foundItems.push(line.replace(/^(-? ?\d{2}:\d{2}(-\d{2}:\d{2})? )/, "").trim()); // 時刻部分削除
if (foundItems.length >= maxTasksToFind) break;
}
}
}
}
}
// 検索結果のリストを表示
if (foundItems.length === 0) {
new Notice("該当するタスクやメモが見つかりませんでした。");
} else {
const selectedItems = await tp.system.suggester(foundItems, foundItems);
if (selectedItems) {
// カーソル位置に挿入
const editor = app.workspace.activeEditor.editor;
editor.replaceRange(selectedItems, cursorPos);
// カーソルを挿入した行の末尾に移動
const newCursorPos = {
line: cursorPos.line,
ch: cursorPos.ch + selectedItems.length
};
editor.setCursor(newCursorPos);
new Notice("選択したタスク・メモを挿入しました。");
}
}
%>