BBEdit AppleScript 用于重新格式化多个文件

发布于 2024-11-26 16:07:30 字数 379 浏览 1 评论 0原文

我正在编写一个 Applescript,它将利用 BBEdit - 循环遍历大量 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 文件有任何建议吗?

I'm looking to write an Applescript which will utilize BBEdit — loop through a ton of HTML files and autoformat them (so the indentation is easier to read).

So far I have:

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

This applies the transformation to a single file, but has anyone got any suggestions how to apply this to an unknown number of HTML files?

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

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

发布评论

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

评论(3

脱离于你 2024-12-03 16:07:31

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

You should use the Text Factory feature for this. Under "File > New > Text Factory". Any operation you can perform on a single file can be done on any number of files and you can save the operation for future use.

飘然心甜 2024-12-03 16:07:30

你差不多已经明白了;诀窍是您要循环访问 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 中的每个元素,并使用该元素运行主体。如果您不确定如何选择文件,一种方法是选择允许多项选择的文件;这将弹出一个对话框,您可以选择任意数量的文件。要使用它,只需将 LIST_OF_FILES 替换为 (选择允许多个选择的文件)

You've almost got it; the trick is that you want to loop through the files returned by open. Thus, you need something like this:

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

As you can see, all you need to do is place your formatting code inside this loop (and don't forget to save the files!); the loop will set doc to each element of the list docs in turn, and run the body with that element. If you're not sure how to select the files, one way is choose file with multiple selections allowed; this will pop up a dialog box which will allow you to select as many files as you want. To use it, just replace LIST_OF_FILES with (choose file with multiple selections allowed).

春风十里 2024-12-03 16:07:30

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

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

这有帮助吗?

BBEdit will perform a find/replace on any group of files you want. Just hit command+shift+f to bring up Multi-File Search instead of the basic find/replace window.

If you have more than one set of find/replace commands that you need to execute at the same time, you need a Text Factory. See here for details on how to set one up: http://www.barebones.com/products/bbedit/benefitsexercise.html

Does that help?

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