Synopsis
One way to approach this is to:
- Obtain the current date/time plus x number of seconds in the future.
- Schedule the system/display to "wake" in the future using the date/time obtained at point 1.
- Then set your display to sleep.
Solution
Consider utilizing the following AppleScript to meet your requirement:
set wakeAfterSeconds to "15"
set myPassword to "xxxxxxxxxx"
set now to do shell script "date -r $(date +%s) '+%m/%d/%y %H:%M:%S'"
set wakeTime to do shell script "date -jv +" & wakeAfterSeconds & "S -f " & ¬
quoted form of "%m/%d/%y %H:%M:%S" & " " & quoted form of now & " " & ¬
quoted form of "+%m/%d/%y %H:%M:%S"
do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
" && pmset displaysleepnow" password myPassword with administrator privileges
Code Explanation
The line that reads;
set now to do shell script "date -r $(date +%s) '+%m/%d/%y %H:%M:%S'"
essentially executes the following bash date
command, using the Applescript do shell script
command, to obtain the current date/time:
$ date -r $(date +%s) '+%m/%d/%y %H:%M:%S'
This assigns the current date/time, formatted as MM/DD/YY HH:MM:SS
, to the variable named now
.
The lines reading;
set wakeTime to do shell script "date -jv +" & wakeAfterSeconds & "S -f " & ¬
quoted form of "%m/%d/%y %H:%M:%S" & " " & quoted form of now & " " & ¬
quoted form of "+%m/%d/%y %H:%M:%S"
obtain a future date/time by adding x no. of seconds to the current date/time (i.e. it adds 15 seconds). This future date/time, (which again is computed using the shells date
utility), is assigned to the variable named wakeTime
.
The lines that read:
do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
" && pmset displaysleepnow" password myPassword with administrator privileges
utilize the pmset
utility to perform the following two tasks:
- Schedules the display to "wake" at a future date/time, i.e. after 15 seconds.
- Sets the system to "sleep" now.
Additional notes:
You'll need to set the no. of seconds assigned to the wakeAfterSeconds
variable to a value which is appropriate for your system. You may find that you need to increase, or decrease, this value.
In essence the number of seconds that you set the wakeAfterSeconds
value to must be longer than the number of seconds it takes for your display to enter sleep mode - it's got to be asleep before it can be woken !
Setting a shedule to wake your computer using the pmset
utility must be run as root, therefore it requires your password. So you'll need to set the value of the myPassword
variable as necessary.
You mentioned;
... if I manually force the screen to sleep and wake a couple of times it does light up.
In which case you may want to consider wrapping the aforementioned code in a repeat
statement.
For instance, the following repeats the process twice:
set wakeAfterSeconds to "15"
set myPassword to "xxxxxxxxxx"
repeat 2 times
set now to do shell script "date -r $(date +%s) '+%m/%d/%y %H:%M:%S'"
set wakeTime to do shell script "date -jv +" & wakeAfterSeconds & "S -f " & ¬
quoted form of "%m/%d/%y %H:%M:%S" & " " & quoted form of now & " " & ¬
quoted form of "+%m/%d/%y %H:%M:%S"
do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
" && pmset displaysleepnow" password myPassword with administrator privileges
delay wakeAfterSeconds
end repeat
Utilize "Login Items" to auto run the application:
Also, you may want to consider adding the aforementioned AppleScript application to your Login Items. This would automatically run the application when you start-up your computer. To do this:
- Firstly ensure the scripts "File Format" has been saved as an "Application" via your AppleScript Editor.
- Launch "System Preferences" and select "Users & Groups".
- Select your user account in the panel on the left side, and click "Login Items"
- Click the plus icon
+
and select the AppleScript Application.
- Relaunch your computer.

Older Mac OSX:
On older versions of macOS (namely OSX) the pmset
utility did not include a displaysleepnow
command. It just had the pmset sleepnow
command. So, for those wanting to run this on older OSX you'll need to change the last do shell script
command to:
do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
" && pmset sleepnow" password myPassword with administrator privileges
^^^^^^^^^^^^^^