Filing ToDos from Things in DevonThink

So first things first. I don’t know much about scripting.
But I want to file completed ToDos from Things (Cultured Code) in DTP.
I have written this script which works but I have 2 problems

  1. I want the new records to go into a group of my choosing (i.e. I made a group called “thingsLog” in the global inbox, and I want to put the records in there but I can’t work out how.
  2. I would ideally like to ‘file’ the ToDos by tag … (i.e. make a group for that tag if it doesn’t already exist, then place a copy of the ToDo in that group) … any suggestions
    Thanks in advance

tell application "Things"
	set logbookToDos to to dos of list "Logbook"
	repeat with logbookToDo in logbookToDos
		
--Each DTP record should be constructed like a Things ToDo
		
		set ToDoID to id of logbookToDo
		set ToDoCreated to creation date of logbookToDo
		set ToDoCompleted to completion date of logbookToDo
		set ToDoName to name of logbookToDo
		set ToDoNotes to notes of logbookToDo
		set ToDoProject to project of logbookToDo
		set ToDoArea to area of logbookToDo
		
		set devonToDoText to "\"" & ToDoNotes
		
		set devonToDoID to ToDoName & ToDoCreated
		
		
		
		tell application id "com.devon-technologies.thinkpro2"
		
			create record with {name:devonToDoID, type:rtf, rich text:devonToDoText, creation date:ToDoCreated, modification date:ToDoCompleted, unread:0} in incoming group of database "inbox"
		end tell
		
	end repeat
end tell

E.g. create the group if necessary via…


set theGroup to create location "/thingsLog" in inbox

…and use theGroup for the “in” parameter of the “create record” command.

The “create location” command could be used for this too.

Christian … thanks very much for your help.
This is far from perfect … in particular …

Some disclaimers

  • I am a novice at scripting and couldn’t have done this without the excellent applescript guide from Cultured Code (Thanks)
  • It run’s pretty slowly and so if you have a lot of ToDos in your log please beware

Some requests

  • Anyone fancy improving this?

For what it’s worth …


tell application "Things"
	set processedToDos to 0
	
	set logbookToDos to to dos of list "Logbook"
	repeat with logbookToDo in logbookToDos
		set ToDoID to id of logbookToDo
		set ToDoCreated to creation date of logbookToDo
		set ToDoCompleted to completion date of logbookToDo
		set ToDoName to name of logbookToDo
		set ToDoNotes to notes of logbookToDo as string
		set ToDoProject to project of logbookToDo
		set ToDoArea to area of logbookToDo
		
		--make text of record
		set devonToDoText to ""
		
		if class of ToDoArea is string then set devonToDoText to "Area:" & tab & ToDoArea & return
		
		if class of ToDoProject is string then set devonToDoText to devonToDoText & "Project:" & tab & ToDoProject & return
		set devonToDoText to devonToDoText & "Notes:" & return & ToDoNotes
		
		--now get a list of tags and add an unTagged tag if no tags for purposes of filing
		
		set tagList to tags of logbookToDo
		if tagList is {} then
			set tag names of logbookToDo to "unTagged"
			set tagList to tags of logbookToDo
		end if
		
		repeat with aTag in tagList
			set tagName to name of aTag
			set recordPath to "/thingslog/" & tagName
			
			
			tell application id "com.devon-technologies.thinkpro2"
				
				set theTagGroup to create location recordPath in inbox
				
				create record with {name:ToDoName, type:rtf, rich text:devonToDoText, creation date:ToDoCreated, modification date:ToDoCompleted} in theTagGroup
				
			end tell
			
		end repeat
		move logbookToDo to list "Trash"
		set processedToDos to processedToDos + 1
		
	end repeat
	display dialog "You have just filed " & processedToDos & " ToDo's"
	
end tell

How did you execute the script? On Snow Leopard executing it via the menu extra is really slow whereas executing via DEVONthink Pro’s Scripts menu or Apple’s Script Editor is much faster. On Leopard, Apple’s Script Editor is quite slow too.

I was trying out your script and it got me going. i wanted to export all my todos from Things’ logbook into DTPO, but i wanted a folder for each Project containing each todo, and i wanted the tags to be pulled into DTPO as actual DT tags.

i’m no export, and mostly resign myself to modifying other code (if its anything remotely complex). i found a cleanup script on the Cultured Code website, used a bit of trial and error, and bits of your script, and came up with this thing.

a couple notes:

  1. obviously, thanks to you and to Stephen Voida for most of the code
  2. i have a database called “To-Do List” that this script dumps into - you’ll have to change that appropriately.
  3. its fairly quick, but still takes a while if you have a lot of logged todos to process.

if anyone has anything to add to streamline it (or add text formatting), feel free!

-- ************************************************************************
-- Things Logbook to DEVONthink Pro migration script
-- Modification by Tony Stout, tstout@lv-426.net
-- 
-- Exports todos (including tags) from the Things logbook to DEVONthink Pro, creating a top-level
-- folder called "thingslog", with each project created as a subfolder.  
--
-- Original script found on the Cultured Code 'Contributed Scripts' webpage.
--
-- Things Logbook Cleaner, v1.2
-- by Stephen Voida, svoida@gmail.com
--
-- quick utility script for finding/replacing text
-- based on http://foolsworkshop.com/applescript/2008/05/an-applescript-replace-text-method/
--
-- ************************************************************************


on findAndReplace(subject, find, replace)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	return subject
end findAndReplace

on processListOfToDos(toDos)
	
	tell application "Things"
		
		repeat with todo in toDos
			set ToDoProject to ""
			set ToDoArea to ""
			set ToDoCreated to creation date of todo
			set ToDoStatus to status of todo
			set ToDoName to name of todo
			set ToDoTags to tag names of todo
			set devonToDoText to ToDoName & return & return & "Status:" & tab & tab & ToDoStatus & return
			if ToDoStatus is canceled then set ToDoCompleted to cancellation date of todo
			if ToDoStatus is completed then set ToDoCompleted to completion date of todo
			if (project of todo) is not missing value then set ToDoProject to (name of project of todo)
			if (area of todo) is not missing value then set ToDoArea to (name of area of todo)
			if class of ToDoProject is string then set devonToDoText to devonToDoText & "Project:" & tab & ToDoProject & return
			if class of ToDoArea is string then set devonToDoText to devonToDoText & "Area:" & tab & ToDoArea & return & return
			if (notes of todo) is not missing value then
				set ToDoNotes to (notes of todo as string)
				set ToDoNotes to my findAndReplace(ToDoNotes, (ASCII character 10), "")
				set ToDoNotes to my findAndReplace(ToDoNotes, (ASCII character 13), " ")
				set ToDoNotes to my findAndReplace(ToDoNotes, tab, "  ")
				set ToDoNotes to my findAndReplace(ToDoNotes, "<note xml:space=\"preserve\">", "")
				set ToDoNotes to my findAndReplace(ToDoNotes, "</note>", "")
				set devonToDoText to devonToDoText & "Notes:" & return & ToDoNotes
			end if
			
			set recordPath to "/thingslog/" & ToDoProject
			
			tell application id "com.devon-technologies.thinkpro2"
				set theGroup to create location recordPath in "To-Do List"
				set theRecord to create record with {name:ToDoName, type:rtf, rich text:devonToDoText, creation date:ToDoCreated, modification date:ToDoCompleted} in theGroup
				if ToDoTags is not "" then
					set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
					set allTags to text items of ToDoTags
					set AppleScript's text item delimiters to od
					repeat with theTags in allTags
						set tags of theRecord to (parents of theRecord) & theTags
					end repeat
				end if
			end tell
			
			if class of todo is project then
				set projectTodos to to dos of todo
				my processListOfToDos(projectTodos)
			end if
			move todo to the list id "FocusTrash"
			
		end repeat
		
	end tell
	
end processListOfToDos


-- warn the user that this could take awhile
display dialog "This script can take several minutes to process your logbook contents, especially if you have many logged todos." & return & return & "It will look like nothing is happening, but please be patient!" with icon 2 with title "Things Logbook Cleaner"

tell application "Things"
	set logbookToDos to the to dos of list id "FocusLogbook"
	my processListOfToDos(logbookToDos)
	display dialog "Logbook cleaning finished."
end tell