Wednesday, May 5, 2010

Getting EyeTV to Put Your Mac to Sleep ... Intelligently!

One feature that I _really_ miss in EyeTV is the ability to put my Mac to sleep after a recording. It is possible to write an AppleScript that reacts to the RecordingDone signal in EyeTV, but my previous foray into that area ended up with a simple script that said something like
on RecordingDone(recordingID)
tell application "Finder" to sleep
end RecordingDone
This works to some extent but it does have some serious flaws. First and foremost it ruins your second recording if you have scheduled to record two programs back-to-back. In that case EyeTV records the first program, emits the RecordingDone signal, starts recording the second program, and about five seconds into that recording the computer goes to sleep ;-) Another problem with this approach is when you are recording something while you are working on the computer - in that case your computer suddenly goes to sleep while you are working.

This has annoyed me quite a bit, but seemingly not enough for me to actively search for a better solution - until today. Googling around a bit I found this script: http://forums.elgato.com/viewtopic.php?f=91&t=1045#p18834. I have altered it a tiny bit to change the timeouts and to put the computer to sleep instead of powering it off. The result is here:
on RecordingDone(recordingID)
set isCurrentlyRecording to check_eyetv_is_recording()
if isCurrentlyRecording is false then
with timeout of 300 seconds
tell me to activate
display dialog "Warning: EyeTV has now finished recording and the system will automatically shut down in 5 minutes unless you click Stop!" with icon 2 buttons {"Stop!"} giving up after 300 default button "Stop!"
set theresults to the result
if gave up of theresults is true then
tell application "System Events" to sleep
end if
end timeout
else
quit
end if
end RecordingDone

to check_eyetv_is_recording()
delay 30
tell application "EyeTV"
if is_recording then
return true
else
return false
end if
end tell
end check_eyetv_is_recording
This script works perfectly! When the recording is done a dialog is displayed telling you that the computer will be put to sleep in five minutes, and if you press the stop button you can keep the computer awake.

Update: A guy called Michael asked we where to put the script to make it work. You are supposed to save the scripts as /Library/Application Support/EyeTV/Scripts/TriggeredScripts/RecordingDone.scpt

4 comments:

  1. Hi:

    I tried your script above on my Mac Mini and it works great when the TV is on. However, when the TV is off and EyeTv is finished recording, the script doesn't seem to trigger. Is there something I can add to the script to make to make it work when the TV is off?

    Thanks

    ReplyDelete
  2. That's very strange. The script is in no way tied to the TV - it shouldn't matter whether the TV is on or off.

    ReplyDelete
  3. one thing that's missing from the applescript dictionary is "is_exporting"...

    if you want to have the machine sleep after an export is done, you can change this script to "on ExportDone(...)." the problem is that if there's another export going on when this one triggers, you'll sleep the machine while it's still busy because there's no way to test if eyeTV is currently exporting.

    you can check if a particular recording ID is busy, but i'm not sure how you make these scripts stateful... that is, when a recording starts, if you could remember the recording id and somehow pass it to the ExportDone script, you could check that every outstanding recording is done exporting before sleeping...

    ReplyDelete
  4. @hermeticseal: True. I never considered that though, seeing as I have never used the export feature in EyeTV.

    ReplyDelete