That script is javascript and not AppleScript. They are two separate, distinct, scripting languages. (Like French and Spanish.... many similarities, but still quite different)
In general, one should not use Apple's ScriptEditor unless you wish to control the operating system in some manner. AppleScript is used to control the Apple operating system and Apple applications.
Adobe applications all use javascript to control Adobe applications. While it is possible to control Adobe applications through AppleScript, the script has to be written in the proper language for any AppleScript to function. It is not possible to just paste a set of javascript commands into Apple's ScriptEditor and have a functional AppleScript.
At the very least any AppleScript would need to call that javascript specifically within the AppleScript to control Adobe applications. But using AppleScript can often be unwarranted overhead if the desire is to merely automate Photoshop.
In short: You do not really need or want AppleScript here. AppleScript is simply overhead given that you have a (presumably) functional javascript and are not seeking to control the Apple operating system in any way.
Copy the script to a plain text file.. save the text file with a .jsx
suffix. Then Photoshop will see the script. Whether or not the javascript works as desired is another matter.
(Important Note: Apple's TextEdit, by default, will save rich text. That is not what you want. You want plain text. You can change the TextEdit preferences to save plain text.)
You can check the help files related to scripting Photoshop for more information.