Skip to content

Instantly share code, notes, and snippets.

@brandonpittman
Last active May 1, 2019 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonpittman/aefa37b56a35f4affc3148585247fc5f to your computer and use it in GitHub Desktop.
Save brandonpittman/aefa37b56a35f4affc3148585247fc5f to your computer and use it in GitHub Desktop.
Things OpenAndComplete written in JS (v2)
function run(argv) {
var app = Application('Things3')
var currentApp = Application.currentApplication()
currentApp.includeStandardAdditions = true
var selected = app.selectedToDos()
if (selected.length !== 0) {
selected.forEach(todo => {
todo.completionDate = new Date()
if (todo.notes().length > 0) {
let notes = todo.notes().split('\n')
notes.forEach(line => {
handleLine(line)
})
} else {
currentApp.displayNotification(`"${todo.name()}" doesn't have any notes.`)
}
})
} else {
currentApp.displayNotification(`You haven't selected any tasks.`)
}
}
function trimLine(line, key) { return line.slice(key.length).trim() }
function sanitizeLine(line) { return line.replace(/'/g, "") }
function handleLine(line) {
var app = Application('Things3')
var currentApp = Application.currentApplication()
currentApp.includeStandardAdditions = true
var line = sanitizeLine(line)
if (line.includes('://')) {
currentApp.doShellScript(`open '${line}'`)
} else if (line.startsWith('scpt:')) {
var script = trimLine(line, 'scpt:')
currentApp.doShellScript(`osascript '${script}'`)
} else if (line.startsWith('sh:')) {
var script = trimLine(line, 'sh:')
currentApp.doShellScript(`'${script}'`)
} else if (line.startsWith('template:')) {
var path = trimLine(line, 'template:')
var template = currentApp.read(Path(path))
var lines = template.split("\n")
lines.forEach(line => handleLine(line))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment