Find and Replace

I am just changing text = Change > "Contents" to "Introduction"

OK Changed the results to = true.

Got the original message about change one file then this error message

Got this error message: ~/Desktop/Report 301, Web Project: Operation not permitted by the OS. -- Checked privacy and BBEdit has full disk access.

Sorry BBEdit did not have full disk access so changed it and it is now working.

Ran BBEdit over same file it made 27273 changes

Roger

Nige

Just a great thanks. I changed the macro to set the variables and added 2 additional apple scripts so that it changes all 3 of my text to what I need. The whole procedure runs in less than 2 minutes. Don't have to worry about misspelling any of the search's or replaces so took out the backup. (I can recreate the file again if needed.)

Roger

Shame on you! Run, don't walk, back to KM and delete those obnoxious clones then make it so you loop over the original (and best!) AppleScript using different variables!

Harrumph...

More seriously -- looping over the same AppleScript gives you only one script to maintain and, done right, can make the macro more versatile (it's just as easy to do two or 10 changes as it is to do three). Things do change, and it'll make your life easier in the future. You can even set different BBEdit's S'n'R options per loop by passing those in as variables too!

Here's a simple example that uses an "infinite loop" that breaks when the prompt's "Search" field is left blank:

Roger Looping S'n'R with BBEdit.kmmacros (5.7 KB)

Image

OK I understand.

Question - Which is better too use the loop or to break it apart so the loop is a completely separate macro? Then have the main macro set the variables and then call the loop macro?

There's more overhead (both for KM and for your brain!) when using a separate macro/subroutine. In general, if you use the same sequence of actions in more than one macro then consider separating those actions out into another macro/subroutine. You can also do that if you use the same sequence in more than one place in the same macro, but you can often rearrange your logic to use a loop instead -- ie rather than "get a thing, do something, get a thing, do something..." you can "get all the things, for each thing do something".

The counterpoint is that sub-macros/subroutines are often specific. If you find yourself passing a dozen parameters every time you want to use it, it probably wasn't specific enough to be spun out.

Ultimately it's a personal choice as to where you strike the balance between reusability and usability. To be honest, I'd usually have done the same as you and repeated the AS action for each search because it fits more neatly with the mental model of "search and replace for this, then search and replace for that..." and it'll be easier to understand in a year's time. But for some reason I'm seeing loops everywhere at the moment...