Stage Manager control from AppleScript

With macOS 13 Apple added the Stage Manager capability. However I have found that leaving Stage Manager turned ON overnight when I wake my M1 iMac in the morning the system has panicked overnight. I always send the report to Apple before I do anything else.
I wand to use AppleScript to write a procedure that will turn Stage Manager off at a specified time in the evening. I would turn it back on in the morning.
Does anyone know of an existing AppleScript that addresses Stage Manager or of what objects related to Stage Manager are available to AppleScripts? I am running Ventura 13.3 .

I believe UI scripting in the only way to do this. The following script opens the Control Center window and toggles Stage Manager on/off.

tell application "System Events"
	set theProcess to first application process whose name is "ControlCenter"
	click menu bar item 2 of menu bar 1 of theProcess
	perform action 1 of button 1 of group 1 of window 1 of theProcess
end tell

Thanks very much Stephen.

Without UI-Scripting:


STATE=$(defaults read GloballyEnabled 2> /dev/null)
[[ ${STATE} = 0 ]] && ENABLE="true" || ENABLE="false"
defaults write GloballyEnabled -bool ${ENABLE}


set currStageManagerMode to (do shell script "echo $(defaults read GloballyEnabled 2> /dev/null)") as integer
if currStageManagerMode is 0 then
	do shell script "defaults write GloballyEnabled -bool true"
	do shell script "defaults write GloballyEnabled -bool false"
end if

Nice. Thanks Dirk,
Where is GloballyEnabled documented?

No idea :man_shrugging: