Applescript for PleaseSleep

Discussion in 'Mac Programming' started by robert-a-hudson, Sep 28, 2013.

  1. robert-a-hudson macrumors regular

    Apr 11, 2006

    My Mac (10.6.8) seems to forget to send itself to sleep after a week or so. I've found the application 'PleaseSleep' which seems to successfully sleep my macbook. However, I sometimes have my macbook using eyeTV or sharing the wifi to my bluray player and so I want to prevent it sleeping when these activities are going.

    Now theoretically, PleaseSleep supports this, by allowing the user to set up an exceptions script which must return false if the computer can sleep, or true if you want the computer to stay awake. I have managed to get a script working for eyeTV:

    set Variable to false as boolean
    --Stay awake if iTunes is playing
    tell application "iTunes"
    	if it is running then
    		if player state is not stopped and player state is not paused then set Variable to true
    	end if
    end tell
    --Stay awake if EyeTV is playing or recording
    tell application "EyeTV"
    	if it is running then
    		if playing is true then set Variable to true
    		if is_recording is true then set Variable to true
    	end if
    end tell
    return Variable
    So next comes the problem. I want to add a test based on network activity (my bluray is connected via ethernet to my macbook which shares its wifi). So i've added the following:

    This should just do a check on the netstat value, and if it's increased (i.e. there is data transfer through en0) then assume that's because the bluray player is using the internet, and don't sleep. Now, when I run the command in terminal, this works. However for some reason in the applescript above this doesnt seem to work (I think it's the If statement not setting 'Variable' to true that's the problem, but am not 100%).

    So can anyone help with my apple script? Or does anyone have a more elegant solution for checking if en0 is in use via an applescript?

  2. Red Menace macrumors 6502

    Red Menace

    May 29, 2011
    Littleton, Colorado, USA
    The results from the do shell script statement is text, so you will need to check for that:

    if (do shell script "if [ `netstat -i -I en0 -b | awk '{print $10}' | awk 'NR==2'` = `sleep 20; netstat -i -I en0 -b | awk '{print $10}' | awk 'NR==2'` ]; then echo false; else echo true; fi") is "true" then set Variable to true

Share This Page