If you are doing a batch script best way is to make a temp path with stroke and fill colour of current colour then loop over each colour and doing both:
app.executeMenuCommand("Find Stroke Color menu item");
app.executeMenuCommand("Find Fill Color menu item");
then checking document selection to see if there is more than 1 (or 2 sometimes not too sure why there is sometimes 2 even though it isn't used elsewhere).
If you find its being used: store colour then loop over the list of colours drawing as u go
If your are opening the document and then running the script you could just go to the swatches panel, click the options drop down top right and select unused colours -> drag to bin then run the script afterwards. That way you the only swatches it will be drawing are only the swatches currently in the document