Re: Play Playlist in iTunes
Re: Play Playlist in iTunes
- Subject: Re: Play Playlist in iTunes
- From: "Mr. Gecko" <email@hidden>
- Date: Fri, 3 Oct 2008 21:54:10 -0500
I am not using AppleScript to do this, I am using AppleEvents, as you
can see in code below. This is way faster than using AppleScript.
- (void)playPlaylist:(NSString *)playlist
{
OSErr err;
AppleEvent cmdEvent;
err = AEBuildAppleEvent(iTunesSignature,
ET_PLAY,
typeApplSignature,
&iTunesSignature,
sizeof(iTunesSignature),
kAutoGenerateReturnID,
kAnyTransactionID,
&cmdEvent,
NULL,
"'----':obj { form:'name', want:'type'(cPly), seld:'utxt'(@),
from:'null'() }",
[playlist lengthOfBytesUsingEncoding:NSUnicodeStringEncoding],
[playlist cStringUsingEncoding:NSUnicodeStringEncoding]);
if (err != noErr) {
ETLog(@"Error creating Apple Event: %d", err);
return;
}
err = AESendMessage(&cmdEvent, NULL, kAENoReply | kAENeverInteract,
kAEDefaultTimeout);
if (err != noErr) {
ETLog(@"Error sending AppleEvent: %d", err);
}
AEDisposeDesc(&cmdEvent);
}
- (void)playTrack:(long)track ofPlaylist:(NSString *)playlist
{
OSErr err;
AppleEvent cmdEvent;
err = AEBuildAppleEvent(iTunesSignature,
ET_PLAY,
typeApplSignature,
&iTunesSignature,
sizeof(iTunesSignature),
kAutoGenerateReturnID,
kAnyTransactionID,
&cmdEvent,
NULL,
"'----':obj { form:'indx', want:'type'(cTrk), seld:'long'(@),
from:obj { form:'name', want:'type'(cPly), seld:'utxt'(@),
from:'null'() } }",
track,
[playlist lengthOfBytesUsingEncoding:NSUnicodeStringEncoding],
[playlist cStringUsingEncoding:NSUnicodeStringEncoding]);
if (err != noErr) {
ETLog(@"Error creating Apple Event: %d", err);
return;
}
err = AESendMessage(&cmdEvent, NULL, kAENoReply | kAENeverInteract,
kAEDefaultTimeout);
if (err != noErr) {
ETLog(@"Error sending AppleEvent: %d", err);
}
AEDisposeDesc(&cmdEvent);
}
I would prefer using AppleEvents because it is the right way to
communicate with different things in the os, using cocoa.
Sorry if there was any mess understanding,
Mr. Gecko
P.S. this code was made with EyeTunes, framework, in mind so I used
the available properties and references for creating the code so it
would be all on the same track as the rest of the code in EyeTunes.
On Oct 3, 2008, at 8:18 PM, has wrote:
On 3 Oct 2008, at 17:09, Mr. Gecko wrote:
On Thu, Oct 2, 2008 at 9:52 PM, Mr. Gecko wrote:
I know but I can't find out the AppleEvent for Play Playlist.
Is there some sort of a program that will parse the AppleScript
and make an
cocoa AppleEvent code.
If you just want the raw four-char-codes from an application's
dictionary so you can construct NSAppleEventDescriptors yourself,
you can obtain them in various ways: [...]
I already looked at that but it doesn't have an actual code for
Play Playlist
I found this though <class name="playlist" code="cPly"
description="a list of songs/streams" inherits="item"
plural="playlists"> which if you look at the output of AEDebugSends
than you can see that that is called when running the play playlist
command.
I think you're a bit confused about how iTunes' Apple event API
works. This is understandable, given how hopelessly inadequate 99%
of scriptable applications' documentation is, along with the rather
unusual, counter-intuitive way that Apple event IPC works in the
first place.
Suffice it to say: nothing in AppleScript makes sense except in the
light of RPC plus queries[1][2].
IOW, you can either approach it on its own terms and accept it for
what it is, which will weird you out till you get your head around
it; or else you can try to approach it in conventional OO terms, in
which case it will frequently confuse and frustrate you whenever it
behaves in a non-OO fashion (which is quite often).
Regarding the playing of iTunes playlists, here's a quick attempt to
clarify:
iTunes has no 'play playlist' command as you suggest. There is,
however, a 'play' command (Apple event), and various kinds of
playable objects: sources, several kinds of playlists (audio CD
playlists, library playlists, user playlists, etc), and several
kinds of tracks (file tracks, URL tracks, etc).
To use the 'play' command, you construct a query ("reference" in
AppleScript jargon) identifying the object you want played, then
pass that query as the 'play' command's direct parameter. e.g.:
play (source 1)
play (source "Library")
play (playlist "My Top Rated")
play (first playlist whose name is "My Top Rated")
play (user playlist "My Top Rated")
play (user playlist "My Top Rated" of source 1)
play (track 1 of user playlist "My Top Rated")
play (file track id 56315 of user playlist id 520 of source id 41)
etc.
As you can see, queries can be pretty flexible in how they identify
objects. But whatever query you use, it's up to iTunes' 'play' event
handler to evaluate that query in order to locate the object or
objects to act upon, and do its funky thing.
Oh, and BTW: one thing you will almost never find provided by
applications' scripting documentation (be it built-in dictionaries
and/or supplementary files) is any formal indication of which
commands can operate on which objects, so expect to use some
intelligent guesswork and trial-and-error testing to figure this out
for yourself. (Yes, this sucks; AppleScripters have been kvetching
about it for the last decade, without notable effect. It's just
something you'll have to deal with; the AppleScript-users list is
good for advice, and there's a whole stack of existing iTunes
scripts at <http://dougscripts.com> to learn from.)
...
If you want to try to wrap your head around the "AppleScript way",
chapter 2 in the appscript manual tries to provide a quick summary
of the concepts involved. There's also an excellent paper by William
Cook (one of the original AppleScript designers), which describes
both the language and the Apple event-based IPC system created
around it, and provides significant insights into the original
motives and decisions behind its design. It dates back to the early
days of AppleScript so doesn't discuss the more recent related Cocoa-
based APIs, but the underlying principles are unchanged:
http://www.cs.utexas.edu/users/wcook/Drafts/2006/ashopl.pdf
Matt Neuburg's "AppleScript: The Definitive Guide" also provides a
good, critical, programmer-friendly guide to AppleScript and
application scripting principles; obviously a big chunk of the book
is about AppleScript itself which might not be of so much interest
to you, but I've heard at least one other non-AppleScript user say
that they've found it helpful, albeit after a slow start (presumably
the basic AppleScript language chapters).
HTH
has
[1] (With apologies to Theodosius Dobzhansky.)
[2] FWIW, it is an odd and unfamiliar way to do IPC, and there are a
lot of real-life design and implementation shortcomings that make it
much more difficult than it ought to be, but at a basic level it is
a logical and self-consistent - and even somewhat elegant (if
unusual) - system. The nearest analogy I can think of is using XPath
queries over XML-RPC, if that helps.
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden