Stupid Mac::Glue Tricks

pudge on 2003-11-17T21:07:07

I use this with XChat Aqua. Note that I create the objects outside the handler: XChat smartly caches the script, so the objects are created once. That, combined with only loading the modules once, significantly decreases the execution time, to nearly nil.

Also, I use IsRunning() to see if I am running iTunes locally; if so, it uses that, and if not, it uses the other box running in the house.

I had to install Mac::Glue etc. for the system perl, which I rarely use, but the plugin library is only compatible with that one. But I made the directory containing the glues a symlink to my local perl's (`sudo ln -s /usr/local/lib/perl5/site_perl/5.8.0/Mac/Glue/glues /Library/Perl/5.8.1/Mac/Glues/` or whatever). Those are just Storable + MLDBM, I don't need separate copies of them.

#!/usr/bin/perl

use strict; use Mac::Apps::Launch; use Mac::Glue;

my $itunes = new Mac::Glue 'iTunes';

my $track = $itunes->prop('current track');

my $name = $track->prop('name'); my $artist = $track->prop('artist'); my $album = $track->prop('album');

sub np { if (IsRunning($itunes->{ID})) { $itunes->ADDRESS; } else { $itunes->ADDRESS(eppc => iTunes => 'sweeney.local'); }

my @data = map { $_->get } ($name, $artist, $album); my $str = $data[0]; return 0 unless $str; $str = "$str - $data[1]" if $data[1]; $str = "$str ($data[2])" if $data[2]; IRC::command("NP: $str"); return 1; }

IRC::register("np", "1.0", "", ""); IRC::add_command_handler("np", "np");