Applescript to check for existing document

This is purely for personal interest - I know it can be trivially done in various manual ways. I have a custom AppleScript in the DT3 scripts menu to create a new markdown document with name = today’s date or open the document if it exists in the database. My script fails on the following line:
set searchResults to search "name == '" & currentDate & "'" in journalDatabase with error 'invalid argument in (-50). I am at a loss to see why this is failing. Any suggestions as to why the command is incorrect would be welcome. Here is the complete script.

-- Get the current date in YYYY-MM-DD format
set currentDate to (do shell script "date +%Y-%m-%d")

-- Define the name of the Journal database
set journalDatabaseName to "Journal"

tell application id "DNtp"
	-- Locate the Journal database
	set journalDatabase to database journalDatabaseName
	if journalDatabase is missing value then
		display alert "Database not found" message "The 'Journal' database could not be found." as warning
		return
	end if
	
	set searchResults to {}
	-- set query to "name == '" & currentDate & "'"
	-- set searchResults to search query in journalDatabase
	set searchResults to search "name == '" & currentDate & "'" in journalDatabase 
	
	-- Check if there are any matching results
	if (count of filteredResults) = 0 then
		-- No note found; create a new one
		set newNote to create record with {name:currentDate, type:markdown} in (root of journalDatabase)
		open tab for record newNote -- Open the new note for editing
	else
		-- Note found; open it
		open tab for record (item 1 of filteredResults)
	end if
end tell

A database is not a record, as you can see in the AppleScript dictionary. Therefore you should use

search query in (root of journalDatabase)

instead of

search query in journalDatabase

There are other issues with your script. Hopefully the amazing GPT will figure those out :wink:

I’d hazard a guess you don’t need to approach things this way unless you’re just randomly creating documents anywhere and everywhere in your database. :thinking:
Are you putting these documents in a specific location in your database?

Crikey, I wasn’t expecting a response so quickly. Thanks for your input. The script wants to find out if any records have the name, in this case, a string looking like the date in “yyyy-mm-dd” format. If so, open it, if not, create it. I have tried the “root of journalDatabase” and it did not work either. BTW I’m not looking for a database, I’m (hopefully) testing if a (markdown) document exists in the database. Also, I don’t expect anyone to provide an alternative solution, I am only concerned as to why the AppleScript line fails and in general, how to correctly search for a document in a database in AppleScript. Hope that clarifies. Regards, Pete

Oh no, it’s happened again
I just edited the script with (root of journalDatabase) as suggested and it worked - makes me look a fool. :hot_face: Sorry meowky. You were right, I was wrong. I’m going to have a strong cup of tea. :tea:
No further input required

You’re welcome (though it’s 3am here - and yes, what am I doing working at 3am :thinking:)

My question still stands: Where are you making the Markdown documents? Just anywhere in the database with no formal structure, like a Journal group or similar?

1 Like

At the moment, it’s just anywhere in the database as you say, like a Journal. Its Saturday 16:00 here - get some sleep!! regards, Pete

Here’s an approach in pure AppleScript…

tell application id "DNtp"
	set {year:yy, month:mm, day:dd} to (current date)
	set dateString to ((yy & "-" & my zeropad(mm as integer) & "-" & my zeropad(dd)) as string)
	set matchName to (dateString & " Journal" as string)
	
	tell current database
		set todaysFile to (contents whose name begins with matchName)
		if (count todaysFile) > 1 then -- Multiple matching documents, alert and quit.
			display alert "More than one document found!"
			return
		else if todaysFile is {} then -- Nothing's found, make a new document
			set newJournal to create record with {name:matchName, type:markdown, content:("# " & matchName & "  " & linefeed & "---" & linefeed as string)} in root
			open tab for record newJournal
		else -- Document found, open it in a new document window
			open tab for record (item 1 of todaysFile)
		end if
	end tell
end tell

on zeropad(theNum)
	return (characters -2 thru -1 of ("0" & theNum) as string)
end zeropad

15 minute coding and crashing now… :slight_smile: :sleeping:

PS: Multiple documents can be reported if a matching document is in the database’s Trash.