O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Mac OS X Panther Hacks
By Rael Dornfest, James Duncan Davidson
June 2004
More Info

HACK
#32
Better iChat Transcripts
Browse and search your iChat conversations and even export them as plain text or, with a little AppleScript magic, web pages
The Code
[Discuss (1) | Link to this hack]

The Code

With a little bit of GUI AppleScripting , combined with a touch of Perl and a smattering of Unix grep goodness, you can have Logorrhea generate its logs and then turn them into nicely formatted HTML pages, one per buddy:

-- Transcripts Away
-- Purpose: Uses GUI scripting to script Logorrhea to export iChat
-- logs out to HTML files.
-- Author: Phil Ulrich

set peopleNames to {}

(* Part 1: Get the names of people we've iChatted with *)
tell application "Logorrhea" to activate
tell application "System Events"
  tell process "Logorrhea"
    get static text in radio group 1 of scroll area 1 of scroll area 1 of browser 1 of 
tab group 1 of splitter group 1 of window 
    repeat with i in the result
     copy the value of i to the end of peopleNames
    end repeat
  end tell
end tell

(* Part 2: Find out which names we need *)
tell application "Logorrhea"
  set chosen_ones to choose from list peopleNames with prompt "Whose chats ¬ would you
 like to export?" OK button name "Export" cancel button name ¬
"Cancel" with multiple selections allowed without empty selection allowed
end tell

(* Part 3: Export, export, export! *)
tell application "System Events"
  tell process "Logorrhea"
    tell menu bar 1
      tell menu bar item "File"
        tell menu "File"
          click menu item "Export Chats..."
        end tell
      end tell
    end tell

    keystroke "D" using command down
    set value of text field 1 of sheet 1 of window 1 to "ChatTranscripts"
    click button "Save" of sheet 1 of window 1

    try
      delay 2
      click button "Replace" of window 1 -- account for possibility that the file exists already
    end try
  end tell
end tell

(* Part 4: Done with Logorrhea. Begin parsing. *)
set infile to "~/Desktop/ChatTranscripts.txt"
do shell script "perl -pi -e 's/\\x0d/\\x0a/g' " & infile
repeat with a_name in chosen_ones
  set output_string to "<html><head><title>" & a_name & "</title></head>¬ <body><table border=\"1\">"
  set outfile to quoted form of ((POSIX path of (path to desktop)) & ¬
a_name & ".html")

  try
    set result_lines to do shell script "grep ^\"" & a_name & "\" " & ¬ infile without altering line endings
    set old_delims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "
"
    set alternating_lines to text items of result_lines

    repeat with j from 1 to number of items in alternating_lines
      set top_line to item j of alternating_lines
      set AppleScript's text item delimiters to " "
      set top_line_parts to text items of top_line
      set output_string to output_string & "<tr>"
      set item_count to the number of items in top_line_parts
 
      repeat with i from 2 to the item_count
        set part to item i of top_line_parts
        if i is item_count then
          if part is "" then
            set output_string to output_string & "</tr></table><p /><hr />¬
<p /><table border=\"1\"><tr>"
          else
            set output_string to output_string & "</tr><tr><td colspan=\"5¬
\">" & part & "</td></tr>"
          end if
        else
          set output_string to output_string & "<td>" & part & "</td>"
        end if
      end repeat

    end repeat
 
    set AppleScript's text item delimiters to old_delims
    set output_string to output_string & "</table></body></html>"
    do shell script "echo \"" & output_string & "\" | cat - > " & outfile
    do shell script "rm " & infile
  end try

end repeat

Here's how it works. AppleScript activates Logorrhea, pulls out the name of everyone for whom an iChat log exists (using GUI scripting), and pushes the name on to an array. The script then pops up a dialog that asks you which buddies' chats you want to export; it creates a separate HTML page for each buddy.

Again using GUI Scripting, the script calls Logorrhea's File→Export Chats... menu item to create that single whopping great text file of all available chats. If there is an existing file on the Desktop called ChatTranscripts.txt (the default filename), you'll notice a Replace or Cancel dialog pop up. Just wait; the script will "click" Replace for you automatically. (Alternately, you can click it yourself if you're just that impatient.)

AppleScript then calls out to the Unix shell (do shell script) for a quick spot of Perl and grep to convert Mac line endings to Unix and scan for lines that match the buddies you chose to build transcripts for, respectively.

WARNING

If any of your buddies' aliases contain characters that grep considers special, such as [] or (), the script might fail silently. It would be helpful if there were an option in AppleScript for escaped form, to go along with quoted form.

Finally, the script parses the matching lines by using AppleScript's text-parsing abilities and pipes them out to a file via Unix's echoand cat.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.