Obsidian CreateTaskNote
ファイル名がyyMMdd hhmmのノートをデフォルトフォルダに作成
カーソル行末尾に作成したノートへのリンクを追加
タスク行
- [ ] タスク名 [[yyMMdd hhmm]]
作成されるノート
code:yyMMdd hhmm.md
yyyy-MM-dd hh:mm
スクリプト
code:js
<%*
function formatDate(date) {
const yy = String(date.getFullYear()).slice(-2); // 2桁の年を取得
const MM = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
const hh = String(date.getHours()).padStart(2, '0');
const mm = String(date.getMinutes()).padStart(2, '0');
return ${yy}${MM}${dd} ${hh}${mm};
}
function formatDate2(date) {
const yyyy = String(date.getFullYear()); // 2桁の年を取得
const MM = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
const hh = String(date.getHours()).padStart(2, '0');
const mm = String(date.getMinutes()).padStart(2, '0');
return [[${yyyy}-${MM}-${dd}]] ${hh}:${mm};
}
// 現在のカーソル行を取得
const editor = app.workspace.activeLeaf.view.editor;
const cursorLine = editor.getCursor().line;
const currentLineText = editor.getLine(cursorLine);
let updatedLineText;
let cursorPosition;
// 現在の日時を取得してフォーマット
const now = new Date();
const formattedDate = formatDate(now);
const formattedDate2 = formatDate2(now)
// 現在のファイルに関する情報を取得
const originalFolderPath = tp.file.folder(true); // 元のノートのフォルダパスを取得
const originalFileName = tp.file.title; // 元のノートのファイル名を取得
try {
// 新しいファイル名
const newFilePath = ${formattedDate};
// リンクを現在の行の末尾に挿入
const link = [[${formattedDate}]];
const updatedLineText = currentLineText.replace(currentLineText, ${currentLineText} ${link});
cursorPosition = updatedLineText.indexOf(currentLineText);
editor.setLine(cursorLine, updatedLineText);
editor.setCursor({ line: cursorLine, ch: cursorPosition });
// 新しいノートを作成
const content = ${formattedDate2} ;
await tp.file.create_new(content, newFilePath, true);
} catch (error) {
console.error("Error processing without selection:", error);
}
%>