Expressing a range (or slice) with ScriptingBridge

167 views Asked by At

I'm trying to express a slice ("thru in AppleScript") in Objective-C using Scripting Bridge. The example code is making a selection in iWorks Pages. The AppleScript code looks like this

 tell application "Pages"
     tell document 1
         select (characters 8 thru 14)
     end tell
 end tell

and the Objective-C code is here. What I need is a way of expressing (characters 8 thru 14) in Objective-C.

PagesApplication *app;
app = [SBApplication applicationWithBundleIdentifier:@"com.apple.iWork.Pages"];
SBElementArray *docs = [app documents];
PagesDocument *doc = [docs objectAtIndex:0];

// now we need to express
// "select (characters 8 thru 14)" in obj-c

// ??

Thankful for any help. Björn

1

There are 1 answers

0
user616030 On

I had to use low level AppleEvents. Did monitor the Apple Events from the command line. Here is a code that does it:

AppleEvent eventToSend, eventToReceive;
OSStatus err;
AEBuildError buildError;

char *bundleID = "com.apple.iWork.Pages";

eventToSend.descriptorType    = 0;
eventToSend.dataHandle        = NULL;

eventToReceive.descriptorType = 0;
eventToReceive.dataHandle     = NULL;        

err = AEBuildAppleEvent(kAEMiscStandards,
                        kAESelect,
                        typeApplicationBundleID,
                        bundleID,
                        strlen(bundleID),
                        kAutoGenerateReturnID,
                        kAnyTransactionID,
                        &eventToSend,
                        &buildError,
                        "'----':'obj '{form:rang, want:type('cha '), seld:'rang' {"
                        "star:'obj '{form:indx, want:type('cha '), seld:long(@), from:ccnt()},"
                        "stop:'obj '{form:indx, want:type('cha '), seld:long(@), from:ccnt()}},"
                        "from:'obj '{form:indx, want:type(docu), seld:long(1), from:()}}", 8, 14);

if (err != noErr) {
    NSLog(@"failed to build Apple Event. Error code %d at pos %d\n", buildError.fError, buildError.fErrorPos);
    exit(1);
}

err = AESendMessage(&eventToSend, &eventToReceive, kAEWaitReply, kAEDefaultTimeout);

if (err != noErr) {
    NSLog(@"failed to send Apple Event\n");    
    exit(1);
}

/* deallocate memory */
AEDisposeDesc(&eventToReceive);
AEDisposeDesc(&eventToSend);