1

There is a problem with the backlight of my mac. Sometimes when I turn it on the backlight does not come on. However if I manually force the screen to sleep and wake a couple of times it does light up. So I want to write an applescript to automate this process. I have looked at ... How do I wake from display sleep in OSX 10.7.4? But that is not written in Applescript (or if it is I cannot figure out how to run it.

I have tried the following

do shell script "pmset displaysleepnow"
tell application "Google Chrome"
activate
end tell
do shell script "pmset displaysleepnow"
tell application "Google Chrome"
quit
end tell

But the screen blanks and never relights. I have also tried using caffeine as per some questions about this. I have tried using code to move the mouse and/or click the mouse programmatically.

If I just run the displaysleepnow followed by the 'activate chrome' that works, but if the displaysleepnow is executed again nothing appears to reactivate it.

I am using macOS 10.14.4

Melchester
  • 421
  • 3
  • 18

1 Answers1

2

Synopsis

One way to approach this is to:

  1. Obtain the current date/time plus x number of seconds in the future.
  2. Schedule the system/display to "wake" in the future using the date/time obtained at point 1.
  3. 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

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

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

  3. 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:

  1. 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 !

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

  3. 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:

    1. Firstly ensure the scripts "File Format" has been saved as an "Application" via your AppleScript Editor.
    2. Launch "System Preferences" and select "Users & Groups".
    3. Select your user account in the panel on the left side, and click "Login Items"
    4. Click the plus icon + and select the AppleScript Application.
    5. Relaunch your computer.

    enter image description here

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
       ^^^^^^^^^^^^^^
RobC
  • 22,977
  • 20
  • 73
  • 80