Re: Find and Replace Text
Re: Find and Replace Text
- Subject: Re: Find and Replace Text
- From: "Eric Phillips" <email@hidden>
- Date: Fri, 26 Apr 2002 09:25:36 -0400
I wrote asking how to replace ', number' with 'TabNumber' and Marc responded with the following:
This should do the trick:
property srchStrings : {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
set theFile to (choose file with prompt "Pick a text file:" of type {"TEXT"})
set fileID to (open for access theFile)
try
set theText to read fileID
close access fileID
on error m number n
try
close access fileID
end try
error m number n
end try
repeat with aString in srchStrings
set theText to textReplace(theText, (" " & aString), tab)
end repeat
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 <email@hidden>
http://AppleScriptsToGo.com
4020 W.220th St.
Fairview Park, OH 44126
(440) 331-1074
I modified the script slightly as follows. I could not get the 'tell (a reference to Applescript's text item delimiters)' to work. I am assuming the reference was used to speed up the script. My list is shorter than 100 items so it only takes a few seconds for the script to run without using the reference. The question comes with now what do I do with the result? I would like to get the result into Exel. If I could do this without having to save it first that would be great but I couldn't find a way to get the information from the variable into Excel. I then tried to write the information in theText to a file. When I did this I got an error saying that the file was already open and then I would get an empty text file in that location.
property srchStrings : {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
set SaveFile to "a disk:with a folder:containing this folder:filename.txt"
set theFile to (choose file with prompt "Pick a text file:" of type {"TEXT"})
set fileID to (open for access theFile)
try
set theText to read fileID
close access fileID
on error m number n
try
close access fileID
end try
error m number n
end try
repeat with aString in srchStrings
set theText to textReplace(theText, (", " & aString), tab)
end repeat
try
set x to (openfor access SaveFile with write permission)
write theText to x
close access x
try
close access fileID
end try
error m number n
end try
on textReplace(theText, srchStrng, replStrng)
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {srchStrng}}
try
set {textList, AppleScript's text item delimiters} to {(text items of theText), {replStrng}}
set {newText, AppleScript's text item delimiters} to {(textList as text), od}
return item 1 of result
on error errMsg number errNbr
set AppleScript's text item delimiters to od
error errMsg number errNbr
end try
end textReplace
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.