Detect Wake on lan packets?

I am quite a newbie when it comes to applescripting so please bear with me.

I am running this script - Narcolepsy 2.0 on my iMac to make sleep work properly. If I don’t use it, my iMac will not go to sleep…

But it has side effects. It does not detect when I am accessing a file (a tv recording) on the iMac from my media center. It goes to sleep…
The easy fix would be to extend the period in which the iMac will NOT go to sleep (in System Preferences), but is there another way?

Is it possible to have applescript detect if the iMac receives a magic packet (wake on lan) and then prevent sleep?

Thanks a lot!

So as for stopping Narcolepsy from sleeping your mac, get it to start itunes. Apparently that stops it. As for detecting network activity, how’s this:

set in1 to do shell script "(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep lastTxRate: | sed 's/.*: //')"
--(ifconfig en0 | grep media: | sed 's/.*(//' | sed 's/ .*//') for wired network
set in1 to in1 as number

if in1 < 70 then -- That's MBit/s
	--atm, there is little network activity.
else
	--atm, there is more network activity.
end

Adjust as necessary.

Thanks a lot for your reply and your effort to help.

But since I am trying to use this script on an iMac (on a wired network, running OSX 10.8.2), I suppose I can’t use the airport utility via cli?

I only get “command not found” when trying: $ airport -help or $ airport --getinfo for instance…

Could you use netstat somehow? Monitor the amount of packets in and out of en0?