#!/usr/bin/perl # pudge@pobox.com # 2003.11.19 - 2004.01.22 # X-Chat Aqua script for controlling iTunes package IRC::pudge::np; use strict; use Mac::Apps::Launch; use Mac::Glue ':all'; our $AUTOLOAD; my $itunes = new Mac::Glue 'iTunes'; my $version = $itunes->prop('version'); my $state = $itunes->prop('player state'); my $track = $itunes->prop('current track'); my $playlist = $itunes->prop(view => window => 1); my $rating = $track->prop('rating'); my $name = $track->prop('name'); my $artist = $track->prop('artist'); my $album = $track->prop('album'); # if no local iTunes, try these remote machines my @hosts = qw(sweeney russell); my %hosts = map { $_ => 0 } @hosts; sub _gethost { my $found = 0; if (IsRunning($itunes->{ID})) { $itunes->ADDRESS; $found = 1 if &_alive; } unless ($found) { for my $host (@hosts) { # only check "bad" hosts every minute or so next unless $hosts{$host} + 60 < time(); $itunes->ADDRESS(eppc => iTunes => $host); $found = 1, last if &_alive; $hosts{$host} = time(); } } return $found; } sub _alive { my($ck_state) = @_; return $ck_state ? $state->get eq 'playing' : $version->get; } sub rate { return 1 unless _gethost(1); my($val) = @_; $rating->set(to => $val); np('me'); } sub np { return 1 unless _gethost(1); my @data = map { $_->get } ($name, $artist, $album, $rating); my $str = $data[0]; return 1 unless $str; $str = "$str - $data[1]" if $data[1]; $str = "$str ($data[2])" if $data[2]; $str = "$str : $data[3]" if $data[3]; $str = "NP: $str"; if ($_[0] eq 'me') { IRC::print($str); } else { IRC::command($str); } return 1; } sub _info { my($type, $info) = @_; my $found = $playlist->obj( track => gAny, tracks => whose($type => contains => $info), ); if ($found->get) { $found->play; np('me'); } else { IRC::print("Error: $type '$info' not found"); } return 1; } sub AUTOLOAD { (my $name = $AUTOLOAD) =~ s/^.*::(\w+)$/$1/; return unless $name =~ /^(?:play|pause|next_track|previous_track|artist|album|name|rating)$/i; return 1 unless _gethost(); if ($name =~ /^(?:artist|album|name|rating)$/i) { _info($name, @_); } elsif ($name =~ /^(?:play|pause|next_track|previous_track)$/i) { $itunes->$name(REPLY => 0); if ($^E+0) { IRC::print("Error in $name: $MacError"); } else { np('me'); } } return 1; } IRC::print("Loading pudge's iTunes scripts ..."); IRC::register("np", "1.0", "", ""); IRC::add_command_handler("np", "IRC::pudge::np::np"); IRC::add_command_handler("play", "IRC::pudge::np::play"); IRC::add_command_handler("pause", "IRC::pudge::np::pause"); IRC::add_command_handler("next", "IRC::pudge::np::next_track"); IRC::add_command_handler("previous", "IRC::pudge::np::previous_track"); IRC::add_command_handler("prev", "IRC::pudge::np::previous_track"); IRC::add_command_handler("artist", "IRC::pudge::np::artist"); IRC::add_command_handler("album", "IRC::pudge::np::album"); IRC::add_command_handler("name", "IRC::pudge::np::name"); IRC::add_command_handler("rating", "IRC::pudge::np::rating"); IRC::add_command_handler("rate", "IRC::pudge::np::rate");