Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6529431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:38:04+00:00 2026-05-25T09:38:04+00:00

I am trying to count the words in Pages document (the doc format is

  • 0

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) wordCountForApp: (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….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:38:04+00:00Added an answer on May 25, 2026 at 9:38 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to modify an existing Pages document from within my application using the
I'm trying to count how many distinct value of FLOOR there is but I
I'm trying to count all the values inside an array, I've tried using count()
I am trying to count characters in comments included in C code using Python
I'm trying to count # of lines of a pre element and I'm using
I am trying to figure out a way to count a words in a
Trying to count how many elements within the array are not equal to 0,
I'm trying to count the number of records that have a null DateTime value.
I am just trying to count the number of white spaces to the LEFT
I am trying to count the number of rows whose date has not yet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.