Search and Replace in TextEdit

Hi, and thanks to everyone for this forum. I am always amazed by the generousity of Mac users.
Here’s what I want to do;
I have text for a surveilance system at work that lists numbers of transmitters used to open up our front gate. I would like to be able to replace those numbers with the corresponding users.
Seems simple enough, but as I said, I don’t have a lot of experience. Any help would be appreciated.
Thanks, Matt

Edit- So I should probably show you what I have.
tell application “TextEdit”
set gate_File to (path to desktop as Unicode text) & “Event Log Report.txt”
set fRef to (open for access file gate_File with write permission)
open gate_File
set file_List to (every character of file gate_File as list)

end tell

After this, I’m not sure how to find and replace those numbers.

Hi Matt,

welcome to MacSripter

To parse a plain text file, TextEdit is not necessary. AppleScript has a few skills to access text files directly.
Can you post an example, what kind of strings you want to search and replace?

Thank you for the reply. This text comes from a windows application that I have copied and pasted into TextEdit.
Example:

Time Date Event Action Gate
------------------------------------------------------------------------------------------------07:43:32 05/28/07 Remote Access Programming Mode
07:39:12 05/28/07 Console Exit Programming Mode
07:37:24 05/28/07 Remote Access Programming Mode
07:16:28 05/28/07 Console Exit Programming Mode
07:14:40 05/28/07 Remote Access Programming Mode
06:56:16 05/28/07 Block Transmitter 11047 Open Channel [A]
06:56:12 05/28/07 Block Transmitter 11047 Open Channel [A]
06:56:12 05/28/07 Block Transmitter 11047 Open Channel [A]
06:43:52 05/28/07 Block Card 193 Open Channel [A]

So for example the “Block Transmitter 11047” I would like to read “Block Transmitter John Doe”

try this


(*
	set T to "07:37:24 05/28/07  Remote Access Programming Mode                                                     
	07:16:28 05/28/07  Console Exit Programming Mode                                                      
	07:14:40 05/28/07  Remote Access Programming Mode                                                     
	06:56:16 05/28/07  Block Transmitter 11047                 Open   Channel [A]                         
	06:56:12 05/28/07  Block Transmitter 11047                 Open   Channel [A]                         
	06:56:12 05/28/07  Block Transmitter 11047                 Open   Channel [A]                         
	06:43:52 05/28/07  Block Card 193                          Open   Channel [A]"
*)

set f to choose file
set ff to open for access f with write permission
set T to read f

set {TID, text item delimiters} to {text item delimiters, "11047"}
set T to text items of T
set text item delimiters to "John Doe"
set T to T as string
set text item delimiters to TID

write T to ff
close access ff

Thank you Stefan,
While that did work for the 11047, how do I get another name to replace the “193” of the last line? I have to keep track of something like 400 names/numbers, so I am wondering if there is a way to maintain a list and have it read the delimiters from that list. Also, I tried to add a tell command to get TextEdit to open the manipulated file, but kept getting error.
I really appreciate your help,
Matt

For TextEdit the syntax is wrong (TextEdit doesn’t understand open for access)

The repeat loop to process a list can be like this

set searchList to {"11047", "193"}
set replaceList to {"John Doe", "James Smith"}

set f to choose file
set ff to open for access f with write permission
set T to read f

set TID to text item delimiters
repeat with i from 1 to (count searchList)
	set text item delimiters to item i of searchList
	set T to text items of T
	set text item delimiters to item i of replaceList
	set T to T as string
end repeat
set text item delimiters to TID

write T to ff starting at 0
close access ff

but you can also read an other text oder Excel file with the values

Note: I forgot starting at 0, otherwise the text will be appended

That Works!
Thanks Stefan. :smiley:

Stefan,
Could you explain this part of the script
“set T to text items of T”
and then
“set T to T as string”

I’m not sure what you are doing there.
Thanks,
Matt

Of course. It depends on the settings of text item delimiters

For example you have the text

set T to "Hi, AppleScript is great"

setting text item delimiters to “space” and the line

set T to text items of T

you get a list {“Hi,”, “AppleScript”, “is”, “great”}, it splits the text into single text items separated by text item delimiter (space character)
setting text item delimiters to e.g. “foo” and the line

set T to T as string

the result is:
“Hi,fooAppleScriptfooisfoogreat”, it concatenates the list into a string with text item delimiter in between (“foo”)

Thank you, I have a lot to learn.
Matt

Hi Matt;

There are a lot of helpful tutorials on this site: visit http://macscripter/unscripted to see them. In particular, for using applescript’s text item delimiters, see: Tutorial for Using AppleScript’s Text Item Delimiters.

Hi Adam,
missed a bit in your link.

http://macscripter.net/unscripted :slight_smile:

Oops :rolleyes: