PDA

View Full Version : Control SlimServer _from_ iTunes (Windows)



Craig, James (IT)
2004-10-05, 02:17
For what it's worth, I've translated Anton's script into Windows
compatible Perl.

There are a couple of problems in that OLE->GetOpenObject doesn't seem
compatible with iTunes and hence iTunes will be started if not already
running. Secondly the script loops every second which is clumsy - you
may not hear a song through to the end on the SlimServer.
(I did try making it event driven but no luck there - the Perl OLE event
support is 'alpha' so I'm not entirely surprised.)
And thirdly I just hard-coded the most common URL character translations
- to be done correctly it should use the same module as SlimServer does.
Seems to work OK for me though.

The best use I can think of is using the iTunes 'party shuffle' to drive
slimServer but if I was doing this I'd rather 'insert' the playlist
items, because then they'd be played to completion and you'd keep a
history of the playlist beyond that which is in iTunes?
Any thoughts let me know!

James

PS. Don't run this at the same time as the iTunesUpdate plugin or you'll
get double play counts on all your songs!


#-- INSTRUCTIONS:
#-- Set your variables server_address and server_port to your situation
my $server_address = '127.0.0.1'; # This is the slimserver's
address
my $server_port = '9000'; # This is the
slimserver's port
my $player = ''; # This
identifies the player

use Win32::OLE qw(EVENTS);
use LWP::Simple;

Win32::OLE->Option(Warn => \&OLEError);

my $iTunesHandle;
my $current_file_track = '';

sub OLEError {
print (Win32::OLE->LastError() . "\n");
}

# A wrapper to allow us to uniformly turn on & off debug messages
sub iTunesUpdateMsg
{
# Parameter - Message to be displayed
my $message = join '',@_;

print STDOUT $message;
}

sub _openiTunesWin {
my $failure;

unless ($iTunesHandle) {
iTunesUpdateMsg ("Attempting to make connection to
iTunes...\n");
$iTunesHandle =
Win32::OLE->GetActiveObject('iTunes.Application');
unless ($iTunesHandle) {
$iTunesHandle = new Win32::OLE(
"iTunes.Application")
or $failure = 1;
if ($failure) {
iTunesUpdateMsg( "Failed to launch
iTunes through OLE!!!\n");
return 0;
}
my $iTunesVersion = $iTunesHandle->Version;
iTunesUpdateMsg ("OLE connection established to iTunes:
$iTunesVersion\n");
}
} else {
#iTunesUpdateMsg ("iTunes already open: testing
connection\n");
$iTunesHandle->Version or $failure = 1;
if ($failure) {
iTunesUpdateMsg ("iTunes dead: reopening...\n");
undef $iTunesHandle;
return _openiTunesWin();
}
}

return 1;
}




while (1 == 1) {
if (_openiTunesWin()) {
# -- iTunes is running. Check if it is
playing a song.
if ($iTunesHandle->PlayerState == 0) {
unless ($stopped) {
iTunesUpdateMsg("iTunes is stopped\n");
# if player state is paused or player
state is stopped then iTunes is not playing. Stop slimserver.
$result = get(
"http://$server_address:$server_port/status.txt?p0=stop&player=$player")
;
#iTunesUpdateMsg($result);
}
$stopped = 1;
} else {
$currTrack = $iTunesHandle->CurrentTrack();
if ($currTrack->Kind == 1) {
$currTrackLoc = $currTrack->Location();
$currTrackLoc =~ s/ /%2520/g;
$currTrackLoc =~ s/[\/\\]/%2F/g;
$currTrackLoc =~ s/:/%3A/g;
if ($currTrackLoc ne
$current_file_track) {
iTunesUpdateMsg("Currently
playing $currTrackLoc\n");
# -- File in iTunes is
different than file on SlimServer. Thus update variable
$current_file_track =
$currTrackLoc;
# -- Tell slimserver to
stop current song and play the song currently playing in iTunes
#$result =
get("http://$server_address:$server_port/status.txt?p0=stop&player=$play
er");
#iTunesUpdateMsg($result);
$result =
get("http://$server_address:$server_port/status.txt?p0=playlist&p1=play&
p2=file%3A%2F%2F%2F$currTrackLoc&player=$player");
#iTunesUpdateMsg($result);
}
}
}
}
sleep 1;
}
--------------------------------------------------------

NOTICE: If received in error, please destroy and notify sender. Sender does not waive confidentiality or privilege, and use is prohibited.

Anton F. van der Kraaij
2004-10-05, 02:52
Hi James,

> For what it's worth, I've translated Anton's script into Windows
> compatible Perl.

looks complicated to me! :-) I dont have windows at home, so cant really test
it. Is this perl support a windows only feature?

> Secondly the script loops every second which is clumsy - you
> may not hear a song through to the end on the SlimServer.

That is also a problem with the applescript I had.

> (I did try making it event driven but no luck there - the Perl OLE event
> support is 'alpha' so I'm not entirely surprised.)

An event driven script would solve the problem indeed. You could then,
depending on what event, append songs to a list instead of just playing it. I
have _no_ idea how to do this. Either in applescript or an iTunes plugin?
Does anybody know how to do this, i.e. catch apple events? Either in
applescript of perl or anything?

> The best use I can think of is using the iTunes 'party shuffle' to drive
> slimServer but if I was doing this I'd rather 'insert' the playlist
> items, because then they'd be played to completion and you'd keep a
> history of the playlist beyond that which is in iTunes?
> Any thoughts let me know!

I just enjoy controlling my slimserver from iTunes. Either random play or
album play or anything. :-)

Greetings,

Anton.