Execute script when item is created

I am currently evaluating DEVONthink for my personal knowledge repository. One idea I really like is how Matt Henderson manages his daily log (see matt.makalumedia.com/archives/000223.html). That might be a good idea to start for me as well. So I’m currently trying to implement something that allows me to create that notes quickly.

What I’m currently missing is, that I want DT to automatically set the title on new items in my DailyNotes folder. The title should be set to for example “[2006-02-19 23:05]”. And ideally the curser would then stand behind that date. It appears that Matt uses Spell Catcher for that. But I don’t need all the extra features that Spell Catcher has and don’t want to shell out money for that.

So I’m thinking: is there a way to execute a script for every new document in my DailyNotes folder (that script would then set the title)? Or is there another way to do what I’d like to achieve?

No suggestions have been made, so I went ahead with a workaround. The following AppleScript prepends the creation date and time to the title. So I’ll create items in my “diary” folder and once in a while execute this script.

Works fine for me as it is.

-- Prepend the creation date&time to the title.
-- Created by Patrice Neff on 2006-02-22.

-- The date functions were extracted and modified from Joe's iPhoto
-- AppleScripts <http://www.joemaller.com/iphoto/>.

on formatDateString(theDate) -- returns the date as a string
	-- the following are for backwards compatibilty with pre-10.4 versions of AppleScript
	set theHours to (time of theDate) div hours
	set theMinutes to ((time of theDate)) div minutes - theHours * minutes
	
	set timestring to " " & my zeroPad(theHours, 2) & ":" & my zeroPad(theMinutes, 2)
	
	return "" & year of theDate & "-" & my zeroPad(my monthToNum(theDate), 2) & "-" & my zeroPad(day of theDate, 2) & timestring
end formatDateString

on monthToNum(theDate)
	-- the following is nearly 300% faster than a loop and indexed list 
	if month of theDate is January then return 1
	if month of theDate is February then return 2
	if month of theDate is March then return 3
	if month of theDate is April then return 4
	if month of theDate is May then return 5
	if month of theDate is June then return 6
	if month of theDate is July then return 7
	if month of theDate is August then return 8
	if month of theDate is September then return 9
	if month of theDate is October then return 10
	if month of theDate is November then return 11
	if month of theDate is December then return 12
end monthToNum

on zeroPad(theVal, endLength)
	-- endLength is number of total digits to return
	repeat (endLength / 10 as integer) + 1 times -- add 10 zeros at a time
		copy "0000000000" & theVal to theVal
	end repeat
	
	return text (endLength * -1) thru -1 of theVal
end zeroPad

tell application "DEVONthink Pro"
	activate
	try
		set this_selection to the selection
		if this_selection is {} then error "No content selected."
		
		repeat with this_record in this_selection
			set this_date to the creation date of this_record
			
			set old_title to the name of this_record
			set prefix to "[" & my formatDateString(this_date) & "] "
			set new_title to prefix & old_title
			if old_title does not start with prefix then set the name of this_record to new_title
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			try
				display alert "DEVONthink Pro" message error_message as warning
			on error number error_number
				if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
			end try
		end if
	end try
end tell

You could also use a triggered version of the script like this one:


-- Prepend the creation date & time to the title. 
-- Created by Patrice Neff on 2006-02-22.
-- Modified by Christian Grunenberg on 2006-02-23 for triggered scripts

on triggered(theRecord)
	tell application "DEVONthink Pro"
		try
			if type of theRecord is group then
				set theChildren to the children of theRecord
				repeat with this_record in theChildren
					set this_date to the creation date of this_record
					
					set theHours to (time of this_date) div hours
					set theMinutes to ((time of this_date)) div minutes - theHours * minutes
					set prefix to "[" & year of this_date & "-" & my zeroPad(my monthToNum(this_date), 2) & "-" & my zeroPad(day of this_date, 2) & " " & my zeroPad(theHours, 2) & ":" & my zeroPad(theMinutes, 2) & "] "
					
					set old_title to the name of this_record
					if old_title does not start with prefix then
						set new_title to prefix & old_title
						set the name of this_record to new_title
					end if
				end repeat
			end if
		on error msg
			display dialog msg
		end try
	end tell
end triggered

on monthToNum(theDate)
	if month of theDate is January then return 1
	if month of theDate is February then return 2
	if month of theDate is March then return 3
	if month of theDate is April then return 4
	if month of theDate is May then return 5
	if month of theDate is June then return 6
	if month of theDate is July then return 7
	if month of theDate is August then return 8
	if month of theDate is September then return 9
	if month of theDate is October then return 10
	if month of theDate is November then return 11
	if month of theDate is December then return 12
end monthToNum

on zeroPad(theVal, endLength)
	repeat (endLength / 10 as integer) + 1 times
		copy "0000000000" & theVal to theVal
	end repeat
	return text (endLength * -1) thru -1 of theVal
end zeroPad

Assuming your daily notes are stored in one group, then save this script somewhere on your harddisc, open the Info panel for the daily group and select this script.

Afterwards DT Pro will execute this script everytime you’re going to open the group in a new window or when you’re selecting the group in vertical/horizontal split or 3-pane views and therefore keep the titles automatically up-to-date.

Thanks, I’ll try that one. Though it may be a performance issue to execute that every time the group is opened.

That depends on the number of children - 100-200 notes shouldn’t cause noticable delays.