Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top