You probably want to first collect all the texts to highlight and then 
highlight all at once:

-- Prompt the user to select a text file
set filePath to choose file with prompt "Please select a text file:" of type 
{"txt"}

-- Read the content of the selected text file
set inputText to read file filePath as «class utf8»


-- Function to extract the first 7 words from each text block using a regular 
expression
on extractFirst7WordsFromBlocks(inputText)
        set wordsLists to {}
        
        -- Set text item delimiters to find the text blocks
        set oldDelimiters to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "***"
        
        -- Split the text into items, each item corresponds to a text block
        set textItems to text items of inputText
        
        -- Regular expression to match 7 consecutive words
        set regex to "(?i)\\b\\w+\\b(\\W+\\w+){0,6}"
        
        -- Iterate through each text block and extract the first 7 words using 
a regular expression
        repeat with i from 2 to count of textItems by 2
                set textBlock to item i of textItems
                set wordsList to extractWordsWithRegex(textBlock, regex)
                
                -- Search for the text in the open PDF file in Skim
                if wordsList is not equal to "" then
                        set wordsLists to wordsLists & {wordsList}
                end if
        end repeat
        
        set AppleScript's text item delimiters to oldDelimiters
        
        return wordsLists
end extractFirst7WordsFromBlocks

-- Helper function to extract words using a regular expression
on extractWordsWithRegex(textBlock, regex)
        try
                set wordMatches to do shell script "echo " & quoted form of 
textBlock & " | grep -oE " & quoted form of regex
                set wordsList to paragraphs of wordMatches
                set resultText to item 1 of wordsList
        on error
                set resultText to ""
        end try
        
        return resultText
end extractWordsWithRegex

-- Helper function to set the search term in Skim and highlight the found text
on setSearchAndHighlightInSkim(searchTexts)
        tell application "Skim"
                activate
                
                if (count of documents) is 0 then
                        beep
                        display dialog "No documents found." buttons {"•"} 
default button 1 giving up after 3
                        return
                end if
                
                set noteType to my chooseNoteType({"Highlight"})
                if noteType is 0 then return
                
                set numberOfMatches to 0
                
                tell document 1
                        repeat with searchText in searchTexts
                                set theSel to find text searchText
                                repeat while theSel is not {}
                                        set numberOfMatches to numberOfMatches 
+ 1
                                        set theNote to make note with data 
theSel with properties {type:noteType}
                                        set text of theNote to searchText
                                        set theSel to find text searchText from 
theSel
                                end repeat
                        end repeat
                end tell
                
                beep
                display dialog "Done selecting " & numberOfMatches & " 
occurrences of \"" & searchText & "\"." buttons {"•"} default button 1 giving 
up after 3
        end tell
end setSearchAndHighlightInSkim

-- Helper function to choose the note type in Skim
on chooseNoteType(typeList)
        tell application "Skim"
                
                set noteTypeString to "Highlight"
                return highlight note
                
        end tell
end chooseNoteType

-- Call the function
set searchTexts to extractFirst7WordsFromBlocks(inputText)
if (count of searchTexts) is not 0 then
        setSearchAndHighlightInSkim(searchTexts)
end if


Christiaan

_______________________________________________
Skim-app-users mailing list
Skim-app-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-users

Reply via email to