Digital comedian, randomly choose items from list without repeating.

Digital Standup Comedian,
Created in OS 10.8 Mountain Lion.

Hello scripters,
Here’s a fun little app that I made. I am still a newbie with AppleScript, so the code may not be the most elegant. But it works well enough, and it was a lot of fun. I thought it would be cool to share it with others.
Link to file in my drop box folder:
https://dl.dropboxusercontent.com/u/20557140/Digital%20Standup.zip

Copy the zip file to your desktop. Double-click to unarchive it. It will create a “Digital Standup” folder. Open the folder and double-click the Digital Standup.app.
The app is not locked down. You can open it in AppleScript editor and examine the code.

About the Digital Standup application
The digital standup application uses text and computer voices to read random jokes to you. It also speaks funny sayings, and uses sound effects in between the jokes. In between each joke it also gives you a chance to stop or continue. After 10 seconds it will continue on its own. It will never repeat a joke, until every joke on the list has been spoken.

About the AppleScript code
This project was actually an experiment in coding, to learn how to compare two lists. It also demonstrates how to randomly choose items from a list, keep track of which ones you have already chosen, so as not to repeat any choices, until all items in the list have been chosen.
The digital standup comedian is an AppleScript application. It uses TextEdit as a visual display, and it uses “text to speech” as a verbal display. It pulls in every joke from the “Jokes list.txt” file, inside the “data” folder. It creates the first list using all of the jokes, Then it randomly picks one, and starts creating a second list with the first chosen joke. It speaks the joke, does it’s routine, then repeats the process. Each time it picks a joke, it checks the second list that it is building to see if it’s random joke exists in that list. If the random joke is already in the second list, then it chooses another random joke. If the random joke is not in the second list, it adds it to the second list, then starts the rest of its routine and repeats the process. Once all jokes have been added to the second list, the application will stop.
Other areas of note:
In some areas of the code, you’ll see me create a random number, then repeat from 1 to that random number. This is how I am randomizing the random selection process. I know that computers cannot generate “real” random numbers, but this process seems to help.
Also, if you look at the “comments list” section, you’ll notice several voice commands in use. These commands are enclosed with double brackets, And are embedded in the quoted ‘say’ commands. This is how I altered the text to speech voices, during the comments in between each joke.
Also, in the sound effects list, you’ll see that I repeated the drumhit sound several times. This is because I wanted that item chosen more often.

I’m including the entire code here.
Although, it will not function correctly unless all of the needed resources are in place. I have many years of digital experience, including scripting and basic programming in the past. But I am a relative newbie to AppleScript coding. Critiques, suggestions or even new versions are always fun! :slight_smile:
I hope you enjoy the digital standup comedian.
Coded by Ted Nicholas, 2013.
nich01@tds.net
blindguynich@gmail.com

(* Digital Standup Comedian 
Part of Speakable Stuff,
 Custom Verbal Commands for Speakable Items
By Ted Nicholas, 2013. nich01@tds.net 
*)

-- Random joke list performed as standup

-- get master Joke List from file
set thePath to (path to desktop) & "Digital Standup:data:Jokes list.txt" as string

tell application "Finder"
	activate
	delay 0.2
	open file thePath
end tell

-- apply every paragraph of file to list
tell application "TextEdit"
	activate
	tell document "Jokes list.txt"
		activate
	end tell
	delay 0.2
	set jokeList to (every paragraph of text of front document) as list
	delay 0.2
	close front document without saving
end tell

--TextEdit Setup
tell application "TextEdit"
	activate
	if front document exists then
		close front document without saving
	end if
	delay 0.2
	make new document with permissions
	delay 0.2
	tell front window
		set bounds to {8, 22, 573, 505}
	end tell
end tell


-- commentList
set commentList to {"I'll be here, [[rate 60]] all [[rset 0]] week!", "[[pbas 46]] [[rate 260]] [[emph -]] Bid [[pbas 47]] [[emph +]] dump, [[rate 165]] [[emph -]] bump [[pbas 43]] [[emph -]] bum. [[rset 0]]", "Ahem!", "Cough, cough!", "[[rset 0]] And if [[emph +]]you [[emph -]]believe [[emph +]]that!", "[[Rate 280]] Ka [[Rate 120]][[emph+]]Ching! [[Rate 196]] Baby![[rset 0]]", "Thank you. [[emph +]] Ladies and [[emph +]] germs.", "I'm 'hot' tonight!", "Ba-da Bing!", "Is it hot in here?", "[[rate 140]] Hell-lo, [[rset 0]] Is [[emph +]] this [[emph -]] thing [[emph +]] on?", "[[rate 60]] [[emph +]] I'll [[rate 220]] [[emph +]] tell [[emph -]] yuh? [[rset 0]]", "Oh! Tough crowd!", "I [[emph -]] gotta [[emph +]] million .97 of them! [[slnc 400]] smileyface.", "Is it [[Emph +]] weird in here, or is it just me!", "I [[emph +]] think I need a [[emph +]] memory upgrade!", "Ahem! Was it [[emph]] something I said?", "Hey, where's [[emph +]] everybody going!", "Wadda you [[emph +]] want for nothin, a [[emph +]] rubber [[rate 60]] bis  [[rset 0]]skit?", "I [[emph +]] just telecommuted in, and boy, are [[rate 100]] my [[rate 120]]'bits', [[rset 0]] tired! L O L!", "[[pbas 47]] [[rate 90]] [[emph +]] Hay, [[rset 0]] what did [[emph +]]you expect?", "[[pbas 47]] [[rate 90]] [[emph +]] Hay, [[rset 0]] [[rate 200]] what's all this [[emph +]]chickenwire for?", "oh, sorry. That was a bad one. [[slnc 1500]]  [[rate 140]] Hay, [[rset 0]] be [[emph +]] nice!"}

