Search: Are digit wildcards broken?

I’m trying to match record names that start with a date.

Works:

name:<2013-01-01

Fails:

name:<[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]

Seems it only fails for digit wildcards as name:[N-M]SA works.

Seems that it is not the digit wildcards (i.e. name:[0-9] behaves as expected). But if you use the “starts with” or “ends with” prefixes like in your case, it gives nothing.
At least in my case, name:>[A-Z] doesn’t work either. Did you try that?

Interesting: If I search for name:!<[A-D](“name does not start with one of the letters A through D”), I get a record with the name “Aaxon…”, too. Either I’m not correctly understanding how that is supposed to work, or it is not working as it is supposed to.

No, didn’t try.

You’re right, this works

name:[N-M]SA

but this fails

name:<[N-M]SA

(checked that there are records that start with “NSA”).

Only the “matches” condition supports operators & wildcards, other conditions like “begins/ends with” or “contains” are plain string queries.

2 Likes

@BLUEFROG please consider adding a line to the help. Not sure where I would expect to find this though as “Wildcards” are explained under “Search operators” and “String Matching” is explained under “Search Prefixes”. Maybe in both?

There are hints in the smart group/rule and advanced search editors, fields supporting operators and/or wildcards have a dedicated placeholder:

1 Like

I seldom use the UI search :slightly_smiling_face:

BTW would it be possible to also add search queries that were initiated via AppleScript to the “Recent Searches” in the drop down? (I know it’s contrary to “seldom use the UI search” but it would be handy sometimes)

We might change this depending on additional feedback but right now there are no such plans yet.

Thanks

Can you clarify whether there is any way to search for filenames that start with a date within an applescript? I cannot even get something to work that matches a date, but what I am really looking for is something that starts with a date. In my case, the format is 21.04.05. Thanks.

Searching for filename is a bit complicated (without using regex).

Adjust the lists, the query and the database name (or remove in root of theDatabase to search all databases).

A bit complicated but without using regex:

-- Search records whose filename starts with a date of format 21.04.05

property theListChar_1 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
property theListChar_2 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
property theListChar_4 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
property theListChar_5 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
property theListChar_7 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
property theListChar_8 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

tell application id "DNtp"
	try
		set theDatabase to database "_temp"
		set theResults to search "name:[0-9][0-9].[0-9][0-9].[0-9][0-9]" in root of theDatabase -- it's not possible to search for filename
		
		set theResults_startsWithDate to {}
		
		repeat with i from 1 to count theResults
			set thisResult to item i of theResults
			set thisFilename to filename of thisResult
			try
				if ((character 1 of thisFilename) as integer) is in theListChar_1 then
					if ((character 2 of thisFilename) as integer) is in theListChar_2 then
						if ((character 4 of thisFilename) as integer) is in theListChar_4 then
							if ((character 5 of thisFilename) as integer) is in theListChar_5 then
								if ((character 7 of thisFilename) as integer) is in theListChar_7 then
									if ((character 8 of thisFilename) as integer) is in theListChar_8 then
										set end of theResults_startsWithDate to thisResult
									end if
								end if
							end if
						end if
					end if
				end if
			end try
		end repeat
		
		return theResults_startsWithDate
		
	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 regex:

(would be possible to provide the searched date e.g. via display dialog)

-- Search records whose filename starts with a date of format 21.04.05 (using regex)

use AppleScript version "2.7"
use framework "Foundation"
use scripting additions

set theDateQuery to "[0-9][0-9].[0-9][0-9].[0-9][0-9]"

tell application id "DNtp"
	try
		set theDatabase to database "_temp"
		set theResults to search "name:" & theDateQuery in root of theDatabase
		
		set theResults_startsWithDate to {}
		
		repeat with i from 1 to count theResults
			set thisResult to item i of theResults
			set thisFilename to filename of thisResult
			set matched to my regexFind(thisFilename, "^" & theDateQuery)
			if matched = true then
				set end of theResults_startsWithDate to thisResult
			end if
		end repeat
		
		return theResults_startsWithDate
		
	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

on regexFind(theText, thePattern)
	try
		set theString to current application's NSString's stringWithString:theText
		set theRange to theString's rangeOfString:thePattern options:(current application's NSRegularExpressionSearch)
		if |length| of theRange > 0 then
			return true
		else
			return false
		end if
	on error error_message number error_number
		activate
		display alert "Error: Handler \"regexFind\"" message error_message as warning
		error number -128
	end try
end regexFind

1 Like

Thank you! These should work. I greatly appreciate it.

1 Like