applescripts。如何在文本文档中的某些行末尾添加text_object/code片段?

发布于 2025-02-01 17:48:26 字数 593 浏览 4 评论 0原文

我正在使用AppleScript来查找某些字符或BBEDIT中的某些字符或字符串,以加快从Microsoft Word文档中剥离内容的过程,然后将其包裹在HTML中,然后再将其包装到CMS中。当它是替换类型的命令时,大多数都很好。但是,我不知道如何用关闭的H标签修改一条线的末端。

例如,以下行在p标签中包装段落做得很好: 使用“</p> \\ n< p>”替换“ \\ n \\ n”“”在文本文档1选项的文本1中搜索{从顶部开始:true}

,我有一个解决方案将H标签添加到A行的正面。但是,我不知道如何用关闭的H标签修改一条线的结束,这就是我希望获得一些帮助的方法。

我正在寻找遵循此逻辑的内容:“如果一行以< h3>开头,则将</h3>添加到行的末尾。”或“当行以< h3>开头时,将下一个\\ n替换为</h3> \\ n。”

如果在Applescript中有些可行(而不是外壳),那将是理想的选择,因为我对此是相当陌生的,并且还没有对自己的贝壳进行过任何知识。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笔绘流年 2025-02-08 17:48:26

您可以使用BBEDIT内置的正则表达式支持来有效地解决此问题,例如:

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

这为所有H3元素添加了关闭标签。

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\\>)(.*)
quot; 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.

千柳 2025-02-08 17:48:26

在我的原始帖子中的一位受访者指出我朝正确的方向指出之后,我弄清楚了。 很有帮助。

Google组中的此交换也 t知道这是否是“正确的”,但它可以做我希望它能做的事情:它在每条线以特定的东西开头的每一行中添加了一个字符串。

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

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  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文