Find and replace

I am new when it comes to applescript but want to get into using it more. So if you see something that just sounds stupid sorry … I am stilling learning.

What I am trying to accomplish is this: (multipart challenge)

A.) I want a script that will open a .txt file (actually a source code from a webpage I have saved) that I choose or a drag and drop would be fine as well. Once opened I would like to have it search through find and replace certain text in the file. It will actually be looking for (src=") and replacing with (src=" I have been reading about this all day and it seems that with the special characters it becomes somewhat of a challenge.

B.) Next I need to have it search through the same file and delete certain lines in the file. I will define what line number to start deleting and where to stop deleting.

C.) Than save the file with the existing file name and close.

I am not asking someone to write this for me but more just get me pointed in the right direction. I have been searching forums all day and I have not been able to come up with anything that works. Actually I have, I have a script that will open a file of my choice and that is about all I got :slight_smile: Thanks to anyone that can lend a helping hand with this!

Look for example of

AppleScript’s text item delimiters
read file
write file

found in Standard Additions Dictionary

UNIX there more advanced option, like grep using

do shell script

you can use application like


if you don’t mind using non standard stuff

If you downloading in Safari, you can get a lot of information with do javascript. Link get links, images, names, sizes. Might make thing easier when parsing

have fun


Thanks for the heads up. I am going to try and play around with one of those programs you listed. Do you have a suggestion on which one is better (TextWrangler or BBEdit). I am open for either one and would like to know which one you think would be a better suit for me. I will keep you posted as to the progress that I make on this. Thanks again!

Both are made by Bare Bones Software. BBEdit has a few more features, but is costs $125; TextWrangler is free.

Hey Bruce, thanks for the info. I was looking into the prices of the two. At this point spending $125 is not an issue. Time is the issue and right now with what I am doing it takes a lot of time for me. Anyway that is not here nor there, would it be worth the money to get the BBEdit, it seems from what I have been reading that BBEdit has more capabilities than TextWrangler but since I am new I am not sure if what I am trying to get accomplished I would need all the extra bells and wistles so to speak.


By the way, thanks for the help. I am really becoming a big fan of this forum. It is amazing how much help you can get by just asking, makes you believe there are still great people in the world that are willing to help those that need it. :slight_smile:

tell application "TextWrangler"
	make new text document
	open {file "Macintosh Adds (txt files):find[10].txt"} with LF translation
	replace "src=\"" using "src=\"" searching in text 1 of text document "find[10].txt" options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
	save text document "find[10].txt" to file "Macintosh Adds (txt files):xxxx:find[10].txt"
	close text window 1
end tell

This is pretty much what I am trying to accomplish. My question is how do I tell it which file to open. I don’t want it to open the same file everytime, it will be a file that I choose in the same folder (not the find[10].txt). Also I would like the same script to delete certain lines of text (line 28 through 37) and a couple others. Any fun little pointers would be great. Thanks to those who pointed me in this direction … it has been a great help so far.