BBEdit AppleScript 用于重新格式化多个文件
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您应该使用文本工厂功能。在“文件>新建>文本工厂”下。您可以对单个文件执行的任何操作都可以对任意数量的文件执行,并且您可以保存操作以供将来使用。
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.
你差不多已经明白了;诀窍是您要循环访问
open
返回的文件。因此,您需要这样的东西:如您所见,您需要做的就是将格式化代码放入此循环中(并且不要忘记保存文件!);循环将依次将
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: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 listdocs
in turn, and run the body with that element. If you're not sure how to select the files, one way ischoose 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 replaceLIST_OF_FILES
with(choose file with multiple selections allowed)
.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?