0

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:)

1 Answers1

1

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
jackjr300
  • 7,111
  • 2
  • 15
  • 25
  • Great! I tried it just now and it works perfectly. Thanks for writing and commenting it in a way that's easily adaptable for my needs. – Simon Lindén Feb 23 '16 at 21:28
  • Perhaps you can help me with another thing that I couldn't fix with grep? For the most part theres only one footnote per verse and therefore I decided to name each link after it chapter and verse, like this:
  • . However, occasionaly there will be doubles (or more) of these id's. I thought I'd fix it manually because I couldn't figure it out myself. But ideally I'd want a script that goes through the id's, and if there are more than one with the same name, it adds a suffix. ("footnote-10:2-1", "footnote-10:2-2" and so on...). Is this easily done?
  • – Simon Lindén Feb 23 '16 at 21:35
  • Thanks a thousand times! This works perfectly and would have taken me long to figure out! – Simon Lindén Feb 24 '16 at 09:40