Mac Sleep Changer Script

Posted: 2009-06-17 09:02:19
Modified: 2010-02-27 06:46:49

I've only tested this with Leopard on my MacBook Pro 2,2. Run at your own risk.
#!/bin/bash
#Copyright Tony Speer 2009
currentsleep="`pmset -g | grep hibernatemode | awk '{print $2}'`"

if [ -f .osleep ]
then
    osleep="`cat .osleep`"
else
    osleep="$currentsleep"
    echo $osleep > .osleep
fi

psleepmode() {
    case $1 in
    0)
        echo -ne "0\tRAM powered on while sleeping, safe\n\tsleep disabled, and super-fast wake."
    ;;
    1)
        echo -ne "1\tHibernation mode, with RAM contents\n\twritten to disk, system\n\ttotally shut\n\tdown while sleeping."
    ;;
    3)
        echo -ne "3\tRAM saved to HDD while sleeping incase\n\tof power interuption and ram powered\n\twhen sleeping."
    ;;
    esac
}

ppsleepmode() {
    psleepmode $1
    if [ "$currentsleep" -eq "$1" -a "$osleep" -eq "$1" ]
    then
        echo -n " <- Current mode and orginal mode"
    else
        if [ "$currentsleep" -eq "$1" ]
        then
            echo -n " <- Current mode"
        fi
        if [ "$osleep" -eq "$1" ]
        then
            echo -n " <- Orginal mode"
        fi
    fi
    echo ""
    echo ""
}

if [ "$1" ]
then
    echo -n ""
else
    clear
    echo "Sleep Modes:"
    ppsleepmode 0
    ppsleepmode 1
    ppsleepmode 3
    
    echo "Other options:"
    echo ""
    
    if [ -f /private/var/vm/sleepimage ]
    then
        echo -ne "x\tRemove sleep image."
        echo ""
        echo ""
        echo "...."
        echo ""
        echo "Sleep image exists."
    else
        echo "...."
    fi
    
    echo ""
    echo -n "What is your selection? q to quit doing nothing.  "
fi
if [ "$1" ] # detect if a cli arg was given and if so then use that instead of user input
then
    sel="$1"
else
    read sel
fi

echo ""

case $sel in
    0)
    ;;
    1)
    ;;
    3)
    ;;
    x)
        if [ "$currentsleep" -eq "0" ]
        then
            if [ -f /private/var/vm/sleepimage ]
            then
                echo "Deleteing Sleep image."
                sudo rm -i /private/var/vm/sleepimage
                exit 0
            else
                echo "No sleep image to delete."
                exit 0
            fi
        else
            echo "I can't delete the sleep image while it is in this mode."
            exit 0
        fi
    ;;
    q)
        echo "Good bye."
        exit 0
    ;;
    *)
        echo "Unkown selection. Doing nothing..."
        exit 0
    ;;
esac

echo "Setting mode."
sudo pmset -a hibernatemode $sel

echo "Done."

Comments



blog comments powered by Disqus