0

I'm using an Applescript to find-and-replace certain characters or strings of characters in BBEdit to speed up the process of ripping content out of a Microsoft Word document and wrapping it in html before it goes into a CMS. Most of this is fine when it's a replace-all type of command. However, I can't figure out how to amend the end of a line with a closing H tag.

For instance, the following line does a great job of wrapping paragraphs in P tags: replace "\\n\\n" using "</p>\\n<p>" searching in text 1 of text document 1 options {starting at top:true}

And I have a solution for adding h tags to the front of the a line. However, I can't figure out how to amend the end of a line with a closing h tag, and that's what I'm hoping to get some help with.

I'm looking for something that follows this logic: "If a line begins with <h3>, add </h3> to the end of the line." OR "When a line begins with <h3>, replace the next \\n with </h3>\\n."

If there's something that works within the Applescript (rather than a shell), that would be ideal, as I am fairly new to this and haven't taught myself anything about shells just yet.

Editgrrr
  • 3
  • 2
  • 1
    Strings are immutable, so you need to build a new one, for example `if myString begins with "

    " then set myString to myString & "/

    "`. If you are already wrapping paragraphs, what are you doing to get the newlines?

    – red_menace May 27 '22 at 00:34
  • Please do still consider shell scripts. You're new to command line, but with respect, it seems you're also new to the ugliness that is string manipulation in AppleScript. Regular expressions, sed, awk, perl, and friends are the lesser demon here, with ten-thousand times the documentation, tutorials, community, and existing problem-solving QA online. Sorry for the off-topic soapboxing, but I want you to succeed, on the easier path. – Joel Reid May 27 '22 at 12:59
  • Here's what I came up with: `tell text 1 of text document 1 set number_of_h3 to count "\\n

    " repeat while number_of_h3 > 0 set find_h3 to find "^

    (.*)" options {search mode:grep, starting at top:true, returning results:true, wrap around:true} set my_h3 to found text of find_h3 replace my_h3 using (" " & my_h3 & "

    ") options {starting at top:true, returning results:true, wrap around:true} replace "\\n\\n\\n\\n

    " using "\\n

    " options {starting at top:true, returning results:true, wrap around:true} set number_of_h3 to count "\\n

    " end repeat end tell`

    – Editgrrr May 28 '22 at 04:10
  • @red_menace (see previous note), I came up with this based on your example. It counts the number of hard return & h3 tag pairings in the manuscript and then repeats a sequence that many times. Each time it runs, the sequence adds a space between the hard return and the h3 tag so that the counter eventually drops to zero and the repeat ends. This actually works, but it spits out an error when it finishes running that reads "Can’t get found text of {found:false}." and it highlights the "found text" and won't finish running the rest of the script. Not sure how to resolve it. – Editgrrr May 28 '22 at 04:16
  • Thanks, @JoelReid. I'm definitely going to get into shells at some point. – Editgrrr May 28 '22 at 04:19

2 Answers2

0

I figured it out after one of the respondents to my original post pointed me in the right direction. (This exchange in a Google Group was also pretty helpful.)

I don't know if this is "correct" but it does what I was hoping it could do: It adds a string to the end every line that begins with something specific.

-- This tell will find h3 tags and add a closing tag to the end of the line. It adds a space before the opening h3 tag so that it won't loop forever, and then the second tell removes that extra space once the loop finishes.  
tell application "BBEdit"  
    tell text window 1  
        set loop_while_true to true  
        repeat while true  
            set find_h3 to find "^<h3>(.*)" options {search mode:grep, starting at top:loop_while_true} with selecting match  
            set loop_while_true to false  
            if found of find_h3 then  
                set my_h3 to found text of find_h3  
                replace my_h3 using (" " & my_h3 & "</h3>\\n") options {starting at top:true, returning results:true, wrap around:true}  
            else  
                exit repeat  
            end if  
        end repeat  
    end tell  
end tell  
-- this will clean up the extra space introduced during the loop sequence.  
tell text 1 of text document 1  
    replace " <h3>" using "<h3>" options {search mode:grep, starting at top:true, returning results:true, wrap around:true}  
end tell  
Editgrrr
  • 3
  • 2
0

You can solve this efficiently using BBEdit's built in regular expressions support, like so:

tell application "BBEdit"
    tell text window 1
        tell contents
            replace "^(\\<h3\\>)(.*)$" using "\\1\\2<\\\\h3>" options {search mode:grep, starting at top:true}
        end tell
    end tell
end tell

This adds closing tags for all h3 elements.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17