Action

Process tasks

Last update about 3 years ago

This action lets you place references to tasks amid your regular notes and automatically extracts them into Things.

Steps

  • script

    const PROCESSED_TASKS_HEADER = '# Processed Tasks & Reminders'
    const REGEX = /^-* *(t|T|remind):.*/gm
    const REGEX_CONTENT_ONLY = /^-* *(t|T|remind):/gm
    const TASKS_REGEX = /^-* *(t|T):.*/gm
    const hasProcessedHeader = (d) =>
      Boolean(d.content.match(PROCESSED_TASKS_HEADER))
    const createProcessedHeader = (d) => {
      d.content = `${d.content}\n\n${PROCESSED_TASKS_HEADER}`
    }
    const parseType = (str) => {
      switch (true) {
        case Boolean(str.match(TASKS_REGEX)): return 't'
        default: return 'unknown'
      }
    }
    const parseContent = str => {
      const res = (str.split(REGEX_CONTENT_ONLY) || [])
        .map(x => x.trim())
        .filter(Boolean)
    
      return res[1] || []
    }
    
    const matchedLines = draft.content.match(REGEX) || []
    const parsedLines = matchedLines.reduce((acc, line) => {
      const type = parseType(line)
      const content = parseContent(line)
      const update = [...(acc[type] || []), content]
      
      return { ...acc, [type]: update }
    }, {})
    
    // Process tasks
    const tasks = (parsedLines['t'] || []).map(task => {
      const todo = TJSTodo.create()
      todo.title = task
      return todo
    })
    const tjscontainer = TJSContainer.create(tasks)
    const thingsCB = CallbackURL.create()
    thingsCB.baseURL = tjscontainer.url
    const thingsSuccess = thingsCB.open()
    
    if (thingsSuccess) {
      if (!hasProcessedHeader(draft)) {
        createProcessedHeader(draft)
        draft.update()
      }
    
      const filteredContent = draft.content
        .split('\n')
        .filter(line => !line.match(TASKS_REGEX))
      const newContent = [
        ...filteredContent,
        ...tasks.map(t => `- ${t.title}`),
      ].join('\n')
    
      draft.content = newContent
      draft.update()
    } else {
      context.fail()
    }
    
    // Process reminders
    

Options

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