0

我正在尝试使用 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

// ??

感谢您的帮助。比约恩

4

1 回答 1

0

我不得不使用低级别的 AppleEvents。是否从命令行监视 Apple 事件。这是执行此操作的代码:

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);
于 2011-02-16T12:25:03.220 回答