Using search in AppleScript

I’m feeling a bit silly asking this, but after trying to find the answer for over an hour now, I’m going to open up the question to the floor.

I want to use search in a script; what I am trying to do is to find theSearchTerm in group B in database A.

This much is clear: set searchResults to search "name==" & theSearchTerm but I cannot figure out how to use either in or scope to define the database and group I want to search in. I don’t want to display the results, rather I need to know whether there is an item whose name is “theSearchTerm” already in the group.

Thanks for any help!

(Keywords: AppleScript, DNtp , search, scope)

I’m quite sure this is far too simple to be of help but this works for me (although admittedly it doesn’t specifiy a group):

set foundRecord to (search "name:" & theTarget & " kind:markdown") in theDatabase

where theDatabase is defined as:

set theDatabase to open database pDatabase

and pDatabase is defined accordingly as the required database.

I have hesitated to respond because I know you know much more about this than I do, so apologies if this is a completely useless response. :grinning:

Stephen

2 Likes

The simple things are sometimes the way to go; I was trying to include in in the search terms (in the same way you have included name and kind) and had brain block precluding me from getting to where I needed to be.

so set searchResults to (search "name==" & theName) in database "nameofdatabase" works; but I still can’t figure out how to define the scope (i.e. the group within the database).

Thanks for providing me with the first step :))

Whole aircraft have crashed due to that attitude :smiley: But on a more serious note: No.

2 Likes

and in fact even if I specifically designate a group in the database using that group’s uuid, I get the same results as if the whole database were designated:

tell application id "DNtp"
	set theName to "Test"
	set theGroup to get record with uuid "C45E7739-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
	set searchResults to (search "name:" & theName) in record theGroup
	return searchResults
end tell

The group designated in the script above doesn’t actually contain any items whose name is or contains “Test”; I still receive two results though - the two documents in the database whose name contains “Test”.

This should be set searchResults to (search "name:" & theName in theGroup)

1 Like

I could have sworn that was one of the million permutations I tried :see_no_evil: Thanks both of you - Criss, that works (you knew that, of course :wink: Is there any way to define the scope other than using the uuid (just out of interest, using the uuid is no problem of course)?

You can use anything that results in a record, e.g. get record at. If you want to search in a specific database you need to use in root of theDatabase .

@cgrunenberg for some AppleScript commands there’s a warning if the parameters are not used correctly which is quite useful. However with the search command it’s e.g. possible to use in theDatabase which is not correct and simply ignored, i.e. the search then yields results from all open databases. I think also giving a warning in such cases would be helpul.

1 Like

I tried that but got an error; I’ll post the precise script and error tomorrow.

That fooled me for a while, because it looked like things were working.

1 Like

You’re right, the next release will improve the error handling.

3 Likes

I was making the same mistake as with the uuid, including in in quotes or outside the parenthesis; so, of course, you suggestion works too. Thanks all for assisting me, really appreciate it.