Link to home
Start Free TrialLog in
Avatar of aa-denver
aa-denverFlag for United States of America

asked on

Need help with accessing UpNext in iTunes though the win32::ole("iTunes.Application")

I am writing a program in Perl to access the currently playing track in iTunes for Windows and to get the next 3 songs that iTunes will play.   I have the code to get the currently playing song.  But I cannot find a way to get the next 3 songs.

I would be open to alternate languages such as Ruby if you know of a way to do this.

Here is my simple Perl code to get the current playing track.  I need help getting the next 3 songs
#!C:\Perl64\bin\perl.exe

use strict;
 use Win32::OLE;

my $iTunesApp = new Win32::OLE("iTunes.Application");
 my $track = $iTunesApp->CurrentTrack;

 print "Current Track: " . $track->Name . "\nCurrent Artist: " . $track->Artist . "\n"
Avatar of Darrell Porter
Darrell Porter
Flag of United States of America image

The COM object has a number of properties, enumerated here with examples.

I would specifically look at CurrentPlaylist to compare the CurrentTrack to determine what the next 3 tracks are, if there are three tracks left to play.

I found this by Googling for

itunes windows com object properties

I hope this helps.
Avatar of aa-denver

ASKER

Thanks for the suggestion.   I have found and downloaded the Apple SDK for Windows OLE.  At this point I need help understanding how to get the objects.  I was able to get the name of the currently playing track (as before) and the name of the currently targeted playlist (code below).  But now I am stuck trying to pull the tracks for the playlist.  I would appreciate any insight you have into using the OLE methods, objects, or hierarchy.

Here is my current code.  All of the last 3 lines produce errors (I have commented them out with #)

#!C:\Perl64\bin\perl.exe
use strict;
 use Win32::OLE;
my $iTunesApp = new Win32::OLE("iTunes.Application");
 my $track = $iTunesApp->CurrentTrack;
 print "Current Track: " . $track->Name . "\nCurrent Artist: " . $track->Artist . "\n";
 my $list = $iTunesApp->CurrentPlaylist;
 print "Current Playlist: " . $list->Name . "\n";

 #my $tracklist = $iTunesApp->SelectedTracks;
 #print "Tracks: " . $tracklist->Item . "\n";
 #Print "$tracklist" ;

Here is the output from above.
c:\itunes\iTuPlay>c:\perl64\bin\perl.exe ituplay.pl
Current Track: Blues Is My Business (And Business Is Good) _West-Coast-Swing
Current Artist: The Groove Hogs
Current Playlist: Perl test

Here are the errors when the lines are not commented out:
c:\itunes\iTuPlay>c:\perl64\bin\perl.exe ituplay.pl
String found where operator expected at ituplay.pl line 19, near "Print "$trackl
ist""
        (Do you need to predeclare Print?)
syntax error at ituplay.pl line 19, near "Print "$tracklist""
Execution of ituplay.pl aborted due to compilation errors.

Here is some information from the SDK.  I need help understanding how to use it.
IITSource::Playlists().

IITTrackCollection Interface Reference

Detailed Description
Represents a collection of track objects.
Note that collection indices are always 1-based.

You can retrieve all the tracks defined for a playlist using IITPlaylist::Tracks().

You can retrieve the currently selected track or tracks using IiTunes::SelectedTracks().

HRESULT  Count ([out, retval] long *count)
  Returns the number of tracks in the collection.
 
HRESULT  Item ([in] long index,[out, retval] IITTrack **iTrack)
  Returns an IITTrack object corresponding to the given index (1-based).
ASKER CERTIFIED SOLUTION
Avatar of Darrell Porter
Darrell Porter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial