Action

Send to Things

Posted by Macks, Last update 11 months ago - Unlisted

Send project outlines to Things using Markdown syntax (requires Things 3.4 and higher).

Tasks added without a project will go to the Inbox, and the app will direct itself to the Inbox, without a callback for the Drafts app.

Project

Project notes

Heading

Todo
Todo notes
- Checklist item

Steps

  • script

    /*
      Send each line of a draft to Things as a todo
    */
    
    const getTargetItem = state => state.todo || state.project
    
    const parsers = [
    	{
    		// Project
    		pattern: /^# /g,
    		transform(state, input) {
    			const project = TJSProject.create()
    			project.title = input
    
    			if (state.list) {
    				project.area = state.list
    			}
    
    			state.items.push(project)
    			state.project = project
    			// state.list = null
    			state.todo = null
    		}
    	},
    	{
    		// Heading
    		pattern: /^## /g,
    		transform(state, input) {
    			const heading = TJSHeading.create()
    			heading.title = input
    
    			if (state.project) {
    				state.project.addHeading(heading)
    			}
    		}
    	},
    	{
    		// Checklist item
    		pattern: /^- /g,
    		transform(state, input) {
    			const checklistItem = TJSChecklistItem.create()
    			checklistItem.title = input
    
    			if (state.todo) {
    				state.todo.addChecklistItem(checklistItem)
    			}
    		}
    	},
    	{
    		// Note
    		pattern: /^> ?/g,
    		transform(state, input) {
    			const targetItem = getTargetItem(state)
    
    			if (targetItem) {
    				if (!targetItem.notes) {
    					targetItem.notes = input
    				} else {
    					targetItem.notes += `\n${input}`
    				}
    			}
    		}
    	},
    	{
    		// Tag
    		pattern: /^#/g,
    		transform(state, input) {
    			const targetItem = getTargetItem(state)
    			console.log(input)
    
    			if (targetItem) {
    				if (Array.isArray(targetItem.tags)) {
    					targetItem.tags = [...targetItem.tags, input]
    				} else {
    					targetItem.tags = [input]
    				}
    			}
    		}
    	},
    	{
    		// List
    		pattern: /^@ ?/g,
    		transform(state, input) {
    			state.list = input
    		}
    	},
    	{
    		// When
    		pattern: /^: ?/g,
    		transform(state, input) {
    			const targetItem = getTargetItem(state)
    
    			if (targetItem) {
    				targetItem.when = input
    			}
    		}
    	},
    	{
    		// Deadline
    		pattern: /^! ?/g,
    		transform(state, input) {
    			const targetItem = getTargetItem(state)
    
    			if (targetItem) {
    				targetItem.deadline = input
    			}
    		}
    	},
    	{
    		// Comments
    		pattern: /^\/\//g,
    		transform(state, input) {
    			// These are comments, so this transform does nothing
    		}
    	},
    	{
    		// Todo (matches everything else)
    		pattern: /^/g,
    		transform(state, input) {
    			const todo = TJSTodo.create()
    			todo.title = input
    
    			if (state.list) {
    				todo.list = state.list
    			}
    
    			if (state.project) {
    				state.project.addTodo(todo)
    			} else {
    				state.items.push(todo)
    			}
    
    			state.todo = todo
    		}
    	}
    ]
    
    const lines = draft.content.split("\n").filter(line => line.length)
    
    const state = lines.reduce((state, line) => {
    	for (var {pattern, transform} of parsers) {
    		if (pattern.test(line)) {
    			const input = line.replace(pattern, "")
    			transform(state, input)
    			break
    		}
    	}
    	
    	return state
    },
    {
      project: null,
      todo: null,
      list: null,
      items: []
    })
    
    console.log(JSON.stringify(state.items))
    
    const {url} = TJSContainer.create(state.items)
    
    // Directly open the Things URL
    let success = app.openURL(url)
    if (success) {
        console.log("Todos created in Things")
    } else {
        context.fail()
    }

Options

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