Obsidian タスクを翌日に移動
code:js
<%*
// 1. カーソル行のテキストを取得
const initialCursorPos = this.app.workspace.activeEditor.editor.getCursor(); // 現在のカーソル位置を取得
const cursorLineText = this.app.workspace.activeEditor.editor.getLine(initialCursorPos.line).trim(); // カーソル行のテキストを取得
new Notice(cursorLineText);
// カーソル行のテキストが undefined または null の場合、空文字に設定
if (!cursorLineText) {
cursorLineText = ''; // デフォルトで空の文字列を設定
}
// 行頭の特定の記号とその後の時間パターンに一致する正規表現
const timePattern = /^\d{2}:\d{2}-\d{2}:\d{2}\s*/;
const symbolAndTimePattern = /^- \.*\\s*\d{2}:\d{2}-\d{2}:\d{2}\s*/;
const symbolPattern = /^- \.*\\s*/;
let newTask = cursorLineText;
// 2. カーソル行のテキストを削除
// カーソル行を削除(空の文字列に置き換え)
this.app.workspace.activeEditor.editor.setLine(initialCursorPos.line, '');
// 改行を削除
const editor = this.app.workspace.activeLeaf.view.sourceMode.cmEditor;
const cursor = editor.getCursor();
editor.replaceRange('', cursor, { line: cursor.line, ch: cursor.ch + 1 });
// 3. 現在の日付を取得し、翌日の日付を計算
let today = new Date();
today.setDate(today.getDate() + 1); // 翌日を設定
let year = today.getFullYear();
let month = ("0" + (today.getMonth() + 1)).slice(-2); // 月を2桁に
let day = ("0" + today.getDate()).slice(-2); // 日を2桁に
// 4. ノートのファイル名(YYYY-MM-DD形式)を作成
let nextDayFileName = ${year}-${month}-${day}.md;
let dailyNotesFolder = 'notes'; // 必要に応じてフォルダを変更
let nextDayFilePath = ${dailyNotesFolder}/${nextDayFileName}; // ノートのパスを指定
// フォルダが存在しない場合は作成
if (!(await app.vault.adapter.exists(dailyNotesFolder))) {
await app.vault.createFolder(dailyNotesFolder);
}
// 5. TFileオブジェクトを取得し、翌日のノートにテキストを追記
let nextDayFile = app.vault.getAbstractFileByPath(nextDayFilePath);
if (nextDayFile) {
// ファイルが存在する場合、テキストを追記
await app.vault.append(nextDayFile, newTask + '\n');
new Notice(${newTask}を翌日にコピーしました。);
} else {
// ファイルが存在しない場合は新規作成
nextDayFile = await app.vault.create(nextDayFilePath, newTask + '\n');
new Notice(新しいノートが作成され、${newTask}を追加しました。);
}
%>