Action

Tasks to OmniFocus

Posted by Andrew Payne, Last update 2 days ago

Sends Markdown tasks formatted “- [ ]” to OmniFocus. The tasks are replaced with ‘- [OF]’

Steps

  • script

    
    const COMPLETED_PREFIX = "- [OF] "; 
    const TASKPAPER_PREFIX = "- ";         
    
    let taskpaperContent = "";
    let updatedDraftContent = "";
    let tasksFound = 0;
    
    const lines = draft.content.split("\n");
    
    for (const line of lines) {
        const trimmedLine = line.trim();
    
        if (trimmedLine.startsWith("- [ ]")) {
            // This is a task to process
            tasksFound++;
    
            // 1. Get the task text *without* the "- [ ] " prefix
            //    (slice(6) skips the 6 characters: "-", " ", "[", " ", "]", " ")
            const taskText = trimmedLine.substring(6).trim();
    
            // 2. Add to Taskpaper string for OmniFocus
            //    Formats as: "- Task name\n"
            taskpaperContent += `${TASKPAPER_PREFIX}${taskText}\n`;
    
            // 3. Add to updated draft content, marked as completed
            //    Formats as: "- [x] [OF] Task name\n"
            //    We preserve the original line's indentation by replacing
            //    only the prefix on the non-trimmed line.
            updatedDraftContent += line.replace("- [ ]", COMPLETED_PREFIX.trim()) + "\n";
    
        } else {
                  updatedDraftContent += line + "\n";
        }
    }
    
    if (tasksFound > 0) {
        const cb = CallbackURL.create();
        cb.baseURL = "omnifocus://x-callback-url/paste";
        
        cb.addParameter("content", taskpaperContent.trim());
    
        const success = cb.open();
    
        if (success) {
            draft.content = updatedDraftContent.trim();
            
            draft.update();
            const taskWord = tasksFound === 1 ? 'task' : 'tasks';
            app.displayInfoMessage(`Sent ${tasksFound} ${taskWord} to OmniFocus`);
        } else {
            app.displayErrorMessage("Failed to send to OmniFocus. Is it installed?");
        }
    
    } else {
        app.displayInfoMessage("No uncompleted tasks found");
    }
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.