Pick random words from a text file

Just wanted to share this piece of code, it’s about the first useful thing I’ve ever written in AppleScript.

It’s a short script designed to pick two random words from a text file (I use a dictionary word list) and display them together. I designed it because I want to register a domain name that’s easy to remember, and slightly oddball:

set loopstop to "0" -- We'll use this to break the dialog box cycle

tell application "TextEdit"
	activate
	tell document "words.txt" -- name of file, must have it open in textedit
		activate
		repeat while loopstop = "0"
			set maxnum to the number of words of it -- sets maximum range for number generator
			set randnum1 to random number from 1 to maxnum
			set randnum2 to random number from 1 to maxnum
			set firstword to word randnum1 of it -- first word
			set secondword to word randnum2 of it -- second word
			display dialog firstword & " " & secondword buttons {"Again", "Done"} default button 1
			set the user_choice to the button returned of the result
			if the user_choice is "Done" then set loopstop to "1" -- break the loop
		end repeat
	end tell
end tell


The next step - which I’d appreciate some help with, since I’m very new to this - is to iterate this loop (say) 100 times and write the results to another text file, so I can pick the best names en masse. Suggestions and comments much appreciated.

Cheers,

Flashman
http://blastradius.blogspot.com

Are you looking for something like this?

property myRandomFile : quoted form of POSIX path of ((path to desktop as text) & "Random Words.txt")

tell application "TextEdit"
	activate
	display dialog "Repeat this many times:" default answer "10"
	
	repeat (text returned of result) times
		tell front document to get (some word) & " " & (some word)
		
		do shell script "echo " & quoted form of result & " >> " & myRandomFile
	end repeat
end tell

Thanks Bruce, that’s nice and succinct, although it appends the word pairs to the dictionary file I’ve got open. Not a big problem, since I can cut and paste, but was that your intention?

Maybe it was, I’m a total n00b :wink:

No. Unless the file you got open is named “Random Words.txt.” In which case you should rename it, or rename the filename in the script.

Ditto on what Reikon said.