2

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?

Frog
  • 1,631
  • 2
  • 17
  • 26
  • 1
    I can tell why your code crashes: you cannot cast `word` to `NSString *` because it’s not a string; use `[[word properties] objectForKey:@"contents"]` instead. Also, you should always specify a literal string as the first argument to `NSLog()`; for example: `NSLog(@"%@", [[word properties] objectForKey:@"contents"]);` and `NSLog(@"%lu words", [words count]);`. I don’t know how to replace words, though. –  Oct 16 '11 at 09:55
  • @Bavarious: Thanks! I couldn't find that code `[[word properties] objectForKey:@"contents"]);` anywhere. Where did you find that? – Frog Oct 16 '11 at 09:59
  • Since Pages doesn’t export `contents` as an Objective-C property of `PagesWord`, I printed out the entire `properties` dictionary: `NSLog(@"word properties: %@", [word properties]);` and saw that the dictionary contained the word itself under the key `contents`. Hopefully someone will chime in and answer how to replace words. –  Oct 16 '11 at 10:00
  • Mmm, okay. That's really smart, I will remember that. Isn't it true that, since all words are references to the actual words according to the Scripting Bridge documentation, I can just modify the word? – Frog Oct 16 '11 at 10:04
  • @Bavarious: Oh wait, I of course can't do that because it's a NSDictionary and not a NSMutableDictionary... – Frog Oct 16 '11 at 10:08

1 Answers1

3

To replace the word i use:

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

[word select];

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

Stéphane
  • 46
  • 1
  • 1
  • Wow, thanks a lot! I already gave up on this, but if this works it will really help me. Thanks again! – Frog Oct 19 '11 at 19:56
  • Do you happen to know how I can select multiple objects? I need to select multiple characters, but when I select a new one the old selection is erased. – Frog Oct 19 '11 at 20:52