is it possible to compare records in list with applescript?


This is one of those basic questions of “do I understand what I’m doing” that is posed as a “how do you do this?” kind of question.

I want to write a script that will set the state on all records in the current group that do not contain a given string. I would like to do this as fast as possible by asking applescript if, in iterating through the list of all the records if the record is in the list. But when I try to run the script that would do this, applescript seems to not like the fact that I am trying to see if the current record is contained in the list of found records. (It complains specifically that it “cannot make item into a vector”).

Here’s the code:

---set state on all items *not* containing string

tell application "DEVONthink Pro"
	set cuPos to {}
	using terms from application "DEVONthink Pro"
			set cuPos to (selection of think window 1)
		on error
			display dialog "please make sure you have selected an article"
		end try
		if cuPos is {} or (kind of item 1 of cuPos is "Group") then
			display dialog "please make sure you have selected an article"
			if parent of item 1 of cuPos is not {} then
				set the_group to last item of parent of item 1 of cuPos
				set the_group to root of current database
			end if
		end if
		set search_string to ""
		display dialog "does not contain" default answer search_string
		if button returned of result is "Cancel" then
			set search_string to text returned of result
		end if
		set all_recs to get children of the_group
		set found_recs to search search_string in the_group
		repeat with this_rec in all_recs
			set state visibility of this_rec to true
			if found_recs contains this_rec then  ----this is the line that fails
				set state of this_rec to true
				set state of this_rec to false
			end if
		end repeat
	end using terms from
end tell

Shouldn’t this work? I tried putting {} around this_rec in the line that fails, but no luck. The question I have is shouldn’t this be valid? Is this a flaw in dtpro? Or does applescript only allow list compares on certain types of data (i.e. strings and numbers)

If anyone can explain this, I’d be most grateful…

p.s. I understand of course that I can iterate through the found set or some such thing and probably make this work, but I am partly interested in why this doesn’t work as is. Why does applescript say “vector”? is that some contrast to a scalar? ?!?

yeah i know this issue…

the solution is one of 2 of the following:

  1. don’t use “repeat with this_rec in all_recs”. instead use:

repeat with i from 1 to (count all_recs)
set this_rec to item i of all_recs
-- etc

  1. if that doesn’t work then it’s some problem / bug with the “if found_recs contains this_rec” syntax. (rather then the other way around, ie, “if this_rec is in found_recs” what i’ve done here is use a matchflag in another repeat loop:

set matchflag to false
repeat with x from 1 to (count found_recs)
if (item x of found_recs) is this_rec then
set matchflag to true
exit repeat
end if
end repeat
if matchflag then
  -- do what you want
 -- do that other thing
end if

please let me / us know if just 1 is sufficient or not. it’s something i never understood myself, just worked around.

thanks for the help!

It’s good to know i’m not the only one who is confused by data typing in applescript. On several occasions with this kind of strangeness I think about doing my scripting in python using appscript. I wonder if it would get around this problem or whether it is a problem with the glue to devonthink.

As for the script, the answer is that only plan b works (i.e full iteration, record by record compare) and i have no idea why. if anyone ever reads this and knows the answer, please share!

meanwhile, I’ll post a completed script below as soon as i get the other kinks worked out…


Just wanted to say thanks to the DT team for pulling all of these old threads into these forums when you set up Discourse. This discussion from 14 years ago (!) helped me solve an issue just now. Wild!

1 Like