So I have some OCR’d records and I would like to search through them to find whether there any occurences of words in a list (could be up to 100 words in the list). If there are I would like to put the list item into a new list.
set myTags to {"Lloyds", "Barclays", "Test3", "Test4"}
set myAssigned to {}
tell application id "DNtp"
set theRecord to first item of selected records
set theString to plain text of theRecord
repeat with myTag in myTags
if myTag is in theString then
copy myTag to end of myAssigned
end if
end repeat
return myAssigned
end tell
I am getting back {item 1 of {"Lloyds", "Barclays", "Test3", "Test4"}, item 2 of {"Lloyds", "Barclays", "Test3", "Test4"}}
for the matches on the first two items, but I rather these were in one list.
Can some kind soul tell me what I am doing wrong please?
Fixed this it should be…
copy text of myTag to end of myAssigned
Just for the fun of it and because I had some extra parenthesis lying around, I wrote a JavaScript version (untested!) of it.
(() => {
const tags = ["Lloyds", "Barclays", "Test3", "Test4"]
const app = Application("DEVONthink 3");
const rec = app.selectedRecords()[0];
const txt = re.plainText();
const newList = tags.filter(t => txt.indexOf(t) >= 0);
return newList;
})()
The filter
method returns those elements of the array it operates on that pass the test. So if txt.indexOf(aTag)
is >=0 (i.e. aTag
appears in txt
) aTag
is added to newList
.
As an aside: Why is text of myTag
not the same as myTag
if myTag
is an item of myTags
? I don’t really know AppleScript, but this at least looks counterintuitive. If I have a list of strings, every item of this list should already be a string, even without converting it to one.