Search-replace text

Hi,
I want to replace some text in a xml file. here is a part of the text:

two values should be changed. I have found this in the “code exchange” forum:

(*
searchReplaceText(searchTerm, replaceTerm, theText)
Replaces a string found in a text by another string.
This handle supports lists of search/replace terms.

Parameters:
searchTerm: the text to search for
replaceTerm: the text to use as replacement
theText: the text to search/replace

Examples:
searchReplaceText("foo", "bar", "You are a foo") --> "You are a bar"
searchReplaceText({"foo", " a "}, {"bar", " one "}, "You are a foo") --> "You are one bar"
*)

to searchReplaceText(searchTerm, replaceTerm, theText)
	set searchTerm to searchTerm as list
	set replaceTerm to replaceTerm as list
	set theText to theText as text
	
	set oldTID to AppleScript's text item delimiters
	repeat with i from 1 to count searchTerm
		set AppleScript's text item delimiters to searchTerm's item i
		set theText to theText's text items
		set AppleScript's text item delimiters to replaceTerm's item i
		set theText to theText as text
	end repeat
	set AppleScript's text item delimiters to oldTID
	
	return theText
end searchReplaceText

my script looks like this:

set xyFolder to choose folder with prompt "Please choose your xyFolder folder. It´s in your user folder by default."

set SearchText to {"<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"20\"/>", "<Entry Name=\"Transport.Synchronize.Start\" Type=\"0\" Value=\"1\"/>"} as list
set replaceText to {"<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"-1\"/>", "<Entry Name=\"Transport.Synchronize.Start\" Type=\"0\" Value=\"0\"/>"}


tell application "Finder"
	
	set thePathToSettingsFile to xyFolder & "name of the file" as text
	
	set theSettingsFileRefNum to open for access file thePathToSettingsFile with write permission
	
	set theText to read theSettingsFileRefNum
	
	close access theSettingsFileRefNum
	my searchReplaceText(SearchText, replaceText, theText)
	
	log theText
	
end tell

--_____________________________________

-- FIND AND REPLACE TEXT 
on searchReplaceText(SearchText, replaceText, theText)
	tell application "TextEdit"
		set SearchText to SearchText as list
		set replaceText to replaceText as list
		set theText to theText as text
		
		set oldTID to AppleScript's text item delimiters
		repeat with i from 1 to count SearchText
			set AppleScript's text item delimiters to SearchText's item i
			set theText to theText's text items
			set AppleScript's text item delimiters to replaceText's item i
			set theText to theText as text
		end repeat
		set AppleScript's text item delimiters to oldTID
		
		return theText
		
	end tell
end searchReplaceText

I don´t get an error message but no text is replaced. I guess the confusion is about the “”" in the text.

Any ideas?

Several points, airbuff;

  1. You don’t have to open a file to read it: set theText to read alias ((xyFolder as text) & “name of file”) will do it.

  2. If the “name of the file” is an rtf file, this won’t work because you will read in all the formatting info. Needs to be a text file.

  3. set theText to my searchReplaceText(SearchText, replaceText, theText) is required to change the value of theText.

Thanx for the replai.

1) You don’t have to open a file to read it: set theText to read alias ((xyFolder as text) & “name of file”) will do it.
Good to know. I need to write the replaced text back in this file. I will open it later, though.

2) If the “name of the file” is an rtf file, this won’t work because you will read in all the formatting info. Needs to be a text file.
It´s an .xml file.

3) set theText to my searchReplaceText(SearchText, replaceText, theText) is required to change the value of theText.
This makes sense and works!:smiley:

Glad it works for you now. An xml file is plain text, so that’s fine.

Hi Adam,

thank you again. To be true, I´m very unshure in this forum for some reason. I do not use forums normally and I´m new to AS. If I don´t get an answer for some while, I always think: “maybe I have asked the wrong way in the wrong part of a forum. maybe I´m to stupid to find answers…”. And again the answer has been very obvious.

Anyway.

Here is my final script. Perhaps it helps some ather newbie:


set theFolder to choose folder with prompt "Please choose your theFolder folder." & return & "It´s in your user folder by default."

set SearchText to {"<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"20\"/>", "<Entry Name=\"Transport.Synchronize.Start\" Type=\"0\" Value=\"1\"/>"}
set replaceText to {"<Entry Name=\"Transport.Loops.AutodetectSize\" Type=\"1\" Value=\"-1\"/>", "<Entry Name=\"Transport.Synchronize.Start\" Type=\"0\" Value=\"0\"/>"}

tell application "Finder"
	set thePathToSettingsFile to theFolder & "theNameOfTheFile.xml" as text
	
	set theSettingsFileRefNum to open for access file thePathToSettingsFile with write permission
	
	set theText to read theSettingsFileRefNum
	
	set theText to my searchReplaceText(SearchText, replaceText, theText)
	
	write theText to theSettingsFileRefNum starting at 0
	close access theSettingsFileRefNum
end tell

--_____________________________________

-- FIND AND REPLACE TEXT 
on searchReplaceText(SearchText, replaceText, theText)
	set theText to theText as text
	
	set oldTID to AppleScript's text item delimiters
	
	repeat with i from 1 to count SearchText
		set AppleScript's text item delimiters to SearchText's item i
		set theText to theText's text items
		set AppleScript's text item delimiters to replaceText's item i
		set theText to theText as text
	end repeat
	
	set AppleScript's text item delimiters to oldTID
	
	return theText
end searchReplaceText

Beside my doubts, I will keep on asking in this forum. :smiley:

You’re welcome, Airbuff. Don’t be shy about asking here; one of the things about the bbs we take some pride in that we don’t trash each other. We all had to learn sometime, and it was only a few years ago that I was the noob asking questions with answers that are obvious to me now.

Adam