Organize archived iChats

In both my job-related and personal use of my Mac, I rely heavily on Apple’s iChat—I have daily chats with fellow Macworld staffers, and with my family and friends spread across the globe. iChat is an excellent way to keep in touch. Since a lot of useful information flows through my iChat sessions, I long ago enabled logging (select iChat: Preferences, click on the Messages tab, and select the Automatically Save Transcripts option). Every so often, I’ll go in and clean these out, but they do tend to pile up quickly—I currently have 5,428 iChat transcripts, dating back to February 2004.

Prior to the release of OS X 10.4, I used Spiny Software’s free Logorrhea to search these chats for specific tidbits. Under OS X 10.4, I now use a mix of Logorrhea and Spotlight (Logorrhea is a bit slower, but it’s easier to control exactly what you’re looking for). That’s all well and good, but with so many archived chats I still need an organization scheme. By (non-changeable) default, all iChat logs are stored in your user’s Documents/iChats folder. There are no subfolders in here, just one big mess of files. You either need to use list view to see the date of each chat, or try to read the special iChat filenaming convention. When it saves a log, iChat names the file with the buddy’s name and the date and time of the chat, like so:

Joe Chatter on 2005-11-11 at 10.45

Regardless of whether I use list view or the filename, it’s a real pain when I want to find, for instance, a chat that I know I had with Richard on the 15th of last month. I must open the iChats folder and scroll down through the long list of entries until I find Richard’s section, then look for the one from the 15th. Ugh. So for today’s Geeky Friday, I thought I’d show you how to organize your iChat logs into meaningful subfolders, using a bit of code written in the Python programming language. Python is included with OS X—version 2.3.5 is installed on OS X 10.4—so you don’t need to download anything special to use it for today’s hint.

Get Organized

To organize the folder, we’re going to use the following Python script. Before we proceed, keep in mind that I’m not a programmer—so not only did I not write this code, I also can’t explain everything it’s doing. It was written by macosxhints reader Jay Soffian, with a modification from a anonymous contributor, dashv, to account for some changes in the iChat logfile naming scheme.

Since this script moves files around within the iChats folder, it’s a very good idea to back the folder up first, in case you either change your mind about using the organization scheme, or the script doesn’t work correctly. Once you’ve done that, open up TextEdit (if you’re comfortable in Terminal, feel free to use it and your favorite editor), and create a new pure text document. If you’re not sure if the document is rich text or plain text, just look at the Format menu. If you see a Make Plain Text menu item, select it. If you see a Make Rich Text entry, then your document is already plain text.

Now, copy and paste the following code into the new document:

	import sys
	import os, os.path
	import re
	ichats_dir = os.path.join(os.getenv("HOME"), "Documents/iChats")
	patterns = map(re.compile,
	    r" on d{4}-d{2}-d{2} at d{2}.d{2}.i{,1}chat$",
	    r" on d{4}-d{2}-d{2} at d{2}.d{2} #d+.i{,1}chat$",
	for chat in os.listdir(ichats_dir):
	  aim = None
	  for pattern in patterns:
	      aim = pattern.sub("", chat)
	  if aim is None: continue
	  aim_dir = os.path.join(ichats_dir, aim)
	  if not os.path.isdir(aim_dir): os.mkdir(aim_dir)
	  src = os.path.join(ichats_dir, chat)
	  dst = os.path.join(ichats_dir, aim_dir, chat)
	  print "%s -> %s/%s" % (chat, aim_dir, chat)
	  os.rename(src, dst)

Basically, this script reads through all the files in the folder, checking their filenames. Each time it finds a new filename, it trims a bit of that entry’s name, and uses it for the name of a new folder. It then moves all logs for that same buddy into the new folder. It repeats this process until it’s looked at all the files in the folder.

Once you’re certain you’ve got the script correct, save it somewhere on your hard drive. If you’ve made a folder for scripts, that’s a perfect spot for this one, or just drop it somewhere in your Documents folder. For a name, it’s best if you include a file extension—otherwise, TextEdit will append .txt , which you’ll probably just want to remove later. I chose to use .py for the extension, to indicate that this is a Python script. When you try to add your own extension when saving, TextEdit will warn you about it:

Click the “Use .py” button to tell TextEdit you know what you’re doing. You’ll also want to keep the name short, and probably lowercase only, as you’re going to have to type it to execute the code. So it might as well be simple to type.

After you save the script, it’s time to fire up the Terminal (in /Applications/Utilities). The following assumes you’ve saved the file to your Documents folder, and called it ; if that’s not true for you, just modify the following as necessary. Type these commands, then pressing Return after each (don’t type the $, it’s just the prompt):

	$ cd ~/Documents
	$ chmod a+x

First you moved into the directory containing the script. Then you told OS X that this is a program that can be run. Now, to use the script (you have made your backup, right?), just type:

$ ./

When you press Return, the script will start executing, and you’ll see a line of output for each file it processes. Depending on how many transcripts you have, it may take a few minutes for the script to finish. But when it does, you’ll find that you’ve now got a nicely organized iChats folder. Below is a before and after picture of my folder. The one on the left shows all the individual files loose in the folder; on the right is the post-script structure. (I had to distort a few of the lines, as they showed either screen names or email addresses.)

With everything in folders, it’s much easier to find a specific iChat with a specific person; just find his or her folder, open it, and sort by date. The good news is that this organization scheme doesn’t bother either Logorrhea or Spotlight; both will continue to search within archived iChat transcripts without any problems at all.

Keep Things Organized

Remember that as you continue to use iChat, more archives will be added to the top level of the folder. So every so often, repeat the Terminal instructions above to re-sort your iChats folder. I’m working on a way of automating this, using Folder Action Scripts and Automator. I’m also working on a way to make iChat transcripts more “Spotlight friendly.” Assuming I figure it all out, look for it in a future Geeky Friday installment.

Shop Tech Products at Amazon