All pastes #94694 Raw Edit

Cheez

public text v1 · immutable
#94694 ·published 2006-07-21 13:26 UTC
rendered paste body
use strict;
use vars qw($VERSION %IRSSI);
use Irssi;

$VERSION = "1.1.0";
%IRSSI = (
		  name        => "itunes",
		  licence     => "Public Domain",
		  description => "Prints currently playing itunes song in active channel",
		  changed     => "Mon Feb 28 00:30:00 EET 2006",
		  changes     => "
							1.1.0 - added error checking and support for mp3s without id3 tags
							1.2.0 - moved all text processing within applescript, added time stamps
						 "
	);

sub cmd_mp3 {
	my $witem = Irssi::active_win()->{active};
	if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
		my $applescript = "tell application \\\"System Events\\\"
	if (get name of every process) contains \\\"iTunes\\\" then
		tell application \\\"iTunes\\\"
			try
				set strout to \\\"\\\"
				if artist of current track is not \\\"\\\" then
					set strout to artist of current track & \\\" - \\\"
				end if
				set strout to strout & name of current track & \\\" \\\"
				if album of current track is not \\\"\\\" then
					set strout to strout & \\\"[Album: \\\" & album of current track & \\\"] (\\\"
				else
					set strout to strout & \\\"(\\\"
				end if
				if player state is not stopped then
					set strout to strout & (round (player position / 60) rounding down) & \\\":\\\" & player position mod 60 & \\\" / \\\"
				end if
				set strout to strout & (round ((duration of current track) / 60) rounding down) & \\\":\\\" & (duration of current track) mod 60 & \\\")\\\"
				get strout
			on error
				get \\\"Error communicating with iTunes\\\"
			end try
		end tell
	else
		get \\\"No song is currently playing.\\\"
	end if
end tell";
		my $string = `osascript -e "$applescript"`;
		chomp($string);
		$witem->command("ACTION ".$witem->{name}." bops to " . $string);
	} else {
		Irssi::print('mp3: You must do this in a channel or query window.');
	}
}

Irssi::command_bind('mp3', 'cmd_mp3');