June 15, 2008

How-To: Make a Simple Amarok Now Playing Script for XChat in Perl

XChat offers great support for scripts in Perl and Python, and even C plugins, making adding features or additional functionality very easy, with minimum programming knowledge.

In this tutorial I will show how to make a very basic, functional Amarok now playing announcer for it.

Open your text editor of choice and put the following code in it:

Xchat::register ("Amarok NP", "0.1.0", "Amarok NP for XChat");
Xchat::hook_command ("ANP", cmd_anp);

Xchat::print ("Amarok NP loaded. Type /ANP to use.");

sub cmd_anp
{
$isPlaying = `dcop amarok default isPlaying`;
chomp ($isPlaying);
if ($isPlaying eq "true")
{
$nowPlaying = `dcop amarok default nowPlaying`;
chomp ($nowPlaying);
Xchat::command ("ME listening to: $nowPlaying");
return Xchat::EAT_ALL;
}
if ($isPlaying eq "false")
{
Xchat::print ("Amarok is either stopped or paused");
return Xchat::EAT_ALL;
}
if ($isPlaying eq "")
{
Xchat::print ("Amarok is not running");
return Xchat::EAT_ALL;
}
return Xchat::EAT_ALL;
}
Next, save the file, say as amarok_np.pl into your ~/.xchat2/ directory, where ~ is your home directory. This way, it will be loaded by default when XChat starts. If you type /ANP in a channel or query while Amarok is playing a song, you'll see a message similar to:

[18:46] * Embryo Listening to: ABBA - Happy New Year

How does it work?
The first line provides XChat information about the script itself, like the name, version number and a brief description. This information will appear in the Window -> Plugins and Scripts... dialogue.

The next line tells XChat that a new command was created, and the code below should be executed when this command is ran.

Xchat::print ("Amarok NP loaded. Type /ANP to use.");


This is what will appear when the script is loaded. You can enter anything here.

sub cmd_anp contains the actual code. Basically, it verifies if Amarok is running, and then it uses the DCOP interface in order to fetch the currently playing song, which will be sent to the current channel/query using the Xchat::command line.

Here you can find several now playing scripts and plugins for XChat.

More resources
XChat Perl Interface - Very useful documentation on the official website on how to use Perl for scripting XChat

Updated: Jun 15, 2008 (Created: Jun 15, 2008)

1 comment:

Anonymous said...

great thanks!