使用ScriptingBridge表示范围(或切片)

时间:2011-02-14 10:38:41

标签: objective-c scripting-bridge applescript-objc

我正在尝试使用Scripting Bridge在Objective-C中表达一个切片(“通过AppleScript”)。 示例代码在iWorks Pages中进行选择。 AppleScript代码如下所示

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

并且Objective-C代码在这里。我需要的是在Objective-C中表达的方式(字符8到14)。

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

// ??

感谢任何帮助。的Björn

1 个答案:

答案 0 :(得分:0)

我不得不使用低级AppleEvents。是否从命令行监视Apple Events。 这是一个代码:

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);