Re: how can I pass file ref in objc scripting bridge (trying to add track to iTunes)
Re: how can I pass file ref in objc scripting bridge (trying to add track to iTunes)
- Subject: Re: how can I pass file ref in objc scripting bridge (trying to add track to iTunes)
- From: has <email@hidden>
- Date: Sat, 23 Feb 2008 00:35:48 +0000
James Mead wrote:
I'm trying to add an mp3 file to iTunes using the scripting bridge.
I'm successful at instantiating an iTunes object and talking to it
(able to
retrieve playlist and track info), but I'm at a loss at to how to
add a
track.
Code portions:
// find already running iTunes
iTunesApplication *iTunes = [SBApplication
applicationWithBundleIdentifier:
@"com.apple.iTunes"];
// a test file to add (not shown - I used FSNewAliasFromPath and
FSPathMakeRef
to verify the path name was valid and could be turned into a file
reference)
NSString *aFilePath = @"//Users/jmead1/bach.mp3"; // test file
iTunes 'add' command requires a list of aliases (typeAlias
descriptors) as its direct parameter. Most applications will ask the
Apple Event Manager to coerce the supplied values to the correct types
as needed; for example, if you pass a typeFileURL descriptor, the AEM
will convert this to a single item AEList containing a typeAlias
descriptor. AEM's support for text/file coercions is patchy, however,
and the default coercion handlers don't recognise POSIX paths at all
[1], so passing POSIX path strings to commands that require file
objects won't work.
Also, Scripting Bridge doesn't provide a proper mapping for typeAlias
descriptors. For commands that require aliases, you either have to
pass an NSURL which SB will pack as a typeFileURL descriptor and [hope
that] the application is smart enough to coerce it to typeAlias, or
else construct your own typeAlias NSAppleEventDescriptors, which is a
bit tedious but doable.
// to add a file to iTunes library, add it to the library playlist.
Script
Editor shows the to: part as optional, but if I leave it out I get
syntax
error.
Scripting Bridge doesn't do optional parameters in the conventional
sense; instead, you have to pass nil for any parameters you don't want.
// locate the iTunes library playlist
SBElementArray *sources = [iTunes sources];
iTunesSource *libsource=nil;
uint i;
for (i=0; i<[sources count]; i++)
[...]
Or you could just ask for the first library playlist of the first
source, which is what AppleScripters do. (Technically you don't even
need to specify the source - e.g. 'tell app "iTunes" to get library
playlist 1' is quite legal and supported, if undocumented - but SB is
super-pedantic about these sorts of things.)
If you're new to AppleScript and iTunes scripting, I'd suggest getting
a copy of Matt Neuburg's 'AppleScript: The Definitive Guide' (2nd
edition) and grokking the basics of the AppleScript language and
application scripting principles in general. You'll need to know the
latter regardless of what language/bridge you're using, and knowing a
bit of AppleScript is handy given that most applications' scripting
interfaces are woefully under-documented and figuring out how to use
them often requires studying existing AppleScripts (for iTunes
scripts, see <http://dougscripts.com/>) and/or asking for advice on
AppleScript-users.
BTW, you might also take a look at objc-appscript (obligatory self-
plug; see my sig. for links) which provides fuller Cocoa-AE type
mappings, more flexible parameter syntax, better application
compatibility, 10.3+ support and various other advantages over SB.
HTH
has
[1] The default text<->file coercion handlers all use HFS paths for
legacy reasons. Personally I think Apple should change them all to use
POSIX paths; while it might mean breaking backwards compatibility for
some applications/AppleScripts, it's not like there isn't precedent
(c.f. AppleScript 2.0's move to Unicode). Feel free to file a feature
request if this bothers you.
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
_______________________________________________
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