Access/control scriptable application using scripting bridge and Cocoa

ScriptingBridge framework in Cocoa can be used to access features from any scriptable application. In this article I will discuss how to access the features of iTunes such as current playing track, duration of the track, playlists etc by using ScriptingBridge.framework in a Cocoa Application. However you can follow the same steps to allow your application to interact with any other scriptable application.

First of all, we need to create a header file of the application with which you want your code to interact. This can be done by executing a terminal command as:

sdef /path/to/ | sdp -fh --basename applicationName

Here, in place of /path/to/application you need to specify the complete path of application  to which you want to send messages using your code. And in place of applicationName you need to specify the name of that application.

In this case, I will create a header file for iTunes application as:

sdef /Applications/ | sdp -fh –basename iTunes

This will create a file iTunes.h in your current directory (the one from where you executed the above command in terminal). By default the directory for terminal will be your Home Directory.

Now copy this file “iTunes.h” from here to your project directory and add it to your project by dragging it in your Xcode project.

The next thing to do is:  Add the Scripting Bridge framework to the project.

Now we can access iTunes from your application in the following way:

Import the file iTunes.h in your source file where you want to access the features of iTunes by using #import “iTunes.h” statement.

Now, we need to create an object of class iTunesApplication (this class is present in iTunes.h file). We can do it with the help of SBApplication.

iTunesApplication* iTunesApp = [SBApplication applicationWithBundleIdentifier:@””];

where is the bundle identifier of iTunes application.

You may find the bundle identifier of any any application by looking at its info.plist in that application’s bundle.

Now once we got the object of iTunesApplication class, we can now use it to access iTunes. Look into the class iTunes.h to find out the methods that you can use to access and control iTunes.

As an example I will show you how to use some of the methods of iTunes.h to get information from iTunes.

First of all, You may check if the application iTunes is open or not by using

[iTunesApp isRunning]

The above method will return TRUE if iTunes is running and FALSE if iTunes is not running.

Similarly, you may check the current state of iTunes, i.e. iTunes is playing a song, song is in paused state or it is in stopped state. The method below will provide you this information:

if ([iTunesApp playerState] == iTunesEPlSStopped)


NSLog(@”Stopped State”);


else if ([iTunesApp playerState] == iTunesEPlSPlaying)


NSLog(@”Playing state”);


The other states that you can check in similar way are:

iTunesEPlSStopped = Stopped state,
iTunesEPlSPlaying = Playing state,
iTunesEPlSPaused = Paused state,
iTunesEPlSFastForwarding = Fast forwarding,
iTunesEPlSRewinding = Rewinding

Also, you may toggle the play/pause button of iTunes from your application by using

[iTunesApp playpause];

This method will play the track if iTunes is in paused/stopped state and will pause the track if it is in playing state. Does the same work as done when you manually toggle the play/pause button of iTunes.

Similarly you may get the playlist or track information from iTunes, get and set the position of pointing head, and open a track in iTunes by using the methods present in iTunes.h.

[iTunes openLocation:@”path of track to be played”];  //Open the track in iTunes

[iTunesApp playerPosition];     //Returns the position of playing head in current track.

[iTunesApp setPlayerPosition: 5];     //Set the player position to 5 in iTunes.

There are various other useful methods that you can explore from the header file itunes.h and use them to access information from iTunes or control the application iTunes.

Note that if you call any of the above methods when the application iTunes is closed, scripting bridge will force open the iTunes Application. So remember to check if iTunes is running or not before using the methods to control iTunes.

In the similar way  you may generate the header file for any scriptable application and explore it to find the methods that you can use to access that application.

Written By: Neha Gupta, Software Engineer, Mindfire Solutions


About Neha Gupta

Software Engineer at Noida, India, Work on MAC OSX Application development with Cocoa, iOS development and Qt (Cross Platform Application Framework)

Posted on February 12, 2014, in Cocoa Application, Objective-C and tagged , , , , , , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: