How can I address a number within text and have it retain # class?

I’m wanting to step through a text document (a Table of Contents), and update page numbers after having inserted some pages. After writing the first bit of code and never getting the expected result, I wrote the uncommented second script to check if and when an integer was encountered… it never was; every number in my list became a text. Can someone here see what I am missing and give me a hint? Thanks.

This is a snippet of the text I’m evaluating in my text editor:
Part 1: Welcome to NameofSoftware
Learning About NameofSoftware 6
Connecting to NameofSoftware from a remote machine 7
Part 2: Section Headline
etc.


(*
tell application "TextWrangler"
	tell text window 1
		repeat with wordX from 1 to count words
			set theData to word wordX
			if class of theData = integer then
				set word wordX to (word wordX) + 5 --advance all numbers by inserted page count
			end if
		end repeat
	end tell
end tell
*)


property theList : {}


tell application "TextWrangler"
	tell text window 1
		repeat with wordX from 1 to count words
			set the end of theList to word wordX
		end repeat
		tell me to TestforIntegers()
	end tell
end tell


to TestforIntegers()
	repeat with listVar from 8 to 10 -- I shortened the range for this test. Item 9, which is "6", should be a number.
		if class of item listVar in theList = integer then
			display dialog "Bingo!" & return & "Item " & item listVar of theList & " is a winner."
		else
			display dialog "Item " & listVar & ", " & return & "''" & item listVar of theList & "''" & return & " is not an integer."
		end if
	end repeat
end TestforIntegers

Everything you get from a “text” document will be text, including any numbers you encounter. To test them, you have to try to coerce them:

set someText to "This is item number 3 of 5"
set w to words of someText
set Nums to {}
repeat with aW in w
	try
		set end of Nums to aW as number -- not numbers fail and we ignore them
	end try
end repeat
Nums --> {3, 5}

Alternatively, you can test against a list:

set someText to "This is item number 3 of 5"
set w to words of someText
set n to {"2", "3", "4", "5"}
set p to {}
repeat with d in w
	if d is in n then set end of p to d as number
end repeat

This works with the characters of the string as well in cases where numbers are mixed with text like “3rd”

Huh. I had actually considered the coercion “as number” at one point, but I thought the script would squawk that the text couldn’t be converted to a number; I didn’t take into consideration that “try” would ignore that error. I can finish the rest on my own, now. Thank you.