code for textedit to speak its text while typing in progress??

dear all applescript experts!
yes. i’m a ‘noobie’ here.
i’m an artist who uses max/msp to make music etc.
right now, i have a project in which i’d like to automate text-to-speech (within textedit) while typing is in progress.
manually(without script = by selecting ‘start speaking’ within scroll-down menu), it keeps reading while typing although it stops at the word where ‘it used to be the end’ when the speech started. (so i believe the speech memorizes the whole text when it’s started??)
it’d be so nice if it can keep speaking while typing in progress.
but friends who know a bit about coding (not necessarily applescript… in fact, +c coders seem to hate applescript…) said that it won’t be possible.
manually, i’d just bring the curser to the top and let it re-read the text (which will be naturally longer each time).
again, repeating the reading in such manner is not ‘looping’. so the following very basic text-to-speech in loop, didn’t work.
(it blocked out from typing.)


repeat
	tell application "TextEdit"
		set the_text to (the text of document 1) as string
		if the_text is "" then error "This document contains no text."
		say the_text
	end tell
end repeat

i think a solution will be scheduling the ‘say the_text’ every 1 min or less (depending on the average length of the file?)
but the typing is being done automatically via ilisten’s dictation function. so i don’t know what would happen during the applescript is running? (manually, it continues to dictating/typing based on the external voice, and when the textedit starts speaking, it’d mix up the both voices and picking up words from both and making up quite interesting text w/o a problem.)

upon some lengthy research online, i’ve stumbled with an applescript called ‘speakerfone 2.0’ created by sam walker (often goes as asan102) - which he claimed that it’d keep speaking while typing. i looked for it everywhere but all download sites are pointing to his link which was totally dead. i wish i can find him somewhere so i can ask the question.

so… is there any good suggestion to make it possible (or possible to bring a similar result?!?)

any interesting replies will be appreciated!
thank you very much in advance.

cheers,
pic

p.s. i’m currently on mac osx 10.3.9 on titanium powerbook 1g model. but the project will be handled by a mini mac 1.42g w/ newest tiger. so if automator may have any different solutions, i’m open for it too.

Don’t you just love it when folks say that? :wink:

The trick to freeing up TextEdit for typing is to simply grab the front document’s current text, and then pass the speech commands to some other app (such as Script Editor or a script application). Try something like this:

