Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 HelloThere.xcodeproj
Octocat-spinner-32 HelloThere
Octocat-spinner-32 .gitignore


Demonstration app for post Calling AppleScriptObjC files from Objective-C

From Original Post

Accessing Objective-C from inside your AppleScriptObjC files is very straight forward and works just as you would expect. Going the other way is not as intuitive but it is simple.

The first thing is to import the AppleScriptObjC framework into your project.
Right-click on Frameworks and choose Add Exisiting Frameworks.

Next, change the main.m file as follows. #import #import

int main(int argc, char *argv[])
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [[NSBundle mainBundle] loadAppleScriptObjectiveCScripts];
    [pool release];

    return NSApplicationMain(argc, (const char **) argv);

Objective-C requires that you add an #import statement when accessing methods from another class file. AppleScriptObjC does not have a header file so there is no way to use the #import statement for this purpose.

There are two ways that I know of to accomplish this task. One is using the function NSClassFromString(). The second is to create an IBOutlet and connect to it in Interface Builder.

Using the first method, you will need to add an ivar to your interface file using id as the type.

id scriptFile;

Then in the .m file, add the following code. A good place would be in the awakeFromNib method. scriptFile = NSClassFromString("ScriptFileName"); if ( !scriptFile ) { // handle error loading file here return; }

Say you have an AppleScript file with the following. script Hello on say_(phrase) say "\"" & phrase & "\"" end end script

Then you could call it from Objective-C like normal.

[scriptFile say:@"How are you today?"];

Second Method

Add this to the interface file.

IBOutlet id scriptFile;

Open Interface Builder, add a Controller object and set its class to ScriptFileName (this is the name of your AppleScriptObjC file). Now connect this object to your Objective-C class outlet scriptFile. Communication is the same as in the previous example.

Important Final Instructions

You will note that you are getting compiler warnings for the handlers you are calling on the AppleScriptObjC file. This is because there is no header declaring these handlers. The application will run just fine but the solution to suppress the compiler warnings is very simple. We need to add a category on NSObject that includes the handlers that we are accessing in the AppleScriptObjC file. Add this just above the @implementation declaration in the .m file.

@interface NSObject (ASHandlers)
- (void)say:(NSString *)phrase;

Released under BSD license.

Something went wrong with that request. Please try again.