2

在 bbedit 中有一个功能,您可以选择文本,从菜单中选择 text->change case->make title case,它将相应地采取行动。

有没有办法在项目中的文件中选择多个文本字符串,然后应用相同的文本格式?

我知道你可以做一些正则表达式的东西并改变它,但这些都不是真正的标题案例,它忽略了“of”“和”“the”等词。标题案例很好,我只需要这样做在许多项目上。

例如 5 个 html 文件有- 所以<h2>THIS IS THE TITLE</h2> 现在我去每个文件选择文本并执行上面的菜单项。如果有 5 个,那很好,但如果我想制作 2500 个<h2>This is the Title</h2>- 那么我需要能够一次选择多个....

提前致谢!

------编辑

因此,如果您在多个文件中搜索所有<h2>标签,并且您在不同文件中返回了几个......

<h2>MY TITLE</h2>
<h2>this is a title</h2>
<h2>Another title</h2>

标题案例将相应地将它们中的每一个更改为:

<h2>My Title</h2>
<h2>This is a Title</h2>
<h2>Another Title</h2>

目前,您可以通过菜单单独选择每一个。如果有意义的话,我们希望通过查找所有内容并更改案例来做到这一点......

F:<h2>(.*?)</h2> R:<h2>\1</h2>[把这个当作一个特定的案例]

谢谢。

4

1 回答 1

1

几乎所有在 BBEdit 中涉及重复的操作都可以使用 AppleScript 自动化。:-)

这是将执行您描述的操作的 AppleScript 脚本的文本。您可以将其复制并粘贴到 AppleScript 编辑器中,并将其保存在 BBEdit 的“Scripts”文件夹中,以备将来根据需要使用。

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "BBEdit"

    --  make sure we start at the top, because searches will (by default) proceed from the end of the selection range.
    tell text of document 1 to select insertion point before first character

    repeat
        tell text of document 1
            -- Find matches for a string inside of heading tags (any level)
            -- NOTE extra backslashes in the search pattern to keep AppleScript happy
            set aSearchResult to find "(<(h\\d)>)(.+?)(</\\2>)" options {search mode:grep}
        end tell

        if (not found of aSearchResult) then
            exit repeat -- we're done
        end if

        -- the opening tag is the first capture group. We'll use this below
        set openingTagText to grep substitution of "\\1"

        -- the title is the third capture group
        set titleText to grep substitution of "\\3"

        -- use "change case" to titlecase the title
        set changedTitleText to change case (titleText as string) making title case

        -- select the range of text containing the title, so that we can replace it

        set rangeStart to (characterOffset of found object of aSearchResult) + (length of openingTagText)
        set rangeEnd to (rangeStart + (length of changedTitleText) - 1)

        select (characters rangeStart through rangeEnd of text of document 1)

        -- replace the range
        set text of selection to changedTitleText
        --      select found object of aSearchResult
    end repeat

    --  put the insertion point back at the top, because it's a nice thing to do
    tell text of document 1 to select insertion point before first character

end tell
于 2020-03-04T23:21:05.230 回答