开发者

Scripting Bridge and com.apple.iWork.Pages -- can this work?

开发者 https://www.devze.com 2023-04-01 09:39 出处:网络
I am trying to count the words in Pages document (the doc format is RTF) using the scripting bridge.(I can do it using NSApplescript but I would rather not have all the applescript-aware duct tape in

I am trying to count the words in Pages document (the doc format is RTF) using the scripting bridge. (I can do it using NSApplescript but I would rather not have all the applescript-aware duct tape in my code) When I perform this task using applescript (and NSAppleScript APIs) I can do this very simply (and successfully):

on wordCount(appName,docName)
local mydoc
local wordcount
tell application appName
    set mydoc to document docName
    set wordcount to count of words of mydoc
    log "wordcount = " & wordcount
    return wordcount
end tell
end wordCount

However when I try the equivalent using the scripting bridge all my objects seem to have null contents. My code is as follows:

+ (NSUInteger) wor开发者_如何学PythondCountForApp: (SBApplication*) sbApp docNamed: (NSString*) docName
{
    PagesApplication *pages = (PagesApplication*)sbApp; 
    PagesDocument *doc = [[pages documents] objectWithName:docName];
    PagesText *text = [doc bodyText];
    SBElementArray *words = [text words];
    NSUInteger wc = [words count];
    NSLog(@"Pages word count = %ul", (unsigned int) wc);
    return wc; // wc comes back as zero always ... grrrr
}   

I have verified that I am running this stuff on the main thread (and that equivalent code works against TextEdit). Any ideas as to what is going on/how to work around?

Thanks for having read this far....


That code works for me. The equivalent in F-Script:

> pages := SBApplication applicationWithBundleIdentifier: 'com.apple.iWork.Pages'

> (pages documents objectWithName: 'fun') bodyText words count
303

I'd suggest you step through it with a debugger and make sure that each object is what you're expecting, i.e. pages, [pages documents], etc.

If you're writing this code for external use, ideally you should not refer to documents by name; the user may have multiple documents with the same name open.

Another option is objc-appscript, which provides an neat ASTranslate utility that transforms AppleScript into Objective-C (or Ruby or Python for the other appscript bindings). For example, for the above, you'd have something like:

#import "PGGlue/PGGlue.h"
PGApplication *pages = [PGApplication applicationWithName: @"Pages"];
PGReference *ref = [[pages documents] byName: @"fun"];
PGCountCommand *cmd = [[ref count] each: [PGConstant word]];
id result = [cmd send];


So the problem was that I was asking for a document named "something.rtf" that I had just opened in Pages. But when Pages opens "something.rtf" it names it "something". And then when you ask for document named "something.rtf" it does not return nil because the document does not exist by that name. Instead, it returns a PagesDocument named "something.rtf" which has no valid contents: a NIL document. Which I guess I just was too stupid to recognize when I submitted to SO.

I have since checked other apps and this seems to be normal applescript behavior when you ask for a document by name (to get back a "valid object" containing a NIL document) oh well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号