set text_buffer to 5 (* avoid attempts to speak just a few new characters: modify as required *)
set previous_count to 0
activate application "TextEdit"
repeat
	tell application "TextEdit" to set current_text to document 1's text
	set current_count to count current_text
	if (current_count - previous_count) > text_buffer then (* some new text was added *)
		set current_text to current_text's text (previous_count + 1) thru current_count
	else (* we've reached the end of the document so far *)
		delay 1.5 (* pause before restarting entire text *)
	end if
	set previous_count to current_count
	say current_text
end repeat

hi kai,
thank you so much for your reply!
i somehow keep getting applescript error which shows whatever the text on the textedit file.
it lists all the text on it with no particular explanatory for ‘what error’ it is.
?!?

ah… well, sorry!!
i must’ve left something (a tiny part of your explanation sandwitched between * & *) in there.
now, i got more understandable error: syntax error - expected “end” but found “else”.
did i do anything wrong here? or something needed to be added?
thank you if you can help.
keiko

Hi,

Speaking text is more complicated then you’ve explained. What do you do if the user edits previous text? Does it speak after every word? Will special word characters interfere with the text? Do you want an app continuously running to monitor the text?

I’m sure I have more questions, but you need to think about this kind of stuff.

Note that AppleScript is slow. When dealing with something like this you might want to do it in C although AppleScript may be able to deal with what you want depending on how complicated it is.

gl,

Hi,

I’m not thinking right right now, but came up with this that doesn’t work right yet:

global t

on run
– initialize
set t to “”
end run

on idle
– get the new text
tell application “TextEdit”
launch
activate
set new_t to text of front document
– compare start of the text
if new_t begins with t or t is “” then
– get text after text
beep 2
set l to length of t
set added_t to (text (l + 1) thru -1 of new_t) as string
end if
end tell
say added_t
return 2
end idle

Maybe you can fix it so it just says the added text. You must save idle handlers as stay open applications. Surely, there are more things that need fixing.

gl,

I found it! Nowhere did I update t to the new text.

Ok, almost got it. I added one line:


global t
--
on run
	-- initialize
	set t to ""
end run
--
on idle
	-- get the new text
	tell application "TextEdit"
		launch
		activate
		set new_t to text of front document
		-- compare start of the text
		if new_t begins with t or t is "" then
			-- get text after text
			beep 2
			set l to length of t
			set added_t to (text (l + 1) thru -1 of new_t) as string
			set t to new_t -- added this
		end if
	end tell
	say added_t
	return 2
end idle

gl,

Thank you, Kel.
One thing regarding this particular project, the-already-typed text won’t be changed. It just continues typing (means that it only keeps adding.) It is because the voice recognition/dictation program, ‘iListen’ will be doing the typing. So, yes. The same text will be getting longer, but it won’t erase or change the previously typed part. (but probably it is still quite complex for ‘Speaking’ to go back to the top and do its speaking again and again the ‘ever-growing’ text?!)

Of course, it’s ideal that it’d wait and keeps reading it without returning to the top, but I don’t think Speaking work like that. (It only reads 'til the temporary ‘end’ which was registered when it started speaking… it won’t renew its understanding of the endpoint of the text.)

Well, in any case, there will be only one text file involved in this project. Only one file (which will be pre-designated with a few word already on top) is to be growing as the dictation progresses.
If my assumtion (= it will be better to time-schedule the one-time only reading every 1-3 min?? rather than struggling with figuring out scripting the repetition of the action) is correct, it’d be really helpful to ‘save the text’ action, then ‘start speaking’.
… maybe I’m dreaming?! It’d only be too complicated?

Thank you to everyone who may have more suggestions!!

Keiko

Hi Keiko,

Did you try my half ass script? It says like parts of words and that’s the bad part. Making the algorithm so it doesn’t need to look at old text makes it easy.

In fact, you could make a new strategy here.

gl,

Hi Keiko,

If I were you I would think about one thing at a time with the things that you’re thinking about. When you talk about using listening, it gets kind of complicated more. Now you’re ntalking about reading text and speaking it.

One word of caution, Do not use computer spoken words into the computer listening. You don’t want to do this because if you have the text that are to be spoken then you do not need to listen to it. Do you understand?

When you come up with an idea for a program, you should write an algorithm. If your idea is not set, then you are open and your mind may change.

gl,

Hi Kei,
Thank you for your words.
I have already figured out the audio part: speaking/listening.
I’m a sound artist primarily, so the technicality of speaking/listening was already configured when I posted the initial question.
It is handled well with using an open-source app called JACK. JACK can route the system voice sound to Max/MSP to get it heard by iListen.
Also, the natural voice coming from external source is to be brought in to Max/MSP (also via JACK.)
JACK is like a mac system administrator’s sound panel. Once it’s set, it’s there in background.
As said before, everything works fine manually.
Based on the codes you came up, I’ll struggle to figure out.
More reports later.
Thanxx again!!
Keiko

Hi Keiko,

I don’t have the apps you wrote about, so used SpeechRecognitionServer. Here’s an example of saving the text in TextEdit:


-- create file specification
set dp to (path to desktop as string)
set file_spec to (dp & "SpeakFile.txt") as file specification
-- create the file if there is no file
set ref_num to (open for access file_spec)
close access ref_num
tell application "TextEdit"
	launch
	activate -- brings TextEdit to front
	open file_spec
	set t to (text of front document) as string
end tell
say t
tell application "SpeechRecognitionServer"
	set new_t to (listen for {"hello", "bye"})
end tell
tell application "TextEdit"
	make new paragraph at end of paragraphs of text of front document with data new_t & return
	save front document in file_spec
end tell
say new_t

gl,