Load and alter external text file


I am trying to load an external text file into appleScript. The text file should have a DEFINE that I will be replacing with other values using AppleScript. I’ll will then be creating a bunch of text files from a repeat loop (which I already have working)

For example the text inside the text filet could be: “This is my text with a DEFINE which needs to be change”

I would like to load the above text into AppleScript, find the work DEFINE and replace it with any words (of undefined length).

Any insights on how to do that?

Thank you in advance.


You’ll probably need to use another application, but it sounds like you just need to run a Find/Replace command on the word DEFINE.

If you really don’t want to use another application, I guess you could iterate through the words of the text, and anytime the word equals define, set it to the desired word.

repeat with i from 1 to count words of text
if word i="DEFINE" then
  set word i to myWord
end if
end repeat

the code isn’t exact, and will probably not compile, but it gives the basic idea of whats going on. Play with it, and get it to work

… Some handlers here:

I’ve used SearchReplaceText and it works great!

Thanks a lot.