Using script can select the atoms in a specified space

What I am going to do is to monitor the density change with time within the defined space . So I need to select and count the atom number in a selected area with  specified XYZ coordinateI.  But I did not find any script API to realize such function. Is there anyone know about it or have the well-written script code to realize such function? Thanks!