views:

213

answers:

3

Hi,

I want to use Scripting Bridge to add a movie to iTunes. And preferably letting me choose between a 'music video' and a 'movie'. I know both Objective-C and AppleScript so I thought it wouldn't be that hard but I can't figure it out. I know how I would use NSAppleScript for it but I'm targeting 10.5 or later and read that Scripting Bridge obsoletes NSAppleScript. Is that right?

All I got is

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier: @"com.apple.iTunes"];

Which is, as you can see, not much at all.

+1  A: 

You should use the "scripting definition processor" (sdp) program to generate a header file from iTunes' scripting definition (.sdef) file (which you can get using the sdef program):

sdef /Applications/iTunes.app | sdp -fh --basename "iTunes"

This'll give you a file called iTunes.h. Then you include that header into your project and read through it to see what the iTunes scripting bridge interface offers.

If it seems like you won't be able to do this with the scripting bridge (it's possible -- not everything that can be done via an app's AppleScript interface can also be done via the scripting bridge), just go ahead and write an AppleScript to do it instead, and then execute that in your program with NSAppleScript.

hasseg
I already did the sdp thing. And I think that the method I need is add:to: . But there's no documentation about it.
Jeroen
If it can't be done via the scripting bridge, you can try [appscript](http://appscript.sourceforge.net/) or just a plain old AppleScript instead.
hasseg
This is definitely possible via the Scripting Bridge: I've done similar things many times with Python.
wbg
the method [iTunesApp add:to:] works - just use an array of NSURL objects as the first parameter and nil as the second.
adib
+1  A: 

Step 1. Generate iTunes.h header file:

sdef /Applications/iTunes.app | sdp -fh --basename "iTunes"

Step 2. The code to add a media file looks like the following:

NSString* sourceMediaFile = ...;
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
iTunesTrack * track = [iTunes add:[NSArray arrayWithObject:[NSURL fileURLWithPath:sourceMediaFile]] to:nil];
NSLog(@"Added %@ to track: %@",sourceMediaFile,track);
adib
A: 

For the second parameter, it takes a playlist object (or nil as previously mentioned). Once you have fetched an instance of a iTunesPlaylist* object through some means (there are several depending on your needs), you can pass it as the second parameter.

Jeremy Johnstone