Well, source code for both Applescript and swift contains large amounts of proprietary information, so I can’t really circulate it. However, they are largely conceptually structured the same way:
In applescript, several arrays of structures contain regular expressions and associated keywords. A function iterates through the arrays, sends the regular expressions out to the shell to evaluate, and based on the responses builds a result array of appropriate keywords. That result array is then used to construct a new file name and rename the file in DEVONthink.
In swift, the process is somewhat similar, though the associations between regular expressions and keywords are done with enums, and the regular expression parsing is done with the swift standard library.
Obviously, Applescript bears a huge overhead, both in talking to DEVONthink through Apple events, and in sending regular expressions to the shell for execution. Overall execution speed allowed for two renames per second, an unbearable burden for hundreds or thousands of files.
Swift on the other hand talks directly to DEVONthink through the scripting bridge, builds an array of all the selected files, pulls out their texts, and then runs the various regular expressions against that text. Finally, it builds an array of file names and tags to assign to each file and sends those back through the scripting bridge to DEVONthink. Early testing shows dozens of files are renamed in a couple of seconds; I haven’t tried a larger selection set yet.
The textual analysis process isn’t particularly exotic. However, I know the choice of swift as the language probably is. I’m trying to standardize on a single language as much as possible in our small office to avoid the overhead of having to be multilingual in SQL, shell scripts, python, swift, applescript, etc., etc.
It’s interesting to discover that bypassing Applescript and going directly to compiled code yields such a large efficiency, so much so that I am a little surprised that DEVONthink hasn’t incorporated an API to bypass Applescript altogether.