This tutorial will guide you through the process of making a Cocoa application that uses the scripting capabilities. The SBSetFinderComment application described here is part of the sample applications and can be found in the distribution.

Before following this tutorial, make sure that you have correctly installed both the Monobjc bridge and the MonoDevelop addins.

The tutorial is divided into the following steps:

  • Creating the project
  • Generating scripting definitions
  • Writing some .NET code

Creating the project

Launch MonoDevelop and select File => New => Solution.... Open the C#/Monobjc folder and select Cocoa Application.

Name the application SBSetFinderComment and click the OK button. The new solution as well as the application project will be created.

When the application is created, double-click on the project and select the Monobjc settings. Add the ScriptingBridge framework and click on the OK button.

Generating scripting definitions

In order to use the scripting capabilities, you need to generate the scripting definitions of the target application, in this case the Finder application.

Open a Terminal window and go the application folder and type the following command:

Mac:SBSetFinderComment$ sdef /System/Library/CoreServices/Finder.app > Finder.sdef

This command will extract the scripting definitions offered by the Finder application.

Now, in the same Terminal window, type the following command:

Mac:SBSetFinderComment$ monobjc-sdp -i=./Finder.sdef -p=Finder

This command will produce the wrappers needed to interact with Finder application based on the scripting definitions.

When this file is generated, you can import it in the application project.

Writing some .NET code

Now that the scripting definitions and wrappers are generated, it is really easy to send commands to the Finder application:

// Retrieve the Finder application Scripting Bridge object.
FinderApplication finder = SBApplication.ApplicationWithBundleIdentifier ("com.apple.finder").CastAs<FinderApplication>();

// Retrieve a reference to our finder item asking for it by location 
FinderItem theItem = finder.Items.ObjectAtLocation (theFileURL).CastAs<FinderItem>();

// Display the item
theItem.Reveal ();

// Activate the Finder application
finder.Activate ();