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 .
Thanks

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.
Larry

Without UI-Scripting:

Shell-Script:

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

AppleScript:

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

Nice. Thanks Dirk,
Where is GloballyEnabled documented?

1 Like

No idea :man_shrugging:

Toggle Showing window apps one by one or all together:

set currStageManagerAppWindowGroup to (do shell script "echo $(defaults read com.apple.WindowManager AppWindowGroupingBehavior 2> /dev/null)") as integer
if currStageManagerAppWindowGroup is 0 then
	do shell script "defaults write com.apple.WindowManager AppWindowGroupingBehavior -bool true"
else
	do shell script "defaults write com.apple.WindowManager AppWindowGroupingBehavior -bool false"
end if

Toggle Hide Desktop Icons:

set currStageManagerHideDesktop to (do shell script "echo $(defaults read com.apple.WindowManager HideDesktop 2> /dev/null)") as integer
if currStageManagerHideDesktop is 0 then
	do shell script "defaults write com.apple.WindowManager HideDesktop -bool true"
else
	do shell script "defaults write com.apple.WindowManager HideDesktop -bool false"
end if