2011/05/01

Delete the duplicate Skim notes

With the following AppleScript you can delete the Skim notes which have exactly the same text in each page of a PDF file.

--2011-05-01
-- http://organognosi.blogspot.com

tell application "Skim"
    --the following code deletes the note text which is repeated one or more times in a PDF page
    set numberOfMyPages to count pages of document 1
    repeat with currentPage from 1 to numberOfMyPages
        set numberOfPageNotes to count notes of page currentPage of document 1
        set textOfCurrentPageNotes to text of notes of page currentPage of document 1 as list
       
        repeat with currentNoteNumber from 1 to numberOfPageNotes
            set noteText to (get text for note currentNoteNumber of page currentPage of document 1) as text
            set offsetPositionsOfDuplicates to list_positions(textOfCurrentPageNotes, noteText, true) of me
            set the numberOfDuplicates to length of offsetPositionsOfDuplicates
            repeat with noteForTextDeletion from 2 to numberOfDuplicates
               
                set noteNumber to item noteForTextDeletion of offsetPositionsOfDuplicates
                delete text of note noteNumber of page currentPage of document 1
            end repeat
        end repeat
    end repeat
    --the following code deletes the empty notes   
    set numberOfMyPages to count pages of document 1
    set numberOfAlltheNotes to count notes of document 1
    set indexesOfEmptyNotes to {}
    repeat with currentNote from 1 to numberOfAlltheNotes
        set noteText to (get text for note currentNote of document 1) as text
       
        if noteText is "" then
            set the end of the indexesOfEmptyNotes to currentNote as string
            set ABC to the length of indexesOfEmptyNotes
        end if
    end repeat
   
    repeat with x from 1 to ABC
        set indexNote to item x of indexesOfEmptyNotes as number
        set indexNote to indexNote - x + 1
        delete note indexNote of document 1
    end repeat
end tell

--the following subroutine is from Soghoian's AppleScript 1-2-3 book (p. 569)
on list_positions(this_list, this_item, list_all)
    set the offsetlist to {}
    repeat with i from 1 to the count of this_list
        if item i of this_list is this_item then
            set the end of the offsetlist to i
            if list_all is false then
                return item 1 of offset_list
            end if
        end if
    end repeat
   
    if list_all is false and offsetlist is {} then return 0
    return the offsetlist
end list_positions

No comments: