Faulty counter in database

After upgrade from version 2 to 3, the database counter in the API produces failures:

set theSubject to …
tell application id “DNtp”
set existingEntries to search theSubject & “.eml” in theGroup
if ((count of existingEntries) = 0) then

endif
end tell

Doesn´t work anymore because

  • no entries in database --> counter = 1
  • 1 entry in database --> counter = 2

No changes at this script since 1,5 years :slight_smile:

Which subject did you use? It’s possible that there’s a conflict with the search syntax, at least a simple test worked as expected over here.

I use the content from the mail subject:
tell theMessage
set IDofMessage to the id as rich text
set theDateReceived to the date received
set theDateSent to the date sent
set theSender to the sender
set theSubject to the subject
if theSubject is equal to “” then
set theSubject to pNoSubjectString
end if
set theSource to the source
set theReadFlag to the read status
end tell

Did you change the search syntax in version 3 ?

The syntax was extended. You could try to display the name for each result to figure out which additional result is found.

small example ?

E.g.

	repeat with theEntry in existingEntries
		display dialog ((name of theEntry) as string)
	end repeat

selected the newest record but get back record before+selected record:
2019.09.30-15:54:24 Name: Sxxxx xxxxxx xxxx xxxxxx xxxx端x xxxxxxxx.xx.28.09.2918 .eml
2019.09.30-15:54:24 Name: Sxxxx xxxxxx xxxx xxxxxx xxxx端x xxxxxxxx.xx.29.09.2918 .eml
or
2019.09.30-15:54:24 Name: Sxxxx xxxxxx xxxx xxxxxx xxxx端x xxxxxxxx.xx.27.09.2918 .eml
2019.09.30-15:54:24 Name: Sxxxx xxxxxx xxxx xxxxxx xxxx端x xxxxxxxx.xx.28.09.2918 .eml

there is a special char in the output.
Originally it´s a “ü” which is stored correctly in the database of Devonthink.

A copy of the complete script would be useful.

-- this string is used when the message subject is empty
 property pNoSubjectString : "(no subject)"
 property nameDatabase : "~/Documents/DMS/myOwn.dtBase2"
 property nameGroup : "/Office/subdir/"
 property nameDestinationMailbox : "Pxxxxx"
 property nameDestinationMailboxMail : "MyLocal folders/Administrator/Pxxxxx"
 property nameSourceMailboxMail : " "
 
 using terms from application "Mail"
 	on perform mail action with messages theSelectedMessages for rule theRule
 		tell application "Mail"
 			
 			set ruleName to name of theRule
 			set ruleScriptName to name of me
 			set sumSelectedMessages to count of theSelectedMessages
 
 			repeat with theMessage in theSelectedMessages
 
 				set IDofMessage to id of theMessage as rich text
 			
 				set nameSourceMailboxMail to name of mailbox of theMessage
 			
 				set theAccount to name of account of mailbox of theMessage
 				set IDofMessage to id of theMessage as rich text
 
 				tell theMessage
 					set IDofMessage to the id as rich text
 					set theDateReceived to the date received
 					set theDateSent to the date sent
 					set theSender to the sender
 					set theSubject to the subject
 					if theSubject is equal to "" then
 						set theSubject to pNoSubjectString
 					end if
 					set theSource to the source
 					set theReadFlag to the read status
 				end tell
 
 				tell application id "DNtp"
 					try
 						set theDatabase to open database nameDatabase -- Ensure that the database is open
 						set theGroup to create location nameGroup & nameDestinationMailbox in theDatabase
 						set existingEntries to search theSubject & ".eml" in theGroup
 						set existingRecordNumber to count of existingEntries
 						repeat with theEntry in existingEntries
 							my logToConsole("Name: " & (name of theEntry) as string)
 						end repeat
 						if ((count of existingEntries) = 1) then
 							create record with {name:theSubject & ".eml", type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string), unread:(not theReadFlag)} in theGroup
 						end if
 					on error error_message number error_number
 						if error_number is not -128 then display alert "Mail" message error_message as warning
 					end try
 					
 				end tell
 				
 				set IDofMessage to id of theMessage as rich text
 				
 			end repeat
 			
 			-- now move the message into another folder			
 			repeat with theMessage in theSelectedMessages
 				set IDofMessage to id of theMessage as rich text
 				if (nameDestinationMailboxMail does not contain nameSourceMailboxMail) then
 					set IDofMessage to id of theMessage as rich text
 					try
 						move theMessage to mailbox nameDestinationMailboxMail of account theAccount -- a folder contained by another folder
 						delay 0.5
 					on error error_message number error_number
 						if error_number is not -128 then display alert "Mail" message error_message as warning
 					end try
 				end if
 				set IDofMessage to id of theMessage as rich text
 			end repeat		
 			
 		end tell
 	end perform mail action with messages
 end using terms from
 
 on logToConsole(txt)
 	-- display dialog "Text: " & txt
 	set output_date to (do shell script "date '+%Y.%m.%d-%H:%M:%S'")
 	-- do shell script "/usr/bin/logger -t \"Mail script DEVONthink:\" " & txt's quoted form
 	do shell script "echo " & output_date & " " & quoted form of (txt as string) & " >> ~/Desktop/mail_script_debug.txt"
 end logToConsole

Subjects indeed quite often collide with the search syntax. This simple test script seems to work as expected:

-- this string is used when the message subject is empty
property pNoSubjectString : "(no subject)"
property pDatabasePath : ""
property pLocation : "/Test"

tell application "Mail"
	set theSelectedMessages to the selection
	repeat with theMessage in theSelectedMessages
		tell theMessage
			set theDateReceived to the date received
			set theDateSent to the date sent
			set theSender to the sender
			set theSubject to the subject
			if theSubject is equal to "" then
				set theSubject to pNoSubjectString
			end if
			set theSource to the source
			set theReadFlag to the read status
		end tell
		
		tell application id "DNtp"
			try
				if pDatabasePath is not "" then
					set theDatabase to open database pDatabase -- Ensure that the database is open
				else
					set theDatabase to inbox
				end if
				set theGroup to create location pLocation in theDatabase
				set theSearchString to my replaceText(theSubject, "?", " ")
				set theSearchString to my replaceText(theSearchString, "(", " ")
				set theSearchString to my replaceText(theSearchString, ")", " ")
				set theSearchString to my replaceText(theSearchString, "[", " ")
				set theSearchString to my replaceText(theSearchString, "]", " ")
				set theSearchString to my replaceText(theSearchString, "-", " ")
				set theSearchString to "\"" & theSearchString & "\" kind:email"
				set existingEntries to search theSearchString in theGroup
				if ((count of existingEntries) = 0) then create record with {name:theSubject & ".eml", type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string), unread:(not theReadFlag)} in theGroup
			on error error_message number error_number
				if error_number is not -128 then display alert "DEVONthink" message error_message as warning
			end try
		end tell
	end repeat
end tell

on replaceText(theString, find, replace)
	if theString contains find then
		local od
		set od to text item delimiters of AppleScript
		set text item delimiters of AppleScript to find
		set theString to text items of theString
		
		set text item delimiters of AppleScript to replace
		set theString to "" & theString
		set text item delimiters of AppleScript to od
	end if
	return theString
end replaceText

yep … your script changes are working.

thx