Section 9.5. iChat Control


9.5. iChat Control

Instant messaging, a sort of real-time email system, is one of the most popular developments of the Internet age. You can chat with your grandma in Washington D.C., your dad in Wichita, or your sister in Walla Wallaall for free. In fact, instant messaging has become so popular that Apple developed its very own chat program: iChat.

If you've never signed up for an instant messaging account, visit www.aim.com and click Starting Out Registration on the left side. Follow the online procedure, and in a few minutes, you'll end up with a free screen name (online identity) that you can use with iChat.

Now, iChat isn't just any chat program; it supports voice and video chats, too (assuming you have a microphone or video camera). Unfortunately, iChat's AppleScript support reaches only into the realm of text messagingyou can't automatically snap pictures with your Webcam, for example.

Still, if you're an AppleScript addict, there are several opportunities to sharpen your skills on iChat's text features. For example, you can let your online buddies know you're not available when your screen saver's running, or keep your buddies entertained with an endless parade of pithy quotes. Read on for the secrets.

9.5.1. Notifying iChat when the Screensaver's On

One of iChat's coolest features is that it lets you notify your friends as to whether you're around or not. When you're at your computer, you set your iChat status to available; when you're out, you set your status to away (and if, for some unthinkable reason, you actually disconnect from the Internet for once, you set your status to offline). Conveniently for you, AppleScript can do the same thing:

tell application "iChat"     set the status to available --Or you can use away, or offline end tell

Of course, that's nothing really unique. To add real power, you can have your script set your status to away only if your screen saver is running.

To enable your screen saver, visit System Preferences Desktop & Screen Saver.

Here's how:

--Part 1: on idle     --Part 2:     tell application "System Events"         if (the name of every process) contains "ScreenSaverEngine" then             --Part 3:             tell application "iChat"                 set the status to away             end tell         end if     end tell     --Part 4:     return 10 end idle

Now, before your script will run properly, you have to select File Save. For the File Format, choose Application, and turn on the Stay Open checkbox, so your script continues running in the background all the time. Save the file wherever you wantbut try to make it somewhere that's easy to access, because you can only run the script by double-clicking its icon in the Finder.

Here's how the code works:

  • Part 1 is an idle handler. To AppleScript, that means, "run the following code whenever this script isn't busy doing something else." To you, it means the script will run constantlyunlike most of your other scripts, which run and then quit themselves.

idle handlers only work if you've turned on "Stay Open" in a script's Save dialog box.

  • Part 2 tells System Events to get a list of all currently running programs on your system. Then the script checks whether that list contains ScreenSaverEngineMac OS X's program in charge of running screen savers.

    In other words, part 2 checks if your screen saver is running at the moment. If it is, the script proceeds to part 3.

  • Part 3 sets your iChat status to away if your screen saver is running. That way, your friends won't try to chat with you when your screen saver is blocking your chat windows.

  • Part 4 tells the idle handler to check back again in 10 seconds. The end result is that your script checks every 10 seconds whether your screen saver is runningand, if it is running, the script sets your iChat status to away.

Once you turn your screen saver off, set your status back to available so your friends know that you're ready to chat again.

When your screen saver isn't playing, the only indication you'll have that your script is running is its icon in the Dock. Since you turned on Stay Open for the script, your code continues checking your screen saver forever (or at least until you Control-click its icon in the Dock and choose Quit from the shortcut menu).

9.5.2. Displaying a Random Status Message

In addition to telling your buddies what your status is, you can post a special message to your buddies, using iChat's status message setting. You might post some information on how to reach you while you're away, a pithy quote, or a link to a Web site you find particularly interesting.

Normally, you set your status message by clicking the small pop-up button at the top of iChat's Buddy List window (Figure 9-8). However, using AppleScript, you can set your status message automatically.

Figure 9-8. Click this button to bring up a menu of status messages. If you don't like any of the built-in choices, choose Custom. (Status messages with green circles indicate that you're available; status messages with red squares indicate that you're away.)


Try a script like this:

tell application "iChat"     set the status to away     set the status message to ¬         "I don't want to talk to you right now. Go away." end tell

Of course, if you run that script and don't change your status message for a while, your buddies will get both bored and annoyed at your rudeness. Nowadays, not changing your status message regularly is the equivalent of not changing your bedsheets.

That's why changing the script to rotate among a few status messages is such a convenient modification. Edit the script as follows, and your witty, rotating status messages will entertain your friends even when you don't have time to:

--Part 1: global messageList --Part 2: on run     set messageList to {"Don't leave your seat, 'cause I'll be back soon", ¬         "Stay Tuned!", ¬         "The stuff I'm doing right now is more important than talking to you"} end run --Part 3: on idle     tell application "iChat"         set the status to away         set the status message to (some item of messageList)     end tell     --Part 4:     return 600 end idle

Since your script uses the idle handler, you have to save it as an application, using the procedure from Section 9.5.1. Once you've done that, you can run the script by double-clicking its icon in the Finder.

Here's how the parts breaks down:

  • Part 1 is what's known as a declaration. Essentially, the global keyword tells AppleScript, "The variable name that follows should be accessible anywhere in this script." If you left out this part, AppleScript would get confused when you used the messageList variable down in part 3, because it wouldn't know that you were referring to the same variable as in part 2.

  • Part 2 uses AppleScript's run handler to define what happens when you double-click the script's icon in the Finder. In this case, the script simply prepares the messageList variable with a list of possible status messages, in anticipation of the list being used in part 3.

Feel free to customize messageList with any number of messages you want.

  • Part 3 uses an idle hander, just like the iChat script on Section 9.5.1. In this case, however, your script picks a random message from messageList each time the idle handler runs, and then posts that random message in iChat. Think of it like a Magic 8 Ball, on a timer, for iChat.

  • Part 4 simply tells AppleScript to check back with your idle handler in 10 minutes (or, since AppleScript can only think in seconds, 600 seconds). At that point, your script starts at part 2 all over again, rotating in a new status message.

Your script runs the idle handler every 10 minutes until you physically quit the script. You can do that in one of two ways: click the script's icon in the Dock and choose [Script's Name] Quit from the menu bar, or Control-click (or right-click) the script's icon in the Dock and choose Quit from the shortcut menu.

If you like to keep your buddies posted on what you're doing, scripting iChat's status message is definitely the easiest way. Luckily, other people have already created some more scripts for you; the free iChat Script Collection (http://cocoaobjects.com/applescript/ics/index.php), for example, includes several scripts for posting a status message based on your currently playing iTunes track.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net