-- intro phrase
set theMessage to "Okay, here we go. Okay to continue or Quit to Quit." as text
displayText(theMessage)
display dialog "OK to continue. Or Quit" buttons {"Quit", "OK"} default button "OK"
if button returned of result = "Quit" then
	tell application "TextEdit"
		activate
		close front document without saving
		quit
	end tell
	tell me
		activate
		say "I'll be here all week."
		quit
	end tell
end if

set usedList to {}
-- repeating random jokes
repeat
	repeat
		set Z to (random number from 4 to 10)
		set X to (random number from 1 to Z)
		repeat X times
			set theMessage to ""
			set theMessage to (some item of jokeList)
		end repeat
		if (count of usedList) = (count of jokeList) then
			exit repeat
		end if
		if usedList contains theMessage then
			exit repeat
		end if
		set usedList to usedList & theMessage as list
		
		-- set pathnames to sound files for QuickTime
		set sndList to {"Drum hit", "Drum hit", "Drum hit", "Crickets", "Cymbal", "Gong", "Drum hit", "Cough", "Bowling strike", "Drum hit", "Cartoon boing", "Coin drop", "Cow moo", "Drum hit", "Race car", "Water drip", "Drum hit", "Drum hit", "Drum hit", "Drum hit"}
		repeat 5 times
			set theSound to (some item of sndList) as text
		end repeat
		set soundEffect to (path to desktop) & "Digital Standup:data:Sound effects:" & theSound & ".m4a" as string
		
		set theMessage to theMessage as text
		displayText(theMessage)
		playSound(soundEffect)
		delay 1
		say (some item of commentList)
		delay 0.2
		say "[[rset 0]] OK or Stop."
		set theChoice to button returned of (display dialog "OK for a random joke, or Stop to stop." buttons {"Stop", "OK"} default button "OK" giving up after 10) as text
		
		if theChoice = "Stop" then
			exit repeat
		end if
		if (count of usedList) = (count of jokeList) then
			say "That's all the jokes I have."
			set theChoice to "Stop"
			exit repeat
		end if
	end repeat
	if theChoice = "Stop" then
		exit repeat
	end if
end repeat

set theMessage to ("Stopping Jokes. Come back anytime.")
displayText(theMessage)
delay 1

tell application "TextEdit"
	activate
	set text of front document to ("  These jokes were gathered from around the Internet and emailed from friends." & return & return & "  Many of these jokes were caused by the mind of Steven Wright." & return & "[url=http://www.stevenwright.com]www.stevenwright.com[/url]" & return & ¬
		return & "jokes gathered and programmed by" & return & "Ted Nicholas. blindguynich@gmail.com")
	set size of text of front document to 24
	say "I'll be here, [[rate 60]] all [[rset 0]] week!"
	delay 3
	repeat
		if front document exists then
			close front document without saving
		else
			exit repeat
		end if
	end repeat
	quit
end tell
tell application "QuickTime Player"
	activate
	quit
end tell


-- Handlers  ------------

--  handler to display text
on displayText(theMessage)
	tell application "TextEdit"
		activate
		tell front document
			activate
		end tell
		set text of front document to theMessage
		set font of text of front document to "Ariel"
		set size of text of front document to 36
	end tell
	say theMessage
	tell me
		activate
	end tell
end displayText

-- handler to play a random sound effect
on playSound(soundEffect)
	tell application "QuickTime Player"
		-- activate
		open file soundEffect
		tell front document
			set audio volume to "0.4" as real
			set looping to "false"
			play
		end tell
	end tell
	tell application "TextEdit"
		activate
	end tell
	tell me
		activate
	end tell
end playSound

Model: MBPro, 13 in, 2012 model,
AppleScript: 2.2.4.
Browser: Safari 536.29.13
Operating System: Mac OS X (10.8)