rosascript 0.1.2

faster dx with obj c ffi bindings
Documentation
#import <Foundation/Foundation.h>
#include <Cocoa/Cocoa.h>

// Assuming these constants are defined somewhere
// Typically, they would be in the AppleScript framework, but you might need to define them yourself
const AEEventClass kASAppleScriptSuite = 'ascr';
const AEEventID kASSubroutineEvent = 'psbr';
const AEKeyword keyASSubroutineName = 'snam';

const char* executeAppleScript(const char *script) {
    NSString *scriptString = [NSString stringWithUTF8String:script];
    NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:scriptString];
    NSDictionary *errorDict;
    NSAppleEventDescriptor *result = [appleScript executeAndReturnError:&errorDict];
    if (errorDict) {
        NSLog(@"AppleScript Error: %@", errorDict);
        return strdup([[errorDict description] UTF8String]);
    }
    if (result) {
        return strdup([[result stringValue] UTF8String]);
    }
    return strdup("No result");
}


void *compileAppleScript(const char *scriptSource) {
    NSString *source = [NSString stringWithUTF8String:scriptSource];
    NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:source];
    NSDictionary *errorDict;
    if (![appleScript compileAndReturnError:&errorDict]) {
        // Compile error, handle appropriately
        return NULL;
    }
    return (__bridge_retained void *)appleScript;
}

const char *executeCompiledAppleScript(void *compiledScript) {
    NSAppleScript *appleScript = (__bridge NSAppleScript *)compiledScript;
    NSDictionary *errorDict;
    NSAppleEventDescriptor *result = [appleScript executeAndReturnError:&errorDict];
    if (result) {
        return [result.stringValue UTF8String];
    } else {
        // Execution error, handle appropriately
        return NULL;
    }
}
const char *sendAppleEvent(void *compiledScript, const char *eventCode, const char *parameters) {
      NSAppleScript *appleScript = (__bridge NSAppleScript *)compiledScript;

    // NSString *handlerName = @"getFolderSize"; // Handler name
    // NSString *parameter = @"/bin"; // Actual path to be passed to the handler

    NSString *parameterStr = [NSString stringWithUTF8String:parameters];
    NSString *eventCodeStr = [NSString stringWithUTF8String:eventCode];

    // Create an AppleEvent descriptor for the handler name
    NSAppleEventDescriptor *handlerNameDescriptor = [NSAppleEventDescriptor descriptorWithString:eventCodeStr];

    // Create an AppleEvent descriptor for the parameter
    NSAppleEventDescriptor *parameterDescriptor = [NSAppleEventDescriptor descriptorWithString:parameterStr];
    NSAppleEventDescriptor *eventParameter = [NSAppleEventDescriptor listDescriptor];
    [eventParameter insertDescriptor:parameterDescriptor atIndex:1];

    // Create an Apple Event to call the handler
    NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite eventID:kASSubroutineEvent targetDescriptor:nil returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
    [event setParamDescriptor:eventParameter forKeyword:keyDirectObject];
    [event setParamDescriptor:handlerNameDescriptor forKeyword:keyASSubroutineName];

    NSDictionary *errorDict;
    NSAppleEventDescriptor *result = [appleScript executeAppleEvent:event error:&errorDict];
    if (result) {
        return [result.stringValue UTF8String];
    } else {
        if (errorDict) {
            NSLog(@"AppleScript Execution Error: %@", errorDict);
        }
        return NULL;
    }
}