Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

robert-a-hudson

macrumors regular
Original poster
Apr 11, 2006
134
0
Britain
Hello,

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:

Code:
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:

set BluRayInUse to true as boolean

--Stay awake if BluRay is using network
--If network is not being used then BluRayInUse will return False
set BluRayInUse to do shell script "if [ `netstat -i -I en0 -b | awk '{print $10}' | awk 'NR==2'` = `sleep 60; netstat -i -I en0 -b | awk '{print $10}' | awk 'NR==2'` ]; then echo false; else echo true; fi"
if BluRayInUse is true then
set Variable to true
end if
--

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?

Thanks,
Robert
 

Red Menace

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

Code:
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.