January 29, 2014

Collecting INFER & SearchBack Results in Accordance via Applescript

Accordance has a great INFER command that let's you find phrases in the corpus of one tab that 'seem similar to' the corpus in a second tag. Think searching out intertextuality echoes.
The Search Back feature let's you select a verse in the results, and see back to where that phrase is in the original source corpus.
Here is a batch script example of how to compile each of those results alongside each other in a text document.
I provide this Applescript with lots of comments so that Accordance users and with some scripting capacity can follow the flow and learn how to roll their own.
Here's a video demonstrating what on earth I'm talking about.


And here's the code:

-- Collect INFER and Search Back results in Accordance
property textApp : "TextEdit" -- name of your word processor or text editor
set d to 0 -- set global delay to 1 or more to observe the process, for troubleshooting purposes
set r to 100 -- max number of verses to iterate through

-- start the script with insertion point in Accordance in position 0, in front of the first marked verse of the INFER source tab (tab2) in the text window

set n to 1 -- counter for searches
set sourceVerse to ""
repeat r times
    tell application "Accordance" to activate
    delay 0.1
    delay d
    tell application "System Events"
        tell process "acord"
            click menu item "Search Back Linked Text" of menu 1 of menu bar item "Amplify" of menu bar 1
            delay 1
            -- Accordance moves to tab4
            
            -- Check Search Back results Search Criteria to make sure an errant string search wasn't conducted
            key code 123 -- left arrow to collapse any selection
            keystroke tab -- highlight search box
            keystroke "c" using {command down} -- copy Search Back search criteria
            delay 0.3
            delay d
            set searchBackCriteria to the clipboard -- save Search Back search criteria to variable
            
            -- go to source tab
            keystroke tab using {control down} -- go to tab1
            keystroke tab using {control down} -- go to tab2
            delay d
            key code 124 using {shift down, command down} -- Cmd Shft Rt to select entire source verse
            delay d
            keystroke "c" using {command down} -- copy
            delay 0.3
            delay d
            set previousSourceVerse to sourceVerse
            set sourceVerse to the clipboard
            delay 0.1
            
        end tell
    end tell
    
    if sourceVerse = previousSourceVerse then
        display dialog "Last source verse searched:" & return & sourceVerse
        exit repeat -- would just be repeating the last Source Verse
    end if
    
    if word 1 of searchBackCriteria is "INFER" then -- a valid Source Verse was used for the Search Back result (i.e. didn't search for a word from a wrapped line)
        
        -- paste the Source Verse in a word processing app or text editor
        tell application textApp to activate
        delay 0.1
        delay d
        tell application "System Events"
            tell process textApp
                keystroke return
                keystroke (n as string) -- add a iteration notation for each new entry
                keystroke " =========="
                keystroke return
                delay d
                keystroke "v" using {command down} -- paste source verse
                delay 0.3
                delay d
                key code 124 -- right arrow collapse selection
                keystroke return
                keystroke " --------"
                keystroke return
                delay d
            end tell
        end tell
        
        tell application "Accordance" to activate
        delay 0.1
        delay d
        tell application "System Events"
            tell process "acord"
                -- go to the results tab
                keystroke tab using {control down} -- go to tab3
                keystroke tab using {control down} -- go to tab4
                delay d
                
                -- go copy the results
                keystroke tab using {option down, shift down} -- place insertion point in the text pane
                delay d
                keystroke "a" using {command down} -- select all
                delay 0.1
                delay d
                keystroke "c" using {command down} -- copy Search Back results
                delay 0.3
                delay d
                
                -- go to the source tab
                keystroke tab using {control down} -- go to tab1
                keystroke tab using {control down} -- go to tab2
                delay d
                
            end tell
        end tell
    end if
    
    -- Accordance is already front app
    tell application "System Events"
        tell process "acord"
            -- prepare the Accordane window for the next verse search
            key code 125 -- down arrow to go to the next verse
            -- sometimes this will go to the next LINE of a single wrapped verse, but not always
            delay d
            key code 123 using {command down} -- Cmd left arrow to put insertion point at beginning of line
            delay d
        end tell
    end tell
    
    tell application textApp to activate
    delay 0.1
    delay d
    tell application "System Events"
        tell process textApp
            keystroke "v" using {command down} -- paste Search Back results
            delay 0.5
            delay d
            keystroke return
            delay d
            delay d
            set n to n + 1
        end tell
    end tell
end repeat