Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
more useful options
 
PerlMonks

iTunes and Windows and Perl == Bliss

by mdog (Pilgrim)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Jul 25, 2005 at 18:21 UTC ( #477933=CUFP: print w/ replies, xml ) Need Help??

Brethern --

I have been waiting for quite some time for an API into iTunes so that I could control it from my keyboard or X10 remote controls and they released an SDK awhile back that I just discovered.

I know that you Mac users have Mac::iTunes but Windows users can now control iTunes natively.

James Craig has done some excellent work with the SDK and I borrowed the "SearchiTunesWinForSong" routine from him.

I am going to come up with a more complete library of functions as time allows but wanted to get this out so folks know that they can start hacking away at iTunes.

Enjoy,
mdog

use Win32::OLE; Win32::OLE->Option(Warn => \&OLEError); #find and start a playlist my $playlistHandle = GetPlaylistFromSource("Matt","Library"); $playlistHandle->PlayFirstTrack(); # find and rate a song my $trackHandle = SearchiTunesWinForSong( artist => "10,000 Maniacs", title => "Candy Everybody Wants", location => "F:\\backup1\\mp3\\10,000 Mani +acs - Candy Everybody Wants.mp3" ); #various methods and objects are case insenstive my $status = $trackHandle->{RATing} = 80; print $trackHandle->{rAting} . "\n"; print $trackHandle->Rating . "\n"; # get the current song playing print GetCurrentlyPlayingSong() . "\n"; sub OLEError { print (Win32::OLE->LastError() . "\n"); } # you can this more directly if you reference the source and playlist +directly # but I prefer to have case insensitivity and partial source / playlis +t naming available sub GetPlaylistFromSource{ my($desiredPlaylist,$desiredSource) = @_; my ($iTunes) = new Win32::OLE( "iTunes.Application"); my $sources = $iTunes->Sources; my $count = $sources->{"Count"}; my $foundPlaylist; # loop through all the sources: be it library, radio, ipod, etc for(my $i = 1; $i <= $count; $i++){ my $source = $sources->Item($i); my $sourceName = $source->{"name"}; # found the source if($sourceName =~ /$desiredSource/i){ #print "$sourceName\n"; my $playlists = $source->Playlists; my $playlistCount = $playlists->Count; # now loop through all the playlist on that device / drive for(my $i = 1; $i <= $playlistCount; $i++){ my $playlist = $playlists->Item($i); my $playlistName = $playlist->{"name"}; if($playlistName =~ /$desiredPlaylist/i){ #print "\t$playlistName\n"; $foundPlaylist = 1; return $playlist; } } } } if(! $foundPlaylist){ print "Did not find desired playlist\n"; exit; } } sub GetCurrentlyPlayingSong{ my ($iTunes) = new Win32::OLE( "iTunes.Application"); # find out what the current track playing is my $currentTrack = $iTunes->CurrentTrack; my $location = $currentTrack->Location; my $filename = $location; $filename =~ s/^.*\\//gis; return $filename; } sub SearchiTunesWinForSong{ my %searchTerms=@_; my $IITrackKindFile = 1; my $ITPlaylistSearchFieldVisible = 1; my $status; my ($iTunes) = new Win32::OLE( "iTunes.Application"); # don't bother unless we have a location and at least one of title +/artist/album return 0 unless $searchTerms{location} and ($searchTerms{title} or + $searchTerms{artist} or $searchTerms{album}); # reverse the slashes in case SlimServer is running on UNIX #$searchTerms{location} =~ s/\//\\/g; #replace \\ with \ - not consistent within iTunes (not in my libra +ry at least) #$searchTerms{location} =~ s/\\\\/\\/; my $mainLibrary = $iTunes->LibraryPlaylist; my $tracks = $mainLibrary->Tracks; #now find it my $searchString = ""; if ($searchTerms{artist}) { $searchString .= "$searchTerms{artist} "; } if ($searchTerms{album}) { $searchString .= "$searchTerms{album} "; } if ($searchTerms{title}) { $searchString .= "$searchTerms{title}"; } #print( "Searching iTunes for *$searchString*\n"); my $trackCollection = $mainLibrary->Search($searchString, $ITPlaylistSearchFieldVisible); if ($trackCollection) { #print("Found ",$trackCollection->Count," track(s) in iTunes\n +"); #print("Checking for: *$searchTerms{location}*\n"); for (my $j = 1; $j <= $trackCollection->Count ; $j++) { #change double \\ to \ my $iTunesLoc = $trackCollection->Item($j)->Location; $iTunesLoc =~ s/\\\\/\\/; #watch out for blank space in iTunes (where did these come + from?) my $iTunesName = $trackCollection->Item($j)->Name; $iTunesName =~ s/\s*$//; my $iTunesArtist = $trackCollection->Item($j)->Artist; $iTunesArtist =~ s/\s*$//; my $iTunesAlbum = $trackCollection->Item($j)->Album; $iTunesAlbum =~ s/\s*$//; # escape all problem characters for search coming up my $searchLocation = $searchTerms{location}; $searchLocation =~ s/(\W)/\\$1/g; #check the name, location and type if ($trackCollection->Item($j)->Kind == $IITrackKindFile and (!$searchTerms{title} or $searchTerms{title} eq +$iTunesName) and (!$searchTerms{album} or $searchTerms{album} eq +$iTunesAlbum) and (!$searchTerms{artist} or $searchTerms{artist} eq +$iTunesArtist) #and ($searchTerms{location} eq $iTunesLoc) and ($iTunesLoc =~ m|$searchLocation$|) ) { #we have the file (hopefully) #print("Found track in iTunes\n"); return $trackCollection->Item($j); } else { #print("$j - False match: *$iTunesLoc*\n"); } } } return 0; }

READMORE tags added by Arunbear

Comment on iTunes and Windows and Perl == Bliss
Download Code
Re: iTunes and Windows and Perl == Bliss
by sithsasquatch (Scribe) on Jul 25, 2005 at 23:50 UTC
    This looks pretty fun; I'll have to give it a spin when I get home. (kind of a lame comment, but I'm too new to be able to vote you up)
[reply]
Re: iTunes and Windows and Perl == Bliss
by halley (Prior) on Jul 26, 2005 at 13:22 UTC
    Okay, I'll be a little snarky and point out the following conjecture.
    iTunes + Windows + Perl == Bliss Ignorance == Bliss Windows == Ignorance .: Perl == -iTunes

    --
    [ e d @ h a l l e y . c c ]

[reply]
[d/l]
Re: iTunes and Windows and Perl == Bliss
by tbone1 (Monsignor) on Jul 28, 2005 at 13:00 UTC
    I am a Perl programmer (obviously) and I love iTunes, but having Windows as part of something blissful seems to require such a lapse of the laws of physics that I must assume you are on drugs or Jayson Blair.

    Now Mac::iTunes, OTOH, is certainly enough to cause bliss, like watching Bill Gates get a blue screen of death.

    --
    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

[reply]
      Damn, I am taking a lot of heat for the title of my posting.

      OK, I am not a Windows lover (rather a user) and the reason for my bliss is not because I am on Windows but because I can talk natively to iTunes via Perl.

      So the title of my post probably should have been "iTunes (on Windows) and Perl == Bliss". I was just so blissed / blitzed out that I did't realize my folly. Forgive me, monks, I will strive to do better.

      I won't change it so that folks reading this thread aren't confused.

      Time to self flaggelate!

[reply]

Back to Cool Uses For Perl

Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: CUFP [id://477933]
Approved by ghenry
Front-paged by Tanalis
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others imbibing at the Monastery: (11)
GrandFather
jdporter
Your Mother
atcroft
herveus
thezip
Eyck
NodeReaper
ssandv
gnosti
im2
As of 2009-11-21 04:14 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (726 votes), past polls