How can I execute AppleScript from Textedit?
You can't execute AppleScript directly from TextEdit. Apple provides an AppleScript editor though in /Applications/Utilities/Script\ Editor.app which makes it easy to create and run AppleScript code.

Just navigate to /Applications/Utilities in Finder, run Script Editor and copy/paste your code into it.