Find existing RTF record given URL or name?

Howdy,

I’m a software developer, but new to applescript. I’m trying to do something simple via scripting…

(1) Create a new RTF record with a given name/URL/UUID and add some text
(2) Unless the record already exists, in which case just append some text to the bottom of the existing record

I’ve got (1) working fine, but in playing around with:

get record with UUID (uuid)

I can’t seem to retrieve the existing record.

exists record with URL (note_name)

works fine though (returns true or false as needed).

Any hints or ideas on retrieving records and appending to them (given a name or URL or UUID)?

Cheers,
Pete

Could you post the script? This would simplify the debugging/testing, thanks.

Definitely. Here it is:

Basically I want to have a script that gives me a dialog box, and takes what I enter to create a new journal RTF entry for a given day, or append to the existing entry for today if it already exists. It just adds a line with a timestamp and the user entry. I haven’t dealt with the database or group it is created in, but I’d like to be able to do this as well (instead of it just getting created in the default inbox). Any suggestions for improvements are greatly welcomed. Thanks


set temp to display dialog "What are you working on?" default answer ""
set text_user_entered to the text returned of temp

set note_name to "Work Journal - " & (date string of (current date))

set line_to_add to time string of (current date) & ":  " & text_user_entered & return

tell application id "com.devon-technologies.thinkpro2"
	if exists record with URL (note_name) then
		set the_record to (get record with uuid (note_name))
		-- update the_record here by appending new line to it!
	else
		create record with {name:note_name, type:rtf, rich text:line_to_add, date:current date, tags:"work journal", uuid:note_name, URL:note_name}
	end if
	
end tell

URLs/UUIDs and names are usually completely different things and therefore you can’t specify the name of the note for the commands “exists record with URL” and “get record with UUID”, especially as the UUID is ready only and can’t be defined by the user via “create record with”.

But here’s a slightly modified and working script but this supports only plain texts (as rich text scripting is really tricky):


set temp to display dialog "What are you working on?" default answer ""
set text_user_entered to the text returned of temp

set note_name to "Work Journal - " & (date string of (current date))

set line_to_add to time string of (current date) & ":  " & text_user_entered & return

tell application id "com.devon-technologies.thinkpro2"
	set theDB to current database
	set existingNotes to every content in theDB whose name is note_name
	if (count of existingNotes) is 1 then
		set theRecord to item 1 of existingNotes
		set plain text of theRecord to (plain text of theRecord) & line_to_add
	else
		create record with {name:note_name, type:text, plain text:line_to_add, date:current date, tags:"work journal"} in incoming group of theDB
	end if
	
end tell

Thanks for the working example. I realize that UUID and URL are different, but I didn’t realize I couldn’t set the UUID when creating the record (I was hoping to set it and then use it as a hook to find the record later).

Is the dictionary opened in the script editor the best API reference? I’m a veteran programmer but a new AppleScripter and am looking for references, code examples, etc.

The dictionary in the script editor describes the complete API. Examples can be found in ~/Library/Application Support/DEVONthink Pro 2/Scripts, in ~/Library/Scripts/, in the folder Extras/Scripts of the disk image and of course in the forum.