How to Make AppleScript Read Numeric Items from a Variable

applescript

set theList to {"123", "124", "abc", "125", "efgh", "126"}
set theNumbers to numbers of theList
return theNumbers

How can I get AppleScript to recognize which items of theList are numbers and assign a variable to those numbers?
The variable I have for theList is a place holder, the items it represents will be constantly changing.

Best Answer

You could try converting items in the list to the desired class, and ignore the ones that error:

set theNumbers to {}
set theList to {"123", "124", "abc", "125", "efgh", "126"}
repeat with anItem in theList
  try
    if contents of anItem is not "" then set the end of theNumbers to anItem as number
  on error errmess number errnum -- didn't convert
    log errmess
  end try
end repeat
return theNumbers