Mac OS X iTunes Development - Easy Cocoa Integration with iTunes

Oct 05, 2010
German Villacreces

You CAN integrate your mac application with iTunes, this post assumes you have basic knowledge of Objective-C. The Cocoa frameworks makes it very easy to add animations, link an action to a button, play videos, audio files and much more. You can use the QTKit framework to embed a QuickTime player in your application, but when it comes to iTunes integration, there is no framework to play with like there is for QuickTime. Nevertheless, that doesn’t mean you can’t integrate your app with iTunes. The solution is AppleScripts.

The concept of AppleScripts is very simple, its a scripting language developed by Apple that can control other applications. Cocoa can run, send data and get data from applescripts. Lets look at the most simplest example applescript that simply opens iTunes:

tell application "iTunes"
end tell

This scripting language is very easy to write. This script tells iTunes to run like you can probably figure out by just reading the code. To run this script we use event descriptors, there are several lines of code required to run scripts and running them with arguments is very different to running them without arguments. I built this manager class (ScriptManager.h,ScriptManager.m) to do all the dirty work for us, its a very simple class with three methods:

(void) executeScript:(NSString *) name;

You can use this method if you simply want to run a script that takes no arguments, the name argument is the script filename you have added to your Cocoa project.

(void) executeScript:(NSString *) name withArguments:(NSArray *) args;

When you have a script that takes one or more arguments, you call this method and send the arguments as an NSArray.

(NSMutableArray *) getResults;

This method returns a mutable array with all the results that the most recently executed script returned.

You will also see a method called “arrayFromDescriptor”, don’t pay any attention to that one, its just an internal method.

So how do we use this class?  In a nutshell, first you add the applescript to your project, adding it to the Resources folder is not enough, you need to drag and drop the script to the Copy Bundle Resources group in the Targets tree, under your project’s name. Otherwise the manager wont find your script. Once you have done that you simple import the ScriptManager class to your project, and make an instance of the class to run any number of scripts.

Lets build an example Cocoa project called ScriptDemo, we will have our AppDelegate main class header and main file (ScriptDemoAppDelegate.h,ScriptDemoAppDelegate.m). We add the ScriptManager.h and ScriptManager.m files to our project, and on our ScriptDemoAppDelegate header class, we add the required lines (in bold and color);

#import <Cocoa/Cocoa.h>
#import "ScriptManager.h"

@interface ScriptDemoAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    ScriptManager *manager;


@property (assign) IBOutlet NSWindow *window;



We create a ScriptManager object, and the IBAction to trigger the applescript with a button.

Open MainMenu.xib on Interface Builder and place a button (Push Button) anywhere in the window, change its label to “Start iTunes” and link it to the action startiTunes.

Next, open the ScriptDemoAppDelegate main class, and place the IBAction definition:

#import "ScriptDemoAppDelegate.h"

@implementation ScriptDemoAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application

    manager = [ScriptManager new];
    [manager executeScript:@"startiTunes"];


All we do here is create an instance of the ScriptManager class and call the executeScript method to trigger an applescript without sending any arguments. Make sure the applescript is called “startiTunes” and its placed on the Copy Bundle Resource group under Targets like I mentioned before.

Build and run your project, click on the Start iTunes button and iTunes should run!

Here are the used files:

ScriptDemo XCode Project
ScriptManager Class

I ope this small example was helpful, feel free to post any comments or questions.