2

我正在寻找一个使用 BBEdit 的 Applescript——遍历大量 HTML 文件并自动格式化它们(这样缩进更容易阅读)。

到目前为止,我有:

tell application "BBEdit"
    activate
    open {file "Macintosh HD:TEST DIRECTORY:testfile copy 2.html"} with LF translation
    (format mode hierarchical)
    beep 
    display alert "Finished!"
end tell

这会将转换应用于单个文件,但是有人对如何将其应用于未知数量的 HTML 文件有任何建议吗?

4

3 回答 3

3

你几乎明白了;诀窍是您要遍历open. 因此,您需要这样的东西:

tell application "BBEdit"
    set docs to open LIST_OF_FILES with LF translation
    repeat with doc in docs
        -- format doc
        save doc
    end repeat
    beep -- Or even `say "Finished!" without waiting until completion`
         -- if you want your computer to talk to you
    display alert "Finished!"
end tell

如您所见,您需要做的就是将格式化代码放入此循环中(并且不要忘记保存文件!);循环将依次设置doc到列表的每个元素,并使用该元素运行主体。docs如果您不确定如何选择文件,一种方法是choose file with multiple selections allowed;这将弹出一个对话框,允许您选择任意数量的文件。要使用它,只需替换LIST_OF_FILES(choose file with multiple selections allowed).

于 2011-07-26T02:54:52.940 回答
0

BBEdit 将对您想要的任何文件组执行查找/替换。只需按 command+shift+f 即可调出多文件搜索,而不是基本的查找/替换窗口。

如果您需要同时执行一组以上的查找/替换命令,则需要一个文本工厂。有关如何设置的详细信息,请参见此处:http ://www.barebones.com/products/bbedit/benefitsexercise.html

这有帮助吗?

于 2012-01-06T01:40:56.333 回答
0

您应该为此使用文本工厂功能。在“文件>新建>文本工厂”下。您可以对单个文件执行的任何操作都可以对任意数量的文件执行,并且您可以保存操作以供将来使用。

于 2015-03-26T13:50:47.850 回答