Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

pudge (1)

pudge
  (email not shown publicly)
http://pudge.net/
AOL IM: Crimethnk (Add Buddy, Send Message)

I run this joint, see?

Journal of pudge (1)

Wednesday April 02, 2003
05:24 PM

Journal Reader for Mac OS X

[ #11410 ]

This script -- using as-yet unreleased versions of Mac::Glue and Mac::Carbon -- works under perl on Mac OS X (as well as MacPerl under Mac OS) to loop through my use.perl.org mailbox for new journal messages, groups them by user, opens them in the browser one user at a time, and then moves the messages to the trash.

Note, to those of you new to Mac:: modules, that GetURL() automatically opens the URL in whichever browser you have chosen as your default.

The Mac::Glue stuff is pretty straightforward.

#!/usr/bin/perl
use strict;
use warnings;
 
use Mac::Glue ':all';
use Mac::InternetConfig 'GetURL';
 
my $eudora  = new Mac::Glue 'Eudora';
my $box     = $eudora->obj(mailbox => ' use perl');
my $count   = $eudora->count($box, 'each' => 'messages');
my $trash   = location(end => $eudora->obj(mailbox => "Trash"));
 
my %users;
# loop over messages in mailbox
for my $i (1 .. $count) {
    my $msg = $eudora->obj(message => $i, $box);
 
    # check status
    my $status  = $eudora->get( $eudora->prop(status  => $msg) );
    next unless $status eq 'unread';
 
    # check subject
    my $subject = $eudora->get( $eudora->prop(subject => $msg) );
    next unless $subject =~ /^\[use Perl\] New Journal Entry by (.+?), "/;
    my $user = $1;
 
    # many Mac apps still use \015 as newline internally, as does AppleScript
    (my $body = $eudora->get( $eudora->prop(body => $msg) )) =~ s/\015/\n/g;
 
    # get URL for journal entry out of the body
    my($url) = $body =~ m|^    (http://use.perl.org/~.+?/journal/\d+)$|m;
 
    # save id and url for later
    my $id = $eudora->get( $eudora->prop(id => $msg) );
    push @{$users{$user}}, [ $id, $url ];
}
 
for my $user (sort { lc $a cmp lc $b } keys %users) {
    # print journal entries to STDOUT
    my @urls = sort map { $_->[1] } @{$users{$user}};
    printf "%s:\n    %s\n", $user, join "\n    ", @urls;
 
    # open each journal entry in the browser
    for my $url (reverse @urls) {
        GetURL($url);
    }
 
    # move messages to the trash
    for my $id (map { $_->[0] } @{$users{$user}}) {
        my $nid = $eudora->obj(message => obj_form(formUniqueID, typeInteger, $id), $box);
        $eudora->move($nid, to => $trash);
    }
 
    <>;  # wait for NL
}
 
__END__

Also worthy of note for those of you into Mac::Glue is that the normal form of object is CLASS => VALUE, such as message => $n to specify the nth message; but when we move the message to the trash, we no longer can go by the index, since it changes; so instead we use the message's unique ID, which is what the formUniqueID is all about (currently, Mac::Glue can't easily distinguish between an index and a unique ID that is an integer, so we have to help it out).

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.