gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:17 24 4
gpt4 key购买 nike

我一直在用这个把头撞在墙上。我正在尝试使用脚本桥在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com