AppleScript cannot find DT items with filenames of mixed Chinese and English characters

I tried to use the following AppleScript to find an item in a DT database:

tell application id "DNtp"
	set theRecords to lookup records with file "测试Test.md"
	set theRecord to the first item in theRecords
	display dialog name of theRecord as string
end tell

With the specified file name 测试Test.md, I got the following error: error "Can’t get item 1 of {}." number -1728 from item 1 of {}.

Is there a workaround for working with DT items with file names of mixed Chinese and English characters?

You could use search. The default scope of search is “all databases”, add e.g. in root of current database if that’s what you want.

tell application id "DNtp"
	try
		set theFilename to "测试Test.md"
		set theResults to search "filename:" & theFilename in root of current database
		if theResults ≠ {} then
			set theRecord to the first item in theResults
			display dialog name of theRecord as string
		else
			-- do something, e.g.
			display dialog "No matches for: \"" & theFilename & "\""
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

Using lookup records with file works if there’s a space between the different languages (but that probably doesn’t help …)

tell application id "DNtp"
	try
		set theResults to lookup records with file "测试Test.md"
		--> {}
				
		set theResults to lookup records with file "测试 Test.md"
		--> {content id 205238 of database id 2}
		
		set theResults to lookup records with file "Test 测试.md"
		--> {content id 205211 of database id 2}
		
		set theResults to lookup records with file "测 Test 试.md"
		--> {content id 205266 of database id 2}
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

Thanks a lot for the help! This works for me.

1 Like

Interesting finding. This explains why lookup sometimes worked for me, sometimes did not, as some file names have a space between different languages and some do not.

The commands exists record with file and lookup records with file seem to be almost completely broken since version 3.0, even without Chinese characters. The next release will fix this and make them faster too.

2 Likes