Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top