open text file and speak the text, repeat every 20 minutes

New to this forum, new to scripting. This is my first post.

I have a need that has brought me here and I hope that someone will kindly assist me.

I have a TextEdit file that I want the Mac to speak.This is running OSX 10.9 and TextEdit 1.9

I’m trying to work with Automator, and I can only get it speak the file path.

I also want it to speak the text every 20 minutes, but one thing at a time.

Any help would be greatly appreciated.

Hey There,

This should get you started.


tell application "TextEdit"
	if front document exists then
		set textData to text of front document
		if textData is not "" then
			say textData
		end if
	end if
end tell

First of all welcome to MS.

Is the file text file or a document (text with markup). When the text is just plain text you can easily use bash (do shell script) to do it for you. The following examples will speak the contents of the file. NOTE: The contents of the file must be plain text

say -f '/Users/<shortname>/Deskop/speakme.txt'

or

say < '/Users/<shortname>/Deskop/speakme.txt'

or

cat '/Users/<shortname>/Deskop/speakme.txt' | say

in AppleScript it would look like

do shell script "say -f " & quoted form of POSIX path of theFile

theFile is a file object or a string containing an HFS file path

To repeat this every 20 minutes there are more ways to skin this cat. You can use an idle handler, the delay command in a repeat loop or the sleep command in bash. Either way I would choose one of the last two options because idle handlers are leaking memory (should be fixed in latest Mac OS X but I haven’t tested it).

In AppleScript to execute something every 20 minutes you need something like:

repeat --forever
--insert code here
delay 1200
end repeat

in bash the same thing would look like

[code]while true; do

Insert code here

sleep 1200
done[/code]
To put this all together you could have something like:

set theFile to (choose file) as string
repeat --forever
do shell script "say -f " & quoted form of POSIX path of theFile
delay 1200
end repeat

Thanks very much!

The scriptlet from ccstone is very close to ideal for my application! The shell script idea from DJ Bazzie Wazzie is great, but will have less applications for the bigger purpose. Let me explain the need here…

This project has about 70 Macs that are running together in an art installation. They all need to speak the contents of a text document as some repeating interval.

I need this to run, if possible, on Systems 9,8 and 7 as well. I am under the impression that AppleScript should be supported on very old OS’s. I may even try to get the System 6 machines to speak the text.

The shell script is less useful on Classic OS’s.

I will try this scriptlet on the older machines to see how it works. I will need to edit it to call simpletext on the Classic machines instead of teachtext.

I would like a way to invoke the script every 20 minutes or so. I see there a script step to repeat,
http://www.mactech.com/articles/mactech/Vol.20/20.12/RepeatLoops/index.html

but I don’t see a wait, or pause option.

Thanks again for your help!

That should be true and you’re right about the do shell script. There is however one problem, the delay command in Mac OS will block the entire OS. So it’s better to repeatedly do a delay than one long one, the system isn’t running any smoother but now can quit the script with a maximum delay of 2 seconds.

--code is not tested, not behind a mac right now
set theFile to (choose file) as string
repeat --forever
say (read file theFile)
repeat 600 times --now on Mac OS system the system is only blocked for 2 seconds
delay 2
end
end repeat

Also note that the precision of the delay command between the Mac OS and Mac OS X have differences in precision. So a repeating 600 times a 2 second delay isn’t accurately 20 minutes and can be off by several seconds. Another approach, since this an art (or mac history museum) you can also run several scripts that are checking the current date. When it meets it’s condition it will speak the contents of the file. A little different approach but if all machines uses ntp they will never get out of sync. Just an idea.

AFAIK, AppleScript only exists since System 7. The time Apple re-wrote their OS from Pascal into C.

I have been working with this script and have mixed results. I really appreciate the assistance that’s been offered.
I’ve modified and synthesized the scripts offered to this- which speaks the text every 10 minutes -

repeat --forever
tell application “TextEdit”
if front document exists then
set textData to text of front document
if textData is not “” then
say textData
repeat 300 times --now on Mac OS system the system is only blocked for 2 seconds
delay 2
end repeat
end if
end if
end tell
end repeat

I want it to auto-run when launched so I saved as an application (named Speaking) and made startup item. Works fine in MacOSX10.8.5, with Script Editor 2.5.1 AppleScript 2.2.4. Except that I must force quit the Application to restart the computer.

I have a problem with MacOSX10.4.11. The Speaking Application runs once but returns an error that Apple Event timed out and the script stops running. This is Script Editor 2.1.2 AppleScript 1.10.7

I am very appreciative of any suggestions that might be offered to modify the script to make it work as desired.

You do not need all the repeats. Which as you see will cause you no end of problems.

Using the code you are comfortable with you can just surround it in a Idle handler with it’s return to the number of seconds that equates to ten minutes.
Save the script as a Stay open Application.

on idle
	
	tell application "TextEdit"
		if front document exists then
			set textData to text of front document
			if textData is not "" then
				say textData
				
			end if
		end if
	end tell
	return 600 -- number of seconds to run again
	
end idle

Below is how Apple explain how this works. No point me writing it myselfe. :slight_smile:

AppleScript Language Guide

Looking at his problems it has nothing to with the delay command. Also the idle handler is introduced by the latest Mac OS 8.5, ls wants to run it on all his macs. So at some point he has no other option than using delay or my previous suggestion matching current date repetitively before continuing.

Saving an script as application does have issues in Mountain Lion. The problems comes from a wrong implicit quit handler. Try adding the following code to your script.

on quit
continue quit
end quit

Wrap the say command in a timeout block. I’m not sure but I thought that the default timeout for every event on older systems was 1 minute. The text you want to speak takes probably longer than a minute.

with timeout of 1800 seconds
	say textData 
end timeout

No – it’s been there since the very beginnings of AppleScript, or near enough.

I thought it was introduced in AppleScript 1.3.2, the AppleScript version which shipped with Mac OS 8.5. My mistake…

I knew applets exists longer, only the the idle handler was introduced later.

I’m very grateful for the assistance offered here.
I will try these modifications to come up with something that works.

Thank you all!