Using TextEdit find and replace url links in a HTML file

I’m struggling to find the right solution to this problem.

What I would like to achieve is to open a html file in TextEdit and do a find and replace on the following:

src=" replace with scr=“http://www.mysite.com
href=” replace with href=“http://www.mysite.com
<img src=” replace with <img src="http://www.mysite.com

then save as a new file.

I have found a great script that does work, but as soon as I use the " eg: scr="
it fails. Does anyone have a solution to this, I would really appreciate the help!

Thanks
James

property search_and_replace_strings : {{"src=", "src=http://www.mysite.com"}, {"search term 2", "replacement term 2"}}
set the_file to (choose file with prompt "Select your text file:")
set the_text to (read the_file)
repeat with i in search_and_replace_strings
	set the_text to my snr(the_text, i's item 1, i's item 2)
end repeat
my write_to_file(the_file, the_text, false)
beep 2
display dialog "Finished!" buttons {"OK"} default button 1 with icon 1 giving up after 5

on snr(the_string, search_string, replace_string)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, search_string}
		set {the_string, contents} to {the_string's text items, replace_string}
		set {the_string, contents} to {the_string as Unicode text, old_tid}
	end tell
	return the_string
end snr

on write_to_file(the_file, the_data, with_appending)
	set the_file to the_file as Unicode text
	try
		set f to open for access file the_file with write permission
		if not with_appending then set eof of f to 0
		write the_data to f starting at eof as (class of the_data)
		close access f
		return true
	on error the_error
		try
			close access file the_file
		end try
		return the_error
	end try
end write_to_file

Try this:

property search_and_replace_strings : {{"src=\"", "src=http://www.mysite.com"}, {"search term 2", "replacement term 2"}}

Hi,

the problem is, src is always a subset of img src, therefore a lot of strings will be replaced twice.
Either it’s sufficient only to replace after src or you need a unique way to distinguish both forms

Hi Guys,

Thanks for your help on this. I did a lot more test and discuvered that if I use any of the following then it fails.

 " . = 

So I think I do need a unique way to distinguish both forms, any ideas?

James,

If I were you I would forget text edit as you can do this with straight AppleScript and it will probably be faster. The key is to use text item delimiter’s and coercion to act as your search and replace and use AppleScripts built in read/write functions to get the file’s text and write the result back to it. Also be sure to escape your quotes (") if you are wanting that to be part of the text you are searching for. Note I took Stefan’s advice and eliminated the redundant img src search item.

set SearchDelim to {"src=\"", "href=\""}
set ReplaceDelim to {"scr=\"http://www.mysite.com", "href=\"http://www.mysite.com"}

set OldDelim to AppleScript's text item delimiters

--select the text, might work best to replace with a drag and drop handlet for this script.
set TheFile to choose file with prompt "Choose File to search/replce."

try --error handliing to make sure that the file is closed if there is a problem.
	open for access TheFile with write permission
	set TheText to read TheFile
	repeat with i from 1 to count of SearchDelim
		set AppleScript's text item delimiters to item i of SearchDelim
		--search items used as text item delimiters, line below returns a list of text between instances of the list item but not the list item's text.
		set TextList to every text item of TheText
		set AppleScript's text item delimiters to item i of ReplaceDelim
		--replace items set up as text item delimiters which are inserted inbetween the list items returned above when it is coerced into a string below.
		set TheText to TextList as string
	end repeat
	set eof TheFile to 0 --make sure we are writing to the beginning of the file
	write TheText to TheFile
	close access TheFile
on error
	close access TheFile
end try

set AppleScript's text item delimiters to OldDelim

with a few lines more you could turn this into a drag and drop which could process on 1 or 100 files.

Hi Jerome

I’ll try some of your suggestions and keep you posted.
And a big thanks to you all for your help!

Cheers
James

Just a quick update, after the help from Jerome, I have now updated the script to become a drag and drop application, stop duplicates, save, show in safari and show source.

For the newbies don’t forget to save the file format for the script as application.

I know there is room for improvement, but hopefully it will help anyone wanting to search and replace in a html file or text.


on open TheFile
	log "open"
	log TheFile
	open for access TheFile with write permission
	log "process " & TheFile
	set TheText to read TheFile
	repeat with i from 1 to the count of TheFile
		set this_item to (item i of TheFile)
		set the item_info to info for this_item
		
		--_______________________________________________________ Search for
		
		set SearchDelim to {"src=\"Scripts", "<img src=\"", "print\" href=\"", "\" src=\""}
		
		--_______________________________________________________ Replace with
		
		
		set ReplaceDelim to {"scr=\"http://www.yoursite.com/Scripts", "<img src=\"http://www.yoursite.com/", "print\" href\"http://www.yoursite.com/", "\" src=\"http://www.yoursite.com/"}
		
		--_______________________________________________________
		
		set checkList to (ReplaceDelim)
		checkList
		set OldDelim to AppleScript's text item delimiters
		repeat with i from 1 to count of checkList
			set AppleScript's text item delimiters to item i of checkList
			set TextList to every text item of TheText
			set AppleScript's text item delimiters to item i of SearchDelim
			set TheText to TextList as string
		end repeat
		
		repeat with i from 1 to count of SearchDelim
			set AppleScript's text item delimiters to item i of SearchDelim --search items used as text item delimiters, line below returns a list of text between instances of the list item but not the list item's text.
			set TextList to every text item of TheText
			set AppleScript's text item delimiters to item i of ReplaceDelim --replace items set up as text item delimiters which are inserted inbetween the list items returned above when it is coerced into a string below.
			set TheText to TextList as string
			
		end repeat
		
		display dialog "Congratulations - Your file has been processed" buttons {"OK"} default button 1 with icon note
		set eof TheFile to 0 --make sure we are writing to the beginning of the file
		write TheText to TheFile
		close access TheFile
		activate application "Safari"
		tell application "Safari"
			open (TheFile)
		end tell
		activate application "Safari"
		tell application "System Events" to tell process "Safari"
			delay 0.5
			keystroke "u" using {command down, option down}
		end tell
		tell application "System Events"
		end tell
	end repeat
end open