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?