From f7b8f3932f1acd6aac89e7b73e2cb2d57e639450 Mon Sep 17 00:00:00 2001 From: Matt Smallman Date: Sun, 26 Mar 2023 15:17:08 +0100 Subject: [PATCH] Update main.ts --- src/main.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main.ts b/src/main.ts index f1a439a..c9d887f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -57,6 +57,37 @@ export default class TasksToOmnifocus extends Plugin { } }, }); + + this.addCommand({ + id: 'extract-tasks-selection', + name: 'Extract Tasks from selection Into OmniFocus', + editorCallback: (editor: Editor, view: MarkdownView) => { + const editorText = editor.getSelection() + try { + let tasks = editorText.match(/- \[ \] .*/g); + + for (let task of tasks) { + let taskName = task.replace("- [ ] ", ""); + let taskNameEncoded = encodeURIComponent(taskName); + let noteURL = view.file.path.replace(/ /g, "%20").replace(/\//g, "%2F"); + let vaultName = app.vault.getName(); + let taskNoteEncoded = encodeURIComponent("obsidian://open?=" + vaultName + "&file=" + noteURL); + + window.open( + `omnifocus:///add?name=${taskNameEncoded}¬e=${taskNoteEncoded}` + ); + } + + if (this.settings.markComplete) { + let completedText = editorText.replace(/- \[ \]/g, "- [x]"); + editor.setValue(completedText); + } + + } catch (err) { + + } + }, + }); this.addSettingTab(new TasksToOmnifocusSettingTab(this.app, this)); }