objective-c - OBJ-C 脚本桥 - 使用属性创建脚本对象时遇到问题 (Microsoft Word)

标签 objective-c cocoa scripting applescript scripting-bridge

我一直在用这个把头撞在墙上。我正在尝试使用脚本桥在 MS Word 中创建一个新的自动文本条目。

这是我尝试使用的代码:

wordApplication *theWordApp = [SBApplication applicationWithBundleIdentifier:@"com.microsoft.Word"];
wordTemplate *theWordTemplate = [[theWordApp activeDocument] attachedTemplate];
wordAutoTextEntry *theNewAutoTextEntry = [[[theWordApp classForScriptingClass:@"auto text entry"] alloc] init];
[[theWordTemplate autoTextEntries] addObject:theNewAutoTextEntry];
[theNewAutoTextEntry setName:@"test name"];
NSLog(@"%@", [theNewAutoTextEntry name]);

使用这个,我得到以下错误:

*** -[SBProxyByClass setName:]: object has not been added to a container yet; selector not recognized [self = 0x6d9fbd0]

我也试过:

wordApplication *theWordApp = [SBApplication applicationWithBundleIdentifier:@"com.microsoft.Word"];
wordTemplate *theWordTemplate = [[theWordApp activeDocument] attachedTemplate];
wordAutoTextEntry *theNewAutoTextEntry = [[[theWordApp classForScriptingClass:@"auto text entry"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:@"testname", @"test", nil]];
NSLog(@"%@", [theNewAutoTextEntry name]);

我用这种方式得到了同样的错误。

有趣的是,当我运行以下命令时:

wordApplication *theWordApp = [SBApplication applicationWithBundleIdentifier:@"com.microsoft.Word"];
wordTemplate *theWordTemplate = [[theWordApp activeDocument] attachedTemplate];
wordAutoTextEntry *theNewAutoTextEntry = [[[theWordApp classForScriptingClass:@"auto text entry"] alloc] init];
NSLog(@"%@", theNewAutoTextEntry);

我得到这个输出:

<future MicrosoftWordAutoTextEntry with properties (null)>

其中自动文本输入指示为“ future ”。有任何想法吗?提前致谢!

最佳答案

您走在正确的轨道上,但似乎没有从其数组中获取对象,而这是使用它所必需的。最好使用 name 属性初始化对象,以确保轻松检索。即...

    word011 *theWordApp = [SBApplication applicationWithBundleIdentifier:@"com.microsoft.Word"];
    word2011Template *theWordTemplate = [[theWordApp activeDocument] attachedTemplate];

    // create the object with them name property to grab it later
    NSString *testName = @"test name";
    word2011AutoTextEntry *theNewAutoTextEntry = [[[theWordApp classForScriptingClass:@"auto text entry"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:testName, @"name", nil]];
    [[theWordTemplate autoTextEntries] addObject:theNewAutoTextEntry];

    // you created the object, but now you have to actually *get* the object
    // check for existence first other EXC_BAD_ACCESS will happen
    if ( [[[theWordTemplate autoTextEntries] objectWithName:testName] exists] {
        theNewAutoTextEntry = [[theWordTemplate autoTextEntries] objectWithName:testName];
    }

    // name already set, so you can move with working with the object
    //[theNewAutoTextEntry setName:@"test name"];

关于objective-c - OBJ-C 脚本桥 - 使用属性创建脚本对象时遇到问题 (Microsoft Word),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17686350/

相关文章:

ios - 将新属性添加到现有实体时是否需要迁移核心数据?

iphone - 从类别中采用协议(protocol)

powershell - 无法使用Get-MessageTrackingLog作为Sender@domain.com.CSV导出外部电子邮件收件人的列表?

objective-c - 如何禁止用户在 NSTextField 中输入数字?

scripting - 关于第二人生中的家具制作和脚本的问题?

scripting - 以批处理脚本将文件夹从一个目录移动到另一个目录

ios - Xcode Watchkit : None of the valid provisioning profiles allowed the specified entitlements: beta-reports-active, com.apple.security.application-groups

ios - UIActivityViewController 为 Twitter 和 Facebook 发布不同的图像和文本

objective-c - 在 NSPopover 中运行 NSAlert

macos - 带 Storyboard的基于文档的应用程序自动保存