There's never enough time in the day to watch all those great TV shows you want to see. Turning your computer into a personal video recorder, or "PVR," has become all the rage. A Macintosh PVR can accomplish many things. At the most basic, it lets you time-shift your shows to watch them at a convenient hour—when you're ready to watch, not when network executives think you should be watching.

In addition, you might record a show for a friend, and then ftp it over, or perhaps send it in segments through e-mail to a Gmail account. You could even let your Mac record all the great shows you think you should watch and then just trash the results. Even if you didn't see the show, your Mac did—and it probably enjoyed it more than you would have anyway. No matter why you want to use your Mac to record TV, HackTV (with a little help from iCal) provides a free and simple automatic video recording solution.

This is the second article in a series about watching TV with Apple's (free) HackTV utility. In the first article, I showed readers how to connect their TVs and other video sources to their Macintoshes using FireWire and watch it with HackTV. Then I discussed how to record that video to disk. This article continues from there.

You're about to learn how to convert your Macintosh to a low-end but working PVR. You'll discover how to control HackTV through Apple's GUI scripting extensions for AppleScript (at least as much as Mac OS X will let you) and how to schedule your recordings with iCal. After mastering the skills in this article, you'll be able to walk away from your Mac and let it handle any recording tasks for you.

Understanding HackTV Limits

In the best of all possible worlds, your Macintosh might wake itself up on a Tuesday evening after checking the latest updates of your local TV listings, reach out through an IR blaster attached to its USB port, tune your digital cable box to the local CBS affiliate, and automatically start recording "The Amazing Race," remembering to start and end the recording two minutes earlier than the listed times.

This article does not concern itself with the best of all possible worlds. It's about cheap and convenient hacks.

Keeping that in mind, you can only expect so much when using your HackTV setup to record video. Here are a few reminders.

Despite these caveats, it is possible to schedule and record with HackTV as you'll learn in this article.

Laying the Groundwork

Do you remember that great scene in Billy Crystal's "City Slickers" movie? During the long cattle drive, actors Crystal and Bruno Kirby attempt to explain to Daniel Stern's character exactly how one can watch one show on TV while recording another. HackTV is kind of like that. When making your connections to HackTV, you have to decide whether to watch the signal that's already playing on your TV or use a different tuner to record a second show.

If you receive satellite TV, the decision is more or less already made for you. Unless you want to fork out the money for a separate receiver, you'll need to hook HackTV to the extra output jacks on the back of the receiver box. (Satellite boxes almost always offer a second set of jacks.) What you watch on TV, you'll watch on HackTV.

If you subscribe to cable, you can be more flexible. Although digital cable boxes present the same problem as satellite TV—you need a second box to get a different signal—you can split standard cable signals at will and tune them separately. Signal splitters are available for very little money at most hardware stores. Many dollar stores even carry them.

To split your signal, use coaxial cable to connect your wall jack to the splitter and then to connect the splitter's output jacks to your TV and to your extra tuner. I highly recommend digging out an older VCR from storage (or from a local Goodwill store) to provide an extra tuner. Even broken VCRs work. They don't have to play VHS tapes properly to tune TV signals; these are entirely separate issues. As you'd expect, using a split signal with a second tuner lets you watch one show on TV and record a different show on HackTV.

An inexpensive cable splitter and a cheap VCR provide a great (and low-cost) solution that allows you to watch one show on TV and record a different show to HackTV. An inexpensive cable splitter and a cheap VCR provide a great (and low-cost) solution that allows you to watch one show on TV and record a different show to HackTV.

Setting Up HackTV

HackTV is, at best, barely scriptable. It's old. It's Carbon. It's not very compliant with the new OS X GUI scripting additions. You can make it start recording. You can make it stop recording. You can even tell it what file to record to. But you can't really automate the video settings—and believe me, I've tried. These limitations mean that HackTV must already be running with its video and sound options set before you start automatic recordings. (Note: Apple introduced GUI scripting in response to programmer demand for AppleScript control of applications that were not specifically written with AppleScript support. GUI scripting lets you programmatically generate interface events such as clicking buttons or selecting items from menus.)

Follow these steps to make sure that HackTV is ready for your recording.

  1. Open System Preferences -> Universal Access. Make sure that Enable access for assistive devices has been checked. GUI scripting will not work unless this option is enabled.
  2. Connect your video signal and launch HackTV, making sure that the HackTV monitor correctly displays your signal. (Setting up the connection is covered in the first article in this series.)
  3. Set your tuner to the channel you want to record. If you want to record channel 61 at 5:00 in the morning, then make sure your signal is tuned to channel 61 now.
  4. Use the Monitor menu to select your window size. The size you pick is the size at which you'll record.
  5. Choose your Video and Sound settings. (Setting up the connection is covered in the first article in this series.)
  6. Select HackTVCarbon -> Hide HackTVCarbon (Command-H). This cleans up your workspace and hides HackTV until it's needed.

Prepare Your Recording Script

