use strict; use vars qw($VERSION %IRSSI); use Mac::AppleScript qw(RunAppleScript); use Irssi; $VERSION = '0.30'; %IRSSI = ( authors => 'John Worthington', contact => 'johnwort@synergistic.cc', name => 'iTunes2irssi', description => 'This script allows basic control over iTunes from irssi', license => 'GPL', ); my $start = "iTunes is blasting"; my $middle = "by"; my $end = ""; my $songTag = "\$s"; my $artistTag = "\$a"; sub cmd_iTunes { my ($data, $server, $activeChannel) = @_; if(make_sure_iTunes_is_running()){ my @commands = split / /, $data, 2; if($commands[0] eq "play" || $commands[0] eq "pause"){ playpause(); } elsif($commands[0] eq "next"){ next_track(); } elsif($commands[0] eq "previous"){ previous_track(); } elsif($commands[0] eq "set"){ my @newOutput = $commands[1]; my $set = @newOutput[0]; set_output($set); } elsif(length($data) > 0){ Irssi::print("Error: I do not know that command(" . $data . ") dave..."); } else { current_song($activeChannel); } } else { launch_iTunes(); } } sub set_output { # Delete whitespaces : http://www.perlfaq.com/cgi-bin/view?view_by_id=122 my $string = shift; my $test = index($string,$songTag); if($test != -1){ my @middle; my @front = split /\$s/, $string; # I should have two halfs now, the first [0] will have the value for $start. the other part will have everything else. @middle = split /\$a/, @front[1]; # @middle[0] has $middle in it, @middle[1] has the last tag and $end. $test = index($string, $artistTag); if($test != -1){ $start = @front[0]; # everything read until that point -> $start $middle = @middle[0]; # repeat until I hit $a, string -> $middle $end = @middle[1]; # if there is anything left, -> $end $middle = " " . $middle . " "; }else{ Irssi::print("Format: /itunes set \$s \$a "); Irssi::print("Short Format: /itunes set \$s "); $start = @front[0]; $middle = @front[1]; $end = ""; } }else{ Irssi::print("Error: Format error."); Irssi::print("Format: /itunes set \$s \$a "); Irssi::print("Short Format: /itunes set \$s "); } } sub current_song { my $activeChannel = shift; my $output; if(playerState() eq "playing"){ if(mp3_or_url()){ # mp3 is playing my $title = song_title(); my $artist = song_artist(); # $artist or $title is missing.... if($artist eq "\"\"" || $title eq "\"\""){ if($artist eq "\"\"" && $title eq "\"\""){ $output = $start . " something ". $middle . " somebody " . $end; }elsif($artist eq "\"\""){ $output = $start . " " . $title. " " . $middle . " somebody " . $end; }else{ $output = $start . " something " . $middle. " " . $artist . " ". $end; } }else{ $output = $start ." ". $title ." ". $middle." " . $artist." " . $end; } } else { # url is playing Irssi::print("Error: I have not implemented url displaying yet"); } $activeChannel->command("/SAY $output") } else { Irssi::print("Error: iTunes is currently not playing anything..."); } } sub playerState { my $script = "tell application \"iTunes\"\n return player state\nend tell"; return RunAppleScript(qq($script)) } sub playpause { my $script = "tell application \"iTunes\"\n playpause\nend tell"; RunAppleScript(qq($script)) } sub next_track { my $script = "tell application \"iTunes\"\n next track\nend tell"; RunAppleScript(qq($script)) } sub previous_track { my $script = "tell application \"iTunes\"\n previous track\nend tell"; RunAppleScript(qq($script)) } sub make_sure_iTunes_is_running { my $script = "tell application \"Finder\"\nif (get name of every process) contains \"iTunes\" then\nreturn 1\nelse\nreturn 0\nend if\nend tell"; if(RunAppleScript(qq($script))){ return 1; # true }else{ return 0; # false } } sub mp3_or_url { # Might change to return the string "my_current_track" my $script = "tell application \"iTunes\"\n set my_current_track to (a reference to current track)\nif (get class of my_current_track) is URL track then\nreturn \"url\"\n else\n return \"mp3\"\nend if\nend tell"; my $result = RunAppleScript(qq($script)); if($result == "mp3"){ return 1; # true } else { return 0; # false } } sub song_title { my $script = "tell application \"iTunes\"\n set my_current_track to (a reference to current track)\nreturn (get name of my_current_track) as string\nend tell"; my $song = RunAppleScript(qq($script)); $song =~ tr/\"//d; return $song; } sub song_artist { my $script = "tell application \"iTunes\"\n set my_current_track to (a reference to current track)\nreturn (get artist of my_current_track) as string\nend tell"; my $artist = RunAppleScript(qq($script)); $artist =~ tr/\"//d; return $artist; } sub launch_iTunes { my $script = "tell application \"iTunes\"\n run \nend tell"; return RunAppleScript(qq($script)) } Irssi::command_bind('itunes', 'cmd_iTunes');