When viewing an auto page number on a master page in an InDesign document it will display as the letter A. An auto page number character on a document page will display as the name of its parent page. This leaves the question: Since the auto page number on a page evaluates to the name of its parent page, what do you intend to replace it with? The following script snippet written in AppleScript demonstrates getting a list of character references for current page number characters within a story. This should give you some idea for writing your script.
(*Assumes that there is an insertion point within the story to be searched.
Do not select a current page number character.*)
tell application "Adobe InDesign CC 2019"
set theInsert to item 1 of selection --insertion point in story
set storyRef to parent story of theInsert
set findText to "^N" --metacharacter for the current page number special character
set foundSet to my findTextRef(storyRef, findText)
end tell
foundSet
(*returns list of text items defined by findText variable within the object objRef*)
on findTextRef(objRef, findText)
tell application "Adobe InDesign CC 2019"
set find text preferences to nothing
set find what of find text preferences to findText
tell objRef
set textList to find text
end tell
set find text preferences to nothing
end tell
return textList
end findTextRef