Skip to content

Instantly share code, notes, and snippets.

@freklov
Last active April 1, 2017 19:07
Show Gist options
  • Select an option

  • Save freklov/d54c0117125d9b50c9196118e12da3ff to your computer and use it in GitHub Desktop.

Select an option

Save freklov/d54c0117125d9b50c9196118e12da3ff to your computer and use it in GitHub Desktop.
AppleScript for Things: Create a new project from a text template
# things-load_from_template freklov 04/01/2017
# Create a new project from a text template
#
# Load Process
# Choose text file from folder
# Create project in Things with name of file
# Add each line of file to created project as to do
# Bring things into project edit mode
#
# Requirements
# Text files need to be created with type utf-8
# set default locaton for template files to Documents
set textTemplateFolder to ((path to documents folder)) as text
# open file dialog to allow user to choose file
# only text files are allowed
# pressing the cancel button stops script execution
set aliasTemplateChoosen to ¬
¬
(choose file with prompt ¬
"Choose the template you want to use:" of type {"public.text"} ¬
default location (alias textTemplateFolder) ¬
invisibles false ¬
)
# read to dos from file
open for access aliasTemplateChoosen
set dataTemplateChoosen to ¬
read aliasTemplateChoosen ¬
using delimiter {linefeed} ¬
as «class utf8»
close access aliasTemplateChoosen
# get filename for creating project name
tell application "Finder"
set {textFileName, textFileExtension} to the {displayed name, name extension} ¬
of aliasTemplateChoosen
# remove extension (.txt) from filename
set textFileName to my findAndReplaceInText(textFileName, "." & textFileExtension, "")
end tell
# Things
# create project
# create a to do for each line of the file
# reopen Things window and bring it to front
# bring Things into project edit modus
tell application "Things"
set projectFromTemplate to ¬
make new project ¬
with properties {name:textFileName}
repeat with textItem in dataTemplateChoosen
tell projectFromTemplate to ¬
make new to do ¬
with properties {name:textItem}
end repeat
reopen
activate
edit projectFromTemplate
end tell
# source Mac Automation Scripting Guide
# https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/index.html#//apple_ref/doc/uid/TP40016239-CH56-SW1
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment