In Mac OS X, is there a programmatic way to get the machine to go to sleep/hibernate? - macos

Is there some command line or AppleScript that I can write/run to make the machine go to sleep automatically or even better, into hibernate mode?
I can run the program as root.

osascript -e 'tell application "Finder" to sleep'

To go into Hibernate mode, you'll need to set the correct setting on the power manager.
Possible Settings:
0 - Old style sleep mode, with RAM powered on while sleeping, safe sleep disabled, and super-fast wake.
1 - Hibernation mode, with RAM contents written to disk, system totally shut down while “sleeping,” and slower wake up, due to reading the contents of RAM off the hard drive.
3 - The default mode on machines introduced since about fall 2005. RAM is powered on while sleeping, but RAM contents are also written to disk before sleeping. In the event of total power loss, the system enters hibernation mode automatically.
5 - This is the same as mode 1, but it’s for those using secure virtual memory (in System Preferences -> Security).
7 - This is the same as mode 3, but it’s for those using secure virtual memory.
So after all that, if you want to hibernate automatically and you don't have secure VM turned on then you could run the following shell commands (as a setuid root script):
pmset -a hibernatemode 1; osascript -e 'tell application "Finder" to sleep'

Stolen from this excellent collection:
Shut down without showing a confirmation dialog:
osascript -e 'tell app "System Events" to shut down'
Shut down after showing a confirmation dialog:
osascript -e 'tell app "loginwindow" to «event aevtrsdn»'
Restart without showing a confirmation dialog:
osascript -e 'tell app "System Events" to restart'
Restart after showing a confirmation dialog:
osascript -e 'tell app "loginwindow" to «event aevtrrst»'
Log out without showing a confirmation dialog:
osascript -e 'tell app "System Events" to «event aevtrlgo»'
Log out after showing a confirmation dialog:
osascript -e 'tell app "System Events" to log out'
Go to sleep (pmset):
pmset sleepnow
Go to sleep (AppleScript):
osascript -e 'tell app "System Events" to sleep'
Put displays to sleep (10.9 and later):
pmset displaysleepnow

The common shutdown utility has a -s option to schedule sleep, rather than a restart or shutdown.
$ sudo shutdown -s now
It does not appear to support hibernation, though.

Related

macOS version 12.3.1: how to quit ScreenSaverEngine by using AppleScript

I use code in mac shell,but it make my computer become black screen:
open -a ScreenSaverEngine.app; sleep 5; osascript -e 'tell application "ScreenSaverEngine" to quit'
Quitting the Screen Saver is easy, but loginning back will require the user password. You have to grant 2 permissions to Terminal.app as well, when first running the osacript bellow. 1) Permissions to control the System.Events.app, 2) permissions to control computer (that is, to be able to send keystrokes):
osascript -e 'tell application "System Events"
tell screen saver preferences
start
delay 5
stop
end tell
delay 1
keystroke "YourUserPasswordHere" & return
end tell'
Other way is: uncheck Require password after sleep or screen saver begins in the General tab of Security&Ptivacy pane of System Preferences. Then you don't need login back.
osascript -e 'tell application "System Events"
tell screen saver preferences
start
delay 10
stop
end tell
end tell'
Note: you can retain iCloud keychain in the pop up dialog.

How to make osascript display dialog while script continues other commands

So I'm looking at running lets say the following script
#!/bin/bash/
do
echo "Starting script"
osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer"'
do somecommands
done
I would like the display to stay on the screen while the "do somecommands" is running in the background without terminating that display. Is this possible?
Thanks
You can do it by running the osascript in the background like this:
#!/bin/bash/
echo "Starting script"
osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer"' &
# do other stuff
However, you will have 2 new problems - how to dismiss the dialog when you are finished and timeouts.
So, if you want to dismiss the dialog later, you will want to know its process id, so you should capture that after you start the background job like this:
osascript -e .... &
pid=$!
# Do some other stuff
# kill the dialog
kill $pid
Unfortunately, that doesn't seem to make the dialog go away - maybe someone else can help with that.
Secondly, if you are doing something time-consuming, the dialog will time out, so you may want to add a timeout like this for, say 100 seconds:
osascript -e 'tell app "System Events" to display dialog "Currently script is running, please do not use computer" giving up after (100)' &
Maybe that is the better way to do it anyway, run a loop and if the timeout expires and you are still busy, redisplay the dialog and if you are finished don't re-display the dialog - then you don't have the problem of how to make it go away at the end.

How to execute commands in new tabs?

I want to start two different databases but want to keep each process running in a separate tab in a terminal. How do I do this within a shell script? I currently have the following code:
#! /bin/bash
mysqld &
redis-server
Each database needs its own tab. This is on OSX.
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'tell application "Terminal" to do script "mysqld" in selected tab of the front window'
(based on https://stackoverflow.com/a/7177891/1566267)
The similar command is for redis-server.

Check if screen saver is running in Mac using AppleScript

Is there a way to check if the screen saver is running using AppleScript?
Currently I'm using the following AppleScript to start the screen saver:
tell application "System Events"
start current screen saver
end tell
I found this script but I don't know if that's correct or how to write that into my script.
What I want is something like this:
tell application "System Events"
if screen saver is not activated then
start current screen saver
end if
end tell
I know it's a bit late now but for others trying similar things.
I used the following recently in a shell script linked to a cronjob.
# check if screen saver is running
saverActive=$(osascript -e "tell application \"System Events\"" -e "get running of screen saver preferences" -e "end tell")
if [ "$saverActive" = "true" ]; then
echo "Screen saver is running"
osascript -e "tell application \"System Events\" to stop current screen saver"
osascript -e "tell application \"System Events\" to start current screen saver"
else
echo "Screen saver is not running"
fi
This script was used in the context of periodically changing the screensaver.
Try this...
tell application "System Events"
if not (running of screen saver preferences) then start current screen saver
end tell

Why do simple osascript commands fail in OS X Lion?

I have two very simple OSA scripts to allow logon and logoff of computers in a lab environment. These scripts work flawlessly in Snow Leopard when pushed via ARD, interactively within an ssh session, but they fail on machines running Lion.
Stripped down to its essentials, the logout script looks like this:
osascript -e 'tell application "System Events" to log out'
WORKS when run directly from an interactive shell on a machine
WORKS when pushed from ARD
FAILS with "execution error: The variable out is not defined. (-2753)" when run from an ssh session
WORKS when the script is compiled to an .scpt, then run from ssh (e.g. "/usr/bin/osacript logout.scpt")
The login script is directly based on this. A stripped-down version that exhibits the problem is:
osascript -e 'tell application "System Events" to keystroke "frontend"'
WORKS when run directly from an interactive session
WORKS when pushed from ARD
FAILS with execution error: An error of type -10810 has occurred. (-10810) when run from ssh
WORKS when run as a compiled scpt and run from ssh
Because these scripts work fine interactively, and because they worked fine in all modes in Snow Leopard, I think something must have changed in osascript, but I don't know what, and the error messages aren't very descriptive. Any suggestions would be welcome.
Try escaping the quotes.
So:
osascript -e 'tell application "System Events" to log out'
becomes:
osascript -e 'tell application \"System Events\" to log out'
And
osascript -e 'tell application "System Events" to keystroke "frontend"'
becomes:
osascript -e 'tell application \"System Events\" to keystroke \"frontend\"'
Give that a go and tell us what happens.

Resources