Quantcast

Updating Twitter via iChat or AIM status message

Update: Here is a Cocoa version that is easier to use!

May did a good job selling us on Twitter, so now you can see my twittr status in the sidebar.. I wished that changing my Twitter status was as easy as changing my iChat status, so I hacked up this bit of perl that will update it when your iChat status changes. It runs every five minutes via launchd, and uses the newly-announced Twitter API.

PERL:
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use URI::Escape;
  5.  
  6. my $user = ‘user@example.com’;
  7. my $pass = ‘pin’;
  8.  
  9. my $isAvailable = `osascript -e ‘tell application "iChat" to status’`;
  10. chomp $isAvailable;
  11.  
  12. exit if (‘available’ ne $isAvailable);
  13.  
  14. my $status = `osascript -e ‘tell application "iChat" to status message’`;
  15. chomp $status;
  16.  
  17. if ( eq $status) {
  18.     $status = ‘lazy’;
  19. }
  20.  
  21. my $savedStatusFile = ’savedStatus.txt’;
  22.  
  23. my $savedStatus = ;
  24. if (-e $savedStatusFile) { 
  25.     $savedStatus = `cat $savedStatusFile`;
  26. }
  27.  
  28. if ($status ne $savedStatus) {
  29.     #status changed, update twitter
  30.  
  31.     my $encStatus = ’status=’ . uri_escape($status, “^A-Za-z0-9″);
  32.    
  33.     `curl -s -d ‘$encStatus’ -u $user:$pass http://twitter.com/statuses/update.xml`;
  34.  
  35.     open (FILE, “>$savedStatusFile”) or die “can’t open $savedStatusFile for writing”;
  36.     print FILE $status;
  37.     close FILE;  
  38. }

You can get launchd to run the script every five minutes by creating a file called ~/Library/LaunchAgents/net.tikirobot.status.plist that looks like this:

XML:
  1. <?xml version=“1.0″ encoding=“UTF-8″?>
  2. <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.
  3. com/DTDs/PropertyList-1.0.dtd">
  4. <plist version=“1.0″>
  5. <dict>
  6.     <key>Label</key>
  7.     <string>net.tikirobot.status</string>
  8.     <key>ProgramArguments</key>
  9.     <array>
  10.             <string>/Users/user/status.pl</string>
  11.     </array>
  12.     <key>StartInterval</key>
  13.     <integer>300</integer>
  14. </dict>
  15. </plist>

And then load the job using:
launchctl load ~/Library/LaunchAgents/net.tikirobot.status.plist

Update: The script now uses URI::URL and POST. Fixed a typo.
Update2: Now uses URI::Escape

15 Responses to “Updating Twitter via iChat or AIM status message”

  1. September 24th, 2006 | 1:17 am

    Why oh why does our theme make source code look like shit?

  2. September 24th, 2006 | 2:03 pm

    I dunno ... I changed it to use the "pre" tag and it was even worse. We need to go find someones source code CSS and lift it .....

  3. September 24th, 2006 | 4:48 pm

    How can WordPress/our stupid theme fuck up even the pre tag?

    And you know what else? When you try to edit the post, WordPress translates all ampersand-l-t-semicolons back into angle brackets, and you have to manually change them back.

    To make the problem even worse, Apple's plist format is stupidly verbose. I'm only setting two parameters. The xml should be as simple as this:

    <cron>
    <path>/full/path/blah.pl</path>
    <interval>300</interval>
    </cron>

  4. September 24th, 2006 | 10:52 pm

    OK, the ig:syntax hiliter is now installed, and things look much better. I found it on this list of wordpress syntax highlighters.

  5. October 10th, 2006 | 6:55 pm

    just testing this thing out :)

  6. October 10th, 2006 | 7:00 pm

    TESTING :)

  7. October 11th, 2006 | 12:57 am

    This looks awesome and I want to give it a try, but permissions are flaked. I made status.pl 777. but I still get this in the console:

    Oct 11 02:43:14 adamnan launchd[2485]: net.tikirobot.status: execvp("/Users/Dydimustk/status.pl", ...): Permission denied

    Oct 11 02:43:14 adamnan launchd[2482]: net.tikirobot.status: execve(): Permission denied

  8. October 11th, 2006 | 8:41 am

    dydimustk, does status.pl work for you if you run it from the command line? Stupid launchd...

  9. November 12th, 2006 | 12:38 pm

    Sorry that I didn't get back to you till now. I did get it to work with a little messing around in launchd. But I wanted to mention, that I want to try and get this to work in reverse now as well. So that iChat will grab its status from twitter. If I get it working, I'll post it here.

  10. Seth
    January 18th, 2007 | 12:15 pm

    I want this to work the other way around... when I change my Twitter info, I want *that* to become my iChat status message.

    Can that be done?

  11. January 18th, 2007 | 1:38 pm

    Sure.. you will have to change the script a bit, but it's not too hard.

    If you don't want to mess with it yourself, I might be able to do it in a few days when I get some time...

  12. January 20th, 2007 | 3:12 pm

    Thanks for posting something like this. Unfortunately, I can't get it to work. I'm getting something like this in the Terminal:

    chrischris:~ chris$ launchctl load ~/Library/LaunchAgents/net.twitter.status.plist
    launchctl: propertyList is NULL
    launchctl: no plist was returned for: /Users/chris/Library/LaunchAgents/net.twitter.status.plist
    launchctl: no plist was returned for: /Users/chris/Library/LaunchAgents/net.twitter.status.plist
    nothing found to load

    Don't know what I'm doing wrong. Please help!

  13. January 29th, 2007 | 1:35 am

    OK, I made a Cocoa version that is much easier to use. You can get it here

  14. May 1st, 2007 | 12:10 am

    [...] while back I posted a perl script that would update your Twitter status whenever you changed your iChat status message. [...]

  15. December 3rd, 2007 | 7:35 am

    [...] Updating Twitter via iChat or AIM status message [...]

Leave a reply