Find and replace in text edit

I have been trying to write a script that will turn hexadecimal into plain text. I have been using textedit and am having trouble. I have been fooling around with the following but to no avail.

set every word where it is "’" to "'"

Am i using the wrong class or does anyone have any suggestions?

Many thanks
Mungo

Hi,

Your reference is probably not complete:

tell application “TextEdit”
launch
activate
make new document at front with properties {name:“Test Document”}
set text of front document to “The rain in Spain falls mainly in the plain.”
delay 1
set every word of front document whose it is “in” to “test”
end tell

gl,

If you are interested, there are lots of already-written routines to convert to/from hex/ascii. Eg:

Thanks for the script, but i can’t seem to get it to do what i want it to do. I’ll outline the entire problem i am faced with.

I need a quick and easy way to convert a lot of text from unicode to plain text. I have come up with a way of converting it to a hexidecimal format so all the apostrophes get turned into & #8217; using javascript. I then need a way to turn this into normal plain text so it is searchable in xml.

As you can probably tell, i am fairly new to applescript, but if there is a quick script to do all of that, and you know where i can find it, it would be much appreciated.

The script that i have come up with so far is:


display dialog "Paste the text here" default answer ("here") buttons {"OK"} default button 1

set the this_text to the text returned of the result

tell application "TextEdit"
	activate
	close every document saving no
	make new document at the beginning of documents
	set the name of window 1 to "Converted Text"
	
	set the text of the front document to ¬
		this_text
	tell the text of the front document
		
		set the size to 12
		
		set every word where it contains "& #8217;" to "'"
		
	end tell
end tell

Many thanks
Mungo

Then you can use and/or adaptate one of these routines:
http://www.macscripter.net/exchange/comments.php?id=313_0_1_0_C
http://www.macscripter.net/exchange/comments.php?id=311_0_1_0_C
The last one is for UTF-8, but seems that you need better the first one, for UTF-16, “Unicode text”.

Or, much simpler, if you have a plain Unicode file (that is, a file where two or more bytes represent a single character), you can simply:

read file "path:to:unicodefile.txt" as Unicode text

Unfortunately this doesn’t help me as I don’t know enough applescript to understand (and therefore adapt) the code.

I have started coming at the problem from a different approach, trying to sort it all out in applescript, but it as yet hasn’t worked.

Any ideas why?

display dialog "Paste the text here" default answer ("here") buttons {"OK"} default button 1

set the this_text to the text returned of the result as string


on AsText(str)
	--coerce Unicode or other text to styled string to plain text  
	try
		return (((str as string) as record)'s «class ktxt»)
	end try
	return str -- if plain text to begin with  
end AsText

tell application "TextEdit"
	activate
	close every document saving no
	make new document at the beginning of documents
	set the name of window 1 to "Converted Text"
	
	set the text of the front document to my AsText(this_text)
	
end tell


Many thanks for your continued suggestions

Mungo

Perhaps this helps. Let’s imagine you have a file called “a.txt”, whose contents are Unicode text “a” (hex “FE FF 00 61”). And you wanna convert it to plain ascii. Then:

And now you have the same file (“a.txt”) with contents “a” (hex “61”).
Is this similar to what you need?

Getting closer, but this one simply converts all of the text to question marks. To be honest I think what i’m trying to do is out of my league a bit.

It’s for dumping straight into a webpage, but the apostrophes and quotation marks all turn into something else while most of the text remains unchanged.

very annoying, don’t worry if you can’t think of something that does it all magically, my brain has gone to mush thinking about it.

Thanks anyway

Mungo