News, info, and opinion by Mac users, for Mac users.

July 7, 2008

tips

Automatically rotate iChat status with AppleScript

Posted Jul. 7, ’08, 4:32 PM PT by Dan Pourhadi
Category | Tips

applescriptiChat1.jpgI’ve been messing around a lot lately with AppleScript. Why? Because I like being bossy and controlling, and what better way to flex my dictatorial muscle than by ordering my computer to perform various actions using blunt and non-negotiable commands without so much as a “please” or “thank you.”

To see what this programming prose can do, I began developing developing solutions to all the “I wish I could…”s in OS X that I’ve built up over the years. The first was just for fun: I wanted a way to automatically change my iChat away status, at a certain interval, to a random selection from a list of messages stored in a text file—one per line—on my Mac.

So if I like using funny quotes as my away messages—which I do—instead of having to change it manually every time I’m bored of an old quote, I use an AppleScript to automatically change my status to a random quote every twenty minutes.

Neat? Yes.

Check after the jump for the AppleScript code, and a line-by-line analysis to help you understand how it works.

Here’s what the AppleScript looks like, to get a feel for what we’re dealing with:

set messagesFile to choose file with prompt "Select a text file:"
open for access messagesFile
set messagesList to every paragraph of (read messagesFile)

set list_count to the count of messagesList

repeat
set pick to random number from 1 to list_count
tell application "iChat"
set status message to item pick of messagesList as string
end tell
delay 1200
end repeat

close access messagesFile

So how does it work, and how do you use it? Let’s dive in.

First, open Script Editor, located in /Applications/AppleScript—we’ll use this to write and run our iChat AppleScript. Copy the code above—or, for bonus points, rewrite it manually—into a blank document.

You also need a text file that contains the list of status messages you want to use—one per line.

To understand what the script is supposed to do, we need to visualize a basic step outline for what we’re trying to accomplish.

  1. Open and read the text file with the list of messages.
  2. Randomly pick one of the messages.
  3. Set iChat’s status to that message.
  4. Wait for 20 minutes.
  5. Repeat.

Keeping up? Sorta? Okay, let’s keep going.

Step One

Step One in our outline is the first three lines in the AppleScript, and the very last line.

set messagesFile to choose file with prompt "Select the file with your list of status messages:"
open for access messagesFile
set messagesList to every paragraph of (read messagesFile)

The first line will pop up a dialog box when the AppleScript runs, asking you to choose the text file that contains your list of status messages. (This is nice because it allows you to have multiple text files each with a different list of status messages, so you’re not limited to one list every time you run the script.) It then stores the name and location of that file in the variable “messagesFile.”

The second line opens the file so AppleScript can access its contents.

And the third line reads the file, and creates a list of all the messages it contains—identified by paragraphs, or new lines—and stores them in the variable “messagesList.”

Now we have a variable in AppleScript that contains a list of each individual status message from your text file, so we can choose one and set it as your iChat status.

(The fourth line simply closes the file so it isn’t stored in memory the whole time the script is running.)

Step Two

Step Two is a combination of these two lines:

set list_count to the count of messagesList

and

set pick to random number from 1 to list_count

Here, we create a new variable that simply stores the number of messages that are contained in the messagesList variable (if there are 10 messages in the list, “list_count” will store the number 10). Then we use the “random number” function to generation a random number between 1 and the number of messages—so if there are 10 messages, it will pick a number, obviously, between 1 and 10. We then store that number in the variable “pick.”

Step Three

Step Three is how we get the AppleScript to deal with iChat:

tell application "iChat"
set status message to item pick of messagesList as string
end tell

By using “tell application,” we’re able to use terms specific to iChat to relay iChat commands. (For example, without the “tell application” line, how would AppleScript know what to do with the “set status message” command? “Set the status message of what?” it would ask, both frustrated and confused.)

The “set status message” line will use the random number stored in “pick” to select the corresponding message from the message list (if the pick was 5, it will select the fifth message from the list). It’ll then set iChat’s status to the selected message.

Step Four

The fourth step is pretty obvious:

delay 1200

The “delay” function pauses the AppleScript for the specified number of seconds—in this case, 1200 (60 seconds * 20 minutes).

Step Five

Step Five, if you haven’t already figured it out, is the “repeat” and “end repeat” lines that sandwich the chunk of the AppleScript that we need to repeat to make the script continually change iChat’s status to a new message.

We don’t include the lines to read the text file in the ‘repeat’ loop because we only need to read the file once to create the list of messages—it’s what we do with that list that needs to keep repeating.

Throw all of that together, then run the script, save it, or save it as an application and launch it whenever you want your status to change automatically.

This was a very high-level rundown of how this particular AppleScript works; so if you have any questions, post them in the comments and I’ll answer as best I can.

(If you have any suggestions to better the script, or different ways to accomplish the same task, please post those in the comments, too.)


Leave a comment

 




Visit other IDG sites: