Skip to content

Instantly share code, notes, and snippets.

@cdzombak
Last active March 15, 2021 17:48
Show Gist options
  • Save cdzombak/3df8c2559b52205fa67d366b2e9f90d6 to your computer and use it in GitHub Desktop.
Save cdzombak/3df8c2559b52205fa67d366b2e9f90d6 to your computer and use it in GitHub Desktop.
Bear templates for Things projects
# Sample Bear-Things Template
* item a
notes for item A
* item b
* item c
notes for item C
; comments/empty lines begin with `;`
a second line of notes for C
---
#things/templates
on showConfirmationDialog(theProjectName)
display dialog "This will create a new project in Things based on the “" & theProjectName & "” template from Bear." buttons {"Cancel", "Create Project"} default button "Create Project" with title "Things Project from Bear Template"
end showConfirmationDialog
on trim_line(this_text, trim_chars, trim_indicator)
-- 0 = beginning, 1 = end, 2 = both
set x to the length of the trim_chars
-- TRIM BEGINNING
if the trim_indicator is in {0, 2} then
repeat while this_text begins with the trim_chars
try
set this_text to (characters (x + 1) thru -1 of this_text) as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
-- TRIM ENDING
if the trim_indicator is in {1, 2} then
repeat while this_text ends with the trim_chars
try
set this_text to (characters 1 thru -(x + 1) of this_text) as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
return this_text
end trim_line
on commit_todo(todoName, todoNotes, theProject)
if the length of todoName is 0 then
return
end if
tell application id "com.culturedcode.ThingsMac"
-- set theTodo to
make new to do ¬
with properties {name:todoName, notes:todoNotes} ¬
at end of theProject -- project theProjectName
end tell
end commit_todo
on run
set previousClipboard to the clipboard
tell application "Bear" to activate
tell application "System Events"
tell process "Bear"
keystroke "c" using command down
end tell
end tell
delay 0.2
set theParagraphs to paragraphs of (the clipboard)
set the clipboard to previousClipboard
-- project name is the first line, minus Markdown header
set theProjectName to trim_line(item 1 of theParagraphs, "# ", 0)
--set theProjectName to item 1 of theParagraphs
set theParagraphs to rest of theParagraphs
showConfirmationDialog(theProjectName)
tell application id "com.culturedcode.ThingsMac"
activate
set theProject to make new project with properties {name:theProjectName}
show theProject
end tell
-- buffer todo name and notes as we read line-by-line
set todoName to ""
set todoNotes to ""
repeat with nextLine in theParagraphs
if length of nextLine is greater than 0 then
-- we're done if we hit a HR, which I use as a footer in Bear
if nextLine begins with "--" then
exit repeat
end if
set theChars to characters of nextLine
if item 1 of theChars is "*" or item 1 of theChars is "-" then
-- if we hit a new todo, try to commit what we've buffered so far, then reset our buffers
commit_todo(todoName, todoNotes, theProject)
set todoName to trim_line(nextLine, item 1 of theChars & " ", 0)
set todoNotes to ""
else
-- not a new todo, so buffer as notes for the current todo
if length of todoNotes is not 0 then
set todoNotes to todoNotes & "
"
end if
if item 1 of theChars is ";" then
set nextLine to ""
end if
set todoNotes to todoNotes & nextLine
end if
end if
end repeat
-- commit the last-buffered todo
commit_todo(todoName, todoNotes, theProject)
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment