Graphical User Session
The AppleScript is not being run within the active graphical user session and thus can not access the WindowServer. This is required for Open Scripting Architecture (OSA) languages to interact with graphical applications.
If you can perform your script without needing to interact with a graphical application, an ssh
initiated approach can work.
Otherwise, consider a third party tool, such as Power Manager, which can run scripts as the active user. I am an engineer behind Power Manager, so feel free to get in touch and ask any technical questions about how it works.