Solved

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

Posted on 2014-10-03
3
251 Views
Last Modified: 2014-10-17
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"
0
Comment
Question by:aa-denver
  • 2
3 Comments
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 40361434
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.
0
 
LVL 4

Author Comment

by:aa-denver
ID: 40366640
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).
0
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 500 total points
ID: 40366808
Just for completeness of answer, arrays described as being 1-based are arrays in which the first element of the array, N(), is located at the first position N(1), as opposed to zero-based arrays whose first element is located at the zeroth position, N(0).
The tracklist is a collection, or an array of elements, not a text string.  The print command has a string as an argument.
You'll note the command
print "Current Playlist: " . $list->Name . "\n";
does not generate an error, so  you do not need to pre-declare print.

For specific scripting examples of how to instantiate the objects and use them and their associated collections, you may want to review the scripts located here.  While these are written in Visual Basic Scripting, it may help you to understand what needs to be done to get the information you're looking for.

For examples in Perl, please review these scripts.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now