After setting up HackTV, you'll need to prepare the AppleScript script that runs the recording. For this you'll use Apple's Script Editor application. In the following steps, you'll learn how to create and customize this script.

  1. Navigate to your Applications directory. There you'll find an AppleScript folder. Open it and look for the Script Editor application inside.
  2. Launch Script Editor. A new Untitled window opens.

    A typical Script Editor window A typical Script Editor window
  3. Paste the following code into the top pane of the Untitled script editor, just under the toolbar. This script tells HackTV to record for a certain number of minutes that you specify.

    -- ------------------------ 
    -- Record via HackTV 
    -- ------------------------ 
    
    -- Specify the recording duration 
    set howmanyminutes to 30 
    
    -- Set the recording name 
    set tw to words of ((current date) as string)
    set rn to getName() 
    
    -- Part I: Activate HackTV and Start Recording.
    tell application "HackTVCarbon"
        activate 

    delay 4

    tell application "System Events" tell application process "HackTVCarbon" click menu item "Record Without Hogging Machine" of menu "Monitor" of menu bar item "Monitor" of menu bar 1 keystroke rn -- name to save as click button "Save" of window "Save" end tell end tell end tell -- Pause for however many minutes of recording delay howmanyminutes * 60 -- Part II: End the recording tell application "HackTVCarbon" activate tell application "System Events" tell application process "HackTVCarbon" click menu item "Stop Recording" of menu "Monitor" of menu bar item "Monitor" of menu bar 1 delay 2 -- wait for "finished" dialog to appear keystroke return end tell end tell end tell -- Return a name for the file based on current date & time on getName() set datewords to words of ((current date) as string) set rstring to (item ((month of (current date)) as integer) of {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}) set rstring to rstring & (((day of (current date)) as integer) as string) set rstring to rstring & "-" set rstring to rstring & item 5 of datewords & "." & item 6 of datewords & item 8 of datewords & ".mov" return rstring end getName
  4. Click the Compile button in the window's toolbar. The Script Editor compiles the code and updates the fonts to the multicolored result seen here.

    The multicolored format (called "pretty printing") indicates that the Script Editor has successfully compiled your AppleScript code. The multicolored format (called "pretty printing") indicates that the Script Editor has successfully compiled your AppleScript code.
  5. Look for the "set howmanyminutes to 30" line, toward the top of your script. This sets the recording time to 30 minutes. Edit this number to reflect the actual length of your recording.
  6. Choose File -> Save As (Command-Shift-S). A Save dialog appears.
  7. Type "MyHackTVScript" in the Save As text entry field, and save the script to your desktop.
    Save your script to your desktop. Save your script to your desktop.
  8. Take a minute or two to review the script you just created. This script instructs HackTV to record video for a certain number of minutes. The process (recording without hogging machine, specifying a file name, waiting for the time to pass, and stopping the recording) exactly mirrors the procedure from the previous article. The difference is that here the script does the work for you. Instead of choosing each menu item yourself, the script proceeds automatically.

Scheduling a Recording with iCal

With iCal, you can record TV at any time you want. iCal alarms make it possible. All you have to do is schedule an AppleScript by adding it to your calendar as a new event. Here's how.

  1. In your Applications folder, double-click iCal to launch the program.
  2. In iCal, choose File -> New Event (Command-N). iCal creates a new event in your schedule.
  3. Select the new event and drag it to the day and time you want the recording to start.
  4. Choose Window -> Show Info (Command-I). The Info drawer slides open.
  5. In the Info drawer, edit the name of the event and fine-tune the start time as needed.
  6. Locate the alarm pop-up. (It's toward the bottom of the drawer, just above the calendar and url pop-ups). Click it.
  7. Choose "Open file" from the pop-up. The alarm updates, changing to three lines. The first line reads "Open file."
  8. Click the second line, just below "Open file." Another pop-up appears.
  9. Choose Other. A file browser opens.
  10. Navigate to your desktop, select MyHackTVScript, and click Select. This tells iCal to open MyHackTVScript as part of the event.
  11. The third line typically defaults to "15 minutes before." Click the number 15 (or whatever number appears there), edit it to 0, and press return. You want your script to run exactly at the time specified.

    Adding an iCal alarm lets you automatically run your HackTV script. Adding an iCal alarm lets you automatically run your HackTV script.
  12. Choose iCal -> Hide iCal (Command-H).

After adding this alarm to your event, you'll have instructed iCal to launch your script exactly at the time you specified. All you have to do now is sit back and wait—or even better, get up from your computer and go do something else. (Note: Make sure your System Preferences -> Energy Saver settings do not automatically put your computer to sleep. This will interfere with the recording.)

What to Expect

After your scheduled recording has finished, you'll find a new file on your desktop when you return to your computer. The file name, which is set in your script, is based on the day and time your recording took place. So if you set up your iCal event to repeat, each recording will bear a different name. To watch the recording, simply double-click the icon. QuickTime Player will launch and display the recorded video.

Your script tells HackTV to create a recording labeled with the date and time of the recording. Your script tells HackTV to create a recording labeled with the date and time of the recording.

Final Thoughts

This article has shown how the one-two punch of HackTV and iCal can turn your Mac into a simple personal video recorder. Hardly elaborate, HackTV still gets the job done—and it does it for free. You've seen how to prepare your recording and set it in motion. If all has gone well and according to plan, your new recordings will be waiting for you on your desktop when you return.


Return to digitalmedia.oreilly.com