grepapplescripttextwrangler

Moving snippets of text to end of document


I'm converting a a book in XML-format to EPUB. It has 67 chapters and more than a thousand footnotes. I've found that a good way to create footnotes in Epub is by moving the contents of the notes to a list at the end and linking back and forth between the note caller and the element at the end. I've used Grep searches in Textwrangler to change the tags into proper html-tags. However I can't think of a way to use Grep to find a snippet of text and move it to the end of the document? Are there any other simple ways to do this using Textwrangler or another text editor? I assume Applescript can link with Textwrangler to do it for me (I'm using OS X) but I'm not sure how. I'm no programmer so I'd prefer as simple a solution as possible, as long as its not manual cut and paste:)


Solution

  • This script use a grep pattern to find a string and append it to a new line at the end of the document

    tell application "TextWrangler"
        tell text of window 1
            set cL to count lines
            select first line -- to start at the top
            repeat
                -- start from the selection, selection change when the "find" command found the search pattern in the document
                set r to find "[\\d]+" options {search mode:grep} with selecting match -- change "[\\d]+" to your search pattern 
                if found of r then
                    if startLine of found object of r > cL then exit repeat -- the end of the original document, to not search in the appended lines
                    set contents of found object of r to "" -- delete the found text
                    make line with data found text of r -- append a new line + the found text
                else
                    exit repeat
                end if
            end repeat
        end tell
    end tell
    

    Important: In the AppleScript script, you must escape the backslash in the pattern.

    Example: this pattern [\d]*\t from TextWrangler must be [\\d]*\\t in AppleScript


    Updated for the another question.

    This script search <li id="......", if it is not unique then it add a suffix --> - and an integer.

    set uniq_ID_names to {}
    tell application "TextWrangler"
        tell text of window 1
            select first line -- to start at the top
            repeat
                -- start from the selection, selection change when the "find" command found the search pattern in the document
                set r to find "<li id=\"[^\"]+" options {search mode:grep} with selecting match -- get character from <li id=" until the next double quote character
                if not found of r then exit repeat
                set t to found text of r
                if t is in uniq_ID_names then -- same ID's name
                    set i to 1
                    repeat -- add suffix to the found text
                        set t2 to t & "-" & i
                        if t2 is not in uniq_ID_names then
                            add suffix (found object of r) suffix ("-" & i)
                            set t to t2
                            exit repeat
                        end if
                        set i to i + 1
                    end repeat
                end if
                set end of uniq_ID_names to t
            end repeat
        end tell
    end tell