Help with replacing text

hi all,
i think i need an applescript addition for this, but i know little or nothing about applescripting and need some help. i need to replace several lines of text within a text document for an installer. basically all i want is a “find” and “replace” type thing. however, simpletext (yes, i need it for os9) doesn’t seem to be even scriptable. i know this shouldn’t be all that hard, so some help would be greatly appreciated. please be aware that i’m distributing this, so i can’t bundle in software unless it’s freeware (and small, too! i can’t have an extra 2mgs…). feel free to post script :slight_smile: thanks a ton,
-Aaron (Blobbo)

This should give you what you want

set theFile to (choose file with prompt "Select the file to modify:")
set fileID to (open for access theFile with write permission)
try
        set theText to (read theFile)
        set theText to textReplace(theText, "a", "z")
        set eof theFile to 0
        write theText to theFile starting at eof
        close access theFile
on error m number n
        try
                close access theFile
        end try
        display dialog "Error #" & (n as text) & return & m
end try

on textReplace(theText, srchStrng, replStrng)
        tell (a reference to AppleScript's text item delimiters)
                set {od, contents} to {contents, {srchStrng}}
                try
                        set {textList, contents} to {(text items of theText), {replStrng}}
                        set {newText, contents} to {(textList as text), od}
                        return item 1 of result
                on error errMsg number errNbr
                        set contents to od
                        error errMsg number errNbr
                end try
        end tell
end textReplace

Marc K. Myers

Does the SimpleText document have any styled text (colors, bold, etc)? If
not, the existing text file could be read into a script, manipulated and then
a new text file could be created in place of the old one.
Rob J

Well that’s nice to know! Thanks Marc! Rob (who wonders where the other reply to this question disappeared to)

I just tried it with a 1600 byte delimiter and it didn’t hiccup or slow down.

Mark Myers