Applescript find and replace text in multiples files

1.5k views Asked by At

I've multiple text files made with TextEdit in a folder. I want to find and replace a same string in all of them using AppleScript.

I'm a total newbie to AppleScript language. I've just made very simple script with it. Would you please suggest me the code?

Thanks.

1

There are 1 answers

0
Neuran On BEST ANSWER

This solved my problems.

set myFiles to (choose file with multiple selections allowed)

display dialog "Text to replace" default answer ""
   set textToReplace to text returned of result

display dialog "Text to insert" default answer ""
   set textToInsert to text returned of result

repeat with aFile in myFiles
   set myData to do shell script "cat " & quoted form of (aFile's POSIX path)
   set newData to do shell script "echo " & quoted form of myData & " | sed 's/" & textToReplace & "/" & textToInsert & "/g' > " & quoted form of (aFile's POSIX path)
end repeat