Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / July 2004



Tip: Looking for answers? Try searching our database.

Mac::Glue which syntax

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian Pink - 27 Jul 2004 15:01 GMT
So I'm using Mac::Glue to talk to iTunes. I've got this line that
works in AppleScript:

play some track of library playlist 1 whose database id is x -- (
where x is a known db id. )

i've tried converting this to Mac::Glue like this:

my $track = $itunes->obj( 'track' => whose( 'database_id', 'equals', $_[1] ));

and I seem to get an object back, i.e. no errors, but if I do this:

print( Dumper( $track->get());

I get $VAR1=undef; Obviously, something is wrong, but the whose seems
to be syntactically correct, so I'm wondering if the omission of the
"some" from the AppleScript is affecting it or what. I also have the
library identifier in the AppleScript, which I have just completely
ignored. Any insights are appreciated, and I'll write again if I find
any solutions.

- brian
Brian Pink - 27 Jul 2004 16:34 GMT
ok, i adjusted the code to look like:

my $library = $itunes->obj( 'library' );
my $track = $library->obj( 'track' => whose( 'database_ID', 'equals', $_[1] ));

which i think should be more accurate, but i'm still getting no track
object back.

back to the mines!

> So I'm using Mac::Glue to talk to iTunes. I've got this line that
> works in AppleScript:
[quoted text clipped - 18 lines]
>
> - brian
Brian Pink - 27 Jul 2004 19:12 GMT
one more update. this is working as expected.

$_[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... =)
Chris Nandor - 27 Jul 2004 22:07 GMT
> $_[1] is a known database_id from iTunes...
>
[quoted text clipped - 15 lines]
> 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

Signature

Chris Nandor                      pudge@pobox.com    http://pudge.net/
Open Source Development Network    pudge@osdn.com     http://osdn.com/

Brian Pink - 27 Jul 2004 22:33 GMT
chris,

thanks for the enlightenment. it was one of those situations where i
knew there had to be something better, but just wasn't sure what it
was. =)

- brian
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.