1
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.

3

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
  • This does work! However when I place the actual, non substitute of theList, I get some results that are just: "" Is there any way to get rid of these empty items? – Adihsam May 27 at 20:26
  • A check can be added for empty strings - I've updated my answer to also add the numbers instead of text. – red_menace May 27 at 20:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .