I can't figure out how to create documents in a specific database

I’m doing something wrong but I don’t understand what, I would appreciate if someone can tell me what I’m doing wrong.

I want to be able to create notes inside a folder in a specific datebase, the note should be placed in a group hierarchy based on the date. This all works fine, except that it’s created in the current database, not the one I specified (or though I specified)

I’ve tried this script

on create_document_in_ym_group(uDatabase, uBase, uTitle, uDate, uContent)
	set dateparts to get_date_parts(uDate)
	tell application id "DNtp"
		try
			activate
			tell database uDatabase
				set targetGroup to create location uBase & "/" & item 1 of dateparts & "/" & item 2 of dateparts
				set titleName to the short date string of uDate & " " & uTitle
				set foundDocuments to children of targetGroup whose name is titleName and type is markdown
				if ((count of foundDocuments) is 0) then -- Create the document from scratch
					set theContent to "# " & titleName & return & return & uContent
					set theDocument to create record with {name:titleName, content:theContent, type:markdown} in targetGroup
				else
					set theDocument to item 1 of foundDocuments
				end if
				open window for record theDocument
			end tell
		on error errMsg number errNum
			display alert (localized string "An error occured when adding the document.") & space & errMsg
		end try
	end tell
end create_document_in_ym_group

and the variant

on create_document_in_ym_group(uDatabase, uBase, uTitle, uDate, uContent)
	set dateparts to get_date_parts(uDate)
	tell application id "DNtp"
		try
			activate
			set targetDB to (open database uDatabase)
			set targetGroup to create location uBase & "/" & item 1 of dateparts & "/" & item 2 of dateparts in targetDB
			set titleName to the short date string of uDate & " " & uTitle
			set foundDocuments to children of targetGroup whose name is titleName and type is markdown
			if ((count of foundDocuments) is 0) then -- Create the document from scratch
				set theContent to "# " & titleName & return & return & uContent
				set theDocument to create record with {name:titleName, content:theContent, type:markdown} in targetGroup
			else
				set theDocument to item 1 of foundDocuments
			end if
			open window for record theDocument
			
		on error errMsg number errNum
			display alert (localized string "An error occured when adding the document.") & space & errMsg
		end try
	end tell
end create_document_in_ym_group

and I’ve tried calling this handler with this

create_document_in_ym_group("/Users/jem/Privat/Databases/Notes.dtBase2", "Testar", "Bara en test", the current date, myContent)

and

create_document_in_ym_group("Notes.dtBase2", "Testar", "Bara en test", the current date, myContent)

However, the note end up being created in the current database (the one I’m viewing when I run the script) not in the Notes database.

It seems like I don’t understand how to address a specific database, can anyone point to my error?

E.g. like this:

tell application id "DNtp"
	set theDB to database "Notes"
	set theGroup to create location "/Test/2024/08/13" in theDB
	set theDocument to create record with {name:"Note", content:"Content", type:markdown} in theGroup
end tell

1 Like

Huge thanks, now it works !!!