Apple - Disable sleep mode for a specific app?

If you have any interest in running shell script the following will work. Just set the "Display_Sleep_Time" and "System_Sleep_Time" as you want them to be along with the "App" you are choosing to prevent sleep on. By launching the script anytime you run your app your all sleep times will be turned to 0 preventing both your display and system from sleeping.

TESTED ON

  • 10.5.x
  • 10.6.x
  • 10.7.4

NOTE: WARNING THIS SCRIPT DOES CONTAIN AN INTERNAL LOOP. You may prefer to utilize the following script inside a Daemon rather than use the internal loop the script provides. However, the same concept applies.

#!/bin/bash

watch="true"

App="Safari"
System_Sleep_Time="30"
Display_Sleep_Time="10"

System_Sleep=$(pmset -g | grep " sleep" | awk '{print $2}')
Display_Sleep=$(pmset -g | grep " displaysleep" | awk '{print $2}')

while [[ ${watch} == "true" ]]; do

    watch_App=$(ps -A | grep "${App}.app" | grep -v grep | sed "s/.*${App}.*/${App}/")

    if [[ ${watch_App} == ${App} ]]; then

        if [[ ${System_Sleep} != "0" ]]; then
            pmset -a sleep 0
            System_Sleep=$(pmset -g | grep " sleep" | awk '{print $2}')
        fi

        if [[ ${Display_Sleep} != "0" ]]; then
            pmset -a displaysleep 0
            Display_Sleep=$(pmset -g | grep " displaysleep" | awk '{print $2}')
        fi

    elif [[ ${watch_App} != ${App} ]]; then

        if [[ ${System_Sleep} == "0" ]]; then
            pmset -a sleep ${System_Sleep_Time} > /dev/null 2>&1
            System_Sleep=$(pmset -g | grep " sleep" | awk '{print $2}')
        fi

        if [[ ${Display_Sleep} == "0" ]]; then
            pmset -a displaysleep ${Display_Sleep_Time} > /dev/null 2>&1
            Display_Sleep=$(pmset -g | grep " displaysleep" | awk '{print $2}')
        fi

    else

        /usr/bin/logger -i An error has occured with preventsleep.sh

    fi

done