How do you Speek random text files

Ok, I am on a quest to have the coolest talking house ever. Asside from announcing things like mail and weather which are huge hits in my home. I am now trying to have “Clarance”, my computer randomly pick a file from a folder and speek the contents of that file aloud. The way it should work is at random time interval’s, Clarance would go to my Jokes folder and selecta file from many, open it and read it aloud.

So, I have created a folder(Jokes), put several *.rtf files in it and I am trying to figure out using applescript how to have Clarance grab a *.rtf file, and speek the contents automatically.

Confession: I am not good at creating applescript from scratch, but I am very good at modifying existing files.

Can you help?:smiley:

Putting it all in a .rtf file was not a great idea. The rich text format includes all kinds of hidden characters for formatting the text, and all you want is the text. This is easy to do if you save your “jokes” one paragraph each and save them in a plain text (.txt) file. You can do that in the TextEdit Format menu: ‘Make Plain Text’. Save it where you want it. In your script editor run the one-liner ‘choose file’ without the quotes. Choose your joke file and in the results pane of the Script Editor, you’ll see the complete path to it as an alias. Copy that to a variable, i.e. ‘set Jokes to putCopyHere’. Set the contents to ‘set J to read jokes’ and you’ll have all the jokes in your script as the variable J. Paragraphs of J is a list of the jokes. ‘set thisJoke to some item of paragraphs of J’ is a randomly chosen joke. Finally, ‘say thisJoke using “Victoria”’ will have the joke spoken aloud.

Get that working, post it, and then ask how to make it happen at random.

Here’s one way to do it with txt files (not rtf) inside of a “jokes” folder on your desktop.

set mypath to ((path to desktop folder as Unicode text) & "jokes")
set myjokes to {}
tell application "Finder" to set mylist to the name of every file of folder mypath
set mycount to count of mylist
repeat with n from 1 to mycount
	set myjokes to myjokes & (do shell script "grep -v thisisgibberish " & quoted form of POSIX path of mypath & "/" & (item n of mylist))
end repeat
say some item of myjokes using "Victoria"

Here’s another way to do it, if your jokes are relatively short.

First, you need to create folder “jokes” on desktop, and then create new folders within, and change their name to the jokes you want to use.

set mypath to ((path to desktop folder as Unicode text) & "jokes")
tell application "Finder" to set mylist to the name of every folder of folder mypath
say some item of mylist using "Victoria"

If I were doing this, I’d save my jokes (or whatever) all in one text file separated by a delimiter not likely to appear in the text. Something like this: (where the leading number is the voice I want to use separated from the line by a colon and space)

set tVoices to {"Agnes", "Albert", "Bahh", "Bruce", "Fred", "Junior", "Kathy", "Princess", "Ralph", "Vicki", "Victoria", "Zarvox"}
set Jokes to read alias ((path to documents folder as text) & "Jokes.txt") -- you'll have to save the quote above as a text file named Jokes.txt in your Documents folder.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "-<^>-"
set aJoke to some item of text items of Jokes
set p to paragraphs of aJoke
set AppleScript's text item delimiters to ": "
repeat with aP in p
	if length of aP > 5 then
		set aVoice to item (text item 1 of aP) of tVoices
		set aLine to text item 2 of aP
		say aLine using aVoice
		delay 0.3
	end if
end repeat
set AppleScript's text item delimiters to astid

This is not extensively tested; I just played with it.

Thank you both for taking the time to help out a newbie. So here is where I ended up.

set mypath to ((alias "Ladan:Users:Sula:Desktop:" as text) & "jokes")
set myjokes to {}
tell application "Finder" to set mylist to the name of every file of folder mypath
set mycount to count of mylist
repeat with n from 1 to mycount
	set myjokes to myjokes & (quoted form of POSIX path of mypath & "/" & (item n of mylist))
end repeat
say some item of mylist using "Cepstral Lawrence"

This says aloud each file in my jokes folder located on my desktop.
Now that the program is recognizing each file, I assume I need to insert “open document file” somewhere. I have tried several locations but I continue to be reminded that I am a NEWBIE…

Hi, LaDan. If you have folder on your desktop called “Jokes” containing one joke per file (in plain text), then this is all you need to say one:

tell application "System Events" to set myJokes to files of alias ((path to desktop as text) & "Jokes")
--> a list of the files in the folder (in the format: file "path to file")
say (read (some item of myJokes))

BRILLIANT! It worked straight away no assistance from my bad coding.
Now for Randomness? Each time I execute the script it should pick a different file?

I’ve modified the file to make it a bit quicker, LaDan (using System Events instead of the Finder)

It should be random. That’s what “some item” does. Remember that random is like coins - you can get several heads in a row.

THANK YOU… Geez that looked too easy, now that I understand what was done!
Thank You:lol:

By the way; if you want the Jokes file in your Documents folder, then this works:

tell application "Finder" to set myJokes to files of alias ((path to documents folder as text) & "Jokes:")
say (read (some item of myJokes as alias))

Also, if you don’t want to hear two the same in a row, something like this:

property lastJoke : missing value
tell application "Finder" to set myJokes to files of alias ((path to documents folder as text) & "Jokes:")
set aJoke to some item of myJokes as alias
repeat
	if aJoke = lastJoke then
		set aJoke to some item of myJokes as alias
	else
		say (read aJoke)
		set lastJoke to aJoke
		exit repeat
	end if
end repeat