Add movie to iTunes using Scripting Bridge

2019-03-20 06:32发布

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.

3条回答
趁早两清
2楼-- · 2019-03-20 07:19

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.

查看更多
Bombasti
3楼-- · 2019-03-20 07:27

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.

查看更多
仙女界的扛把子
4楼-- · 2019-03-20 07:34

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);
查看更多
登录 后发表回答