AppleScript “random” examples

AppleScript random FAQ: Can you share some AppleScript “random list” item examples?

While working with some Mac speech recognition software I originally didn’t know how to do something with a random item from an AppleScript list, so my first AppleScript random script attempt looked like this:

-- my applescript list
set myList to {"Problem", "There was a problem", "Bummer"}

-- get the list size
set listSize to count of myList

-- get a random number
set randomNumber to (random number from 1 to listSize)

-- get the random item from the list
set reply to item randomNumber of myList

-- say the string
say reply

However, after some digging around, I was able to find this more concise AppleScript random syntax:

set myList to {"Problem", "There was a problem", "Bummer"}
set theItem to some item of myList
say theItem

As you can see, with or without the comments that second AppleScript random syntax is much more concise.

AppleScript “waiting until completion” example

While I’m in the neighborhood, I should also mention that the initial AppleScript random example I found had this extra syntax at the end of the script:

set myList to {"Problem", "There was a problem", "Bummer"}
set theItem to some item of myList
say theItem displaying theItem with waiting until completion

As you can see, that last line is different, and I’ve found that the “with waiting until completion” is helpful when you’re telling an AppleScript script to say something while you’re also working with the Mac speech recognition software.