As my previous post advised, searching the Adobe Photoshop forum for a scripting keyword will deliver a wealth of working code examples.
The other main scripting site with code to learn from is:
https://www.ps-scripts.com/
As for books, legacy ExtendScript and the new UXP is exhaustively covered by:
https://www.davidebarranca.com
https://www.ps-scripting.com/
Thanks Stephen, let me give @Joseph25509234xr72 some extra-context.
The scripting code in the replies above is ExtendScript (an Adobe-proprietary flavour of JS based on a very old language specification). Since late 2020, Photoshop has been transitioning to UXP, which uses modern JavaScript. ExtendScript is going to be faded out (ETA not available), in favour of UXP. Please note: the two are not compatible with each other.
I have a series of videos available on YouTube for free about UXP, if you want to get started. For a more curated learning path, in late 2023 I've published a 326-page book on the subject; Marc Autret (a fine InDesign developer) has written a detailed, independent review which you can read here if you're interested.
Adobe is generally good with Reference documentation, not so much with narrative content; I'm trying to fill the gap.