Search code examples
objective-cxcodensstringscripting-bridge

Scripting Bridge objective-c PagesWord


I'm trying to modify an existing Pages document from within my application using the Scripting Bridge. I've followed all steps mentioned in the documentation: I created a header file and thoroughly examined it, but I just can't figure out how how to do this.

I'm basically trying to do a search an replace: I've got a list of strings and I want to replace some search values with those strings. The problem is that I just can't figure out how the PagesWord class works. I just want to get a string from it and it compare it to my search value. I currently have to following code:

PagesApplication *pages = [SBApplication applicationWithBundleIdentifier:@"com.apple.iWork.Pages"];
PagesDocument *document = [pages open:inputURL];
PagesText *bodyText = [document bodyText];
SBElementArray *words = [bodyText words];

NSLog([NSString stringWithFormat:@"%d words.", [words count]]);

for (PagesWord *word in [bodyText words]) {
    NSLog((NSString *)word);
}

Everything works well until the last 3 rows: the correct Pages document is opened and the word count is logged, but the string isn't: I just see exception messages. I also tried to work with the properties of PagesWord, but I have the same problems then...

Can anyone help me?


Solution

  • To replace the word i use:

    for (PagesWord *word in [bodyText words]) {

    [word select];

    [[document selection] setTo:@"my new value"];}