After spending many hours on this (probably, this corresponds to the
"minutes" of someone with some meaningful  Applescript skills), here is a
script that may be useful for some folks, maybe after being modified
accordingly.

Say, you have a number of quotes separated by " *** " in a .txt file
(whether they are done or kindle or not... Kindle highlights have to be
modified so that they have only *** to separate them.)
This should search the first 7 words of each highlight on an open Skim pdf
and highlight them....

I am sure this can be much improved but sort of works for me...

On a remotely related issue, previously we could navigate among annotations
on Skim by selecting one on the page and pressing "alt+down arrow/." That
doesn't seem to work anymore?

Again, thank you for the suggestions, Christian -- indeed, I added the
"highlight occurrences" script to one AI came up with to have the script
above...

Script:

-- 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 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*

setSearchAndHighlightInSkim(wordsList)

*end* *if*

*end* *repeat*

*set* AppleScript's text item delimiters *to* oldDelimiters

*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(searchText)

*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

*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* *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

extractFirst7WordsFromBlocks(inputText)

On Sat, Dec 30, 2023 at 2:02 PM haphaes haphaes <haphaes2...@gmail.com>
wrote:

> Thank you very much, Christiaan, for your detailed reply and suggestion. I
> will surely give that a try...
> Cheers
>
> On Sat, Dec 30, 2023 at 1:27 PM Christiaan Hofman <cmhof...@gmail.com>
> wrote:
>
>>
>>
>> On 30 Dec 2023, at 10:10, haphaes haphaes <haphaes2...@gmail.com> wrote:
>>
>>
>> Hi,
>> Apparently, what I would like to seek help about is quite similar to the
>> issue raised here:
>> https://sourceforge.net/p/skim-app/mailman/message/37743641/
>>
>> Specifically, what I need to do is the following:
>> If I have a lot of pdf files to read, here is what I usually do;
>> I convert them both to text-based pdfs and epubs, the same OCR app and
>> then send the epub file to Kindle using amazon's dedicated app.
>>
>> ... Then comes reading / highlighting on the Kindle and transferring the
>> "My Clippings" file in the kindle to the computer. Using a website I can
>> get transfer the highlights for only one book to, say, a text file.
>>
>> Now, what would be incredibly useful is if after opening the pdf file on
>> Skim, I could have an applescript search for each highlight on the text
>> file in the pdf on skim and highlight it...
>>
>> Further, if for one single highlight the script cannot find it (probably
>> because, it's too long) the script can reduce the number of the words to
>> look for and then add the highlight once it finds a match.
>>
>> (e.g. a single highlight on Kindle:
>> "All men are mortal, Socrates is a man, therefore Socrates is mortal. "
>>
>> When the script doesn't find a match for the whole sentence, it will
>> search for a phrase with a reduced number of words, say:
>>
>> "All men are mortal, Socrates is a man,"
>>
>> if still no match, reduce 4 words again:
>> Hi,
>> Apparently, what I would like to seek help about is quite similar to the
>> issue raised here:
>> https://sourceforge.net/p/skim-app/mailman/message/37743641/
>>
>> Specifically, what I need to do is the following:
>> If I have a lot of pdf files to read, here is what I usually do;
>> I convert them both to text-based pdfs and epubs, the same OCR app and
>> then send the epub file to Kindle using amazon's dedicated app.
>>
>> ... Then comes reading / highlighting on the Kindle and transferring the
>> "My Clippings" file in the kindle to the computer. Using a website I can
>> get the highlights for only one book...
>>
>> Now, what would be incredibly useful is if I after opening the pdf files
>> on Skim, I could then get the highlights for the single book on a text file
>> and have an applescript search for each highlight on the text file in the
>> pdf on skim and highlight it...
>>
>> Further, if for one single highlight the script cannot find it (probably
>> because, it's too long) the script can reduce the number of the words to
>> look for and then add the highlight once it finds a match.
>>
>> (e.g. a single highlight on Kindle:
>> "All men are mortal, Socrates is a man, therefore Socrates is mortal. "
>>
>> When the script doesn't find a match for the whole sentence, it will
>> search for a phrase with a reduced number of words, say:
>>
>> "All men are mortal, Socrates is a man,"
>>
>> if still no match, delete the last 4 words from the search phrase again
>> etc
>>
>> "All men are mortal,"
>> But stop there, don't search for less than, say, e.g 4  words
>>
>>
>> Once it finds the phrase, the script will have skim highlight it.
>>
>> And then will do the same for the second highlight from the Kindle/
>> ereader/ any textfile separated by "paragraphs" etc.
>>
>>
>> What do you think? Any chance for that? I was hoping that would be
>> possible nowadays and asked, of course, what else, ChatGPT
>>  Below is its reply: --well, it doesn't work..
>>
>> Many thanks everyone!
>> -- Specify the paths to your text file and PDF file
>> set textFilePath to "/path/to/your/textfile.txt"
>> set pdfFilePath to "/path/to/your/pdfdocument.pdf"
>>
>> -- Read lines from the text file
>> set linesToSearch to paragraphs of (read file textFilePath)
>>
>> -- Iterate through each line and search for it in the PDF using Skim
>> Reader
>> repeat with currentLine in linesToSearch
>>     set searchString to currentLine
>>
>>     -- Use a shell command to search for the text in the PDF
>>     set searchCommand to "grep -n " & quoted form of searchString & " " &
>> quoted form of pdfFilePath
>>     set searchResult to do shell script searchCommand
>>
>>     -- Extract the line number from the search result
>>     set lineNumber to first word of searchResult
>>
>>     -- Use SkimNotes command-line tool to add annotation to the PDF
>>     set skimCommand to "skimnotes add -page " & lineNumber & " " & quoted
>> form of pdfFilePath
>>     do shell script skimCommand
>> end repeat
>>
>> ——
>>
>>
>> Interesting script, it shows AI picks up stuff, and does not understand
>> what it means. It certainly isn’t ready for programming.
>>
>> Although perhaps some ideas could be helpful. If I understand you
>> correctly, you have a text file containing strings to highlight (is there
>> no more information, like the location where they are?), and a PDF where
>> you want to highlight those texts. Getting the texts to highlight;ight
>> could be something like in this script. But the searching and highlighting
>> in the PDF should be a lot different. You can use the “find” command in
>> Skim, and then the “make new note”. On the Wiki there is a sample
>> AppleScript to highlight occurrences of a search string, you could use part
>> of that script to do this part.
>>
>> HTH,
>> Christiaan
>>
>> _______________________________________________
>> Skim-app-users mailing list
>> Skim-app-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/skim-app-users
>>
>
_______________________________________________
Skim-app-users mailing list
Skim-app-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-users

Reply via email to