In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Brian Pink) wrote:

> $_[1] is a known database_id from iTunes...
> 
> my $library = $itunes->obj( 'playlist' => '1' );
> my $tracks = $library->obj( 'tracks' => whose( 'database_id',
> 'equals', "$_[1]" ));
> 
> if( $^E ) { $msg .= $^E; }
> else
> {
>       for my $track ( $tracks->get() )
>       {
>               $itunes->play( $track );
>       }
>       
> }
> 
> apparently "some track" in AppleScript can correspond to "tracks" with
> Mac::Glue. since there is only one result, the syntax looks funky but
> works. hopefully someone else learns something from this... =)

First off: the for loop is completely unnecessary.  If you have only one 
item, you can just do $tracks->play (no need to get, and no need to do 
$itunes->play($track)).  If you have more than one item, you'd want to break 
out of the loop anyway: both $tracks->play and for ($tracks->get) { $_->play 
} will end up trying to play multiple tracks immediately.

Second, in AppleScript, "some track" is the same as "any track", and it is 
asking for one random element from a group.  And this is where AppleScript 
hides some details that Mac::Glue does not.  What is actually going on is 
AppleScript is converting "some track whose ..." to "some track of tracks 
whose ...".  For example, your code is equivalent to the longer form:

   some track of (tracks of library playlist 1
     whose database ID is x)

And this is the literal Mac::Glue translation (note that gAny is used for 
the synonymous some/any):

  my $track = $itunes->obj(
     track    => gAny(),
     tracks   => whose(database_id => equals => $x),
     playlist => 1
  );

But since we can only get one result (as database_id has to be unique), we 
can just get $tracks, as in AppleScript, you can also do:

   tracks of library playlist 1 whose database ID is x

And as you found out in Mac::Glue, you can do:

  my $track = $itunes->obj(
     tracks   => whose(database_id => equals => $x),
     playlist => 1
  );

So pulling this all together, I'd do it like this:

#!/usr/bin/perl
use warnings;
use strict;
use Mac::Glue ':all';

my $itunes = new Mac::Glue 'iTunes';
my $id = 45;

my $track = $itunes->obj(
   tracks   => whose(database_id => equals => $id),
   playlist => 1
);

$track->play;

__END__

I hope that helps a bit.

BTW, I posted a Name That Tune script using iTunes and Mac::Glue.

  http://use.perl.org/~pudge/journal/19985

-- 
Chris Nandor                      [EMAIL PROTECTED]    http://pudge.net/
Open Source Development Network    [EMAIL PROTECTED]     http://osdn.com/

Reply via email to