Finding highlighted text in Pages using Applescript

Hi guys, I'm writing a script that will parse through a Pages document and retun me the indices of the first and last characters of each block of text which is highlighted with a given colour. I do have a script that works, the only problem is it runs very slowly.
Here's what I have at the moment:
tell application "Pages"
          tell front document
  --colour to test for in RGB (0 to 65535)
                    set testColour to {65535, 0, 65535}
                    set lastCharColour to missing value
                    set N to (count of characters)
                    set indexArray to {}
                    repeat with i from 1 to N
                              set thisCharColour to character background color of character i
  --add the character if it is highlighted
  --and the previous one wasn't
                              if thisCharColour is testColour and lastCharColour is not testColour then
                                        set end of indexArray to i
  --or add the previous one if this one is
  --not highlighted and the previous one was
                              else if thisCharColour is not testColour and lastCharColour is testColour then
                                        set end of indexArray to (i - 1)
                              end if
  --update the previous character's colour
                              set lastCharColour to thisCharColour
                    end repeat
  --if there are an odd number in the list,
  --the document must end with a highlight,
  --so we'll add the last character
                    if ((count of indexArray) mod 2) = 1 then
                              set end of indexArray to N
                    end if
                    get indexArray
          end tell
end tell
I'm parsing over every character in the document, and adding the index to a list if its background has changed colour either to or from the specified colour. It seems to be that the "get character background color of character i" call inside the loop is what's taking a long time. I have tried caching off a list of all the colours as a first step and then just comparing against an item in that list, but it's not significantly faster (in fact it might even be slower, I aborted it because it was taking too long).
I know in MS Word it is possible to do a find operation to find characters which are highlighted in a particular colour, so I was sondering if it was possible to do something similar in Pages, or can anybody think of a way to streamline my logic here?
Thanks,
Andy

However, I've just realised, my reason for getting the character indices was so as I could, in a subsequent step, get the characters themselves. I can modify your script to get the actual characters as well as the character offsets, and then where there's a break in the character offsets I can split the characters into substrings, as below:
set theBackgroundColor to {0, 65535, 0}
getCharIndex(theBackgroundColor)
on getCharIndex(bgColor)
          script o
                    property myList : {}
                    property charList : {}
          end script
          tell application "Pages" to tell front document
                    set o's myList to character offset of (characters whose its character background color is bgColor)
                    set o's charList to (characters whose its character background color is bgColor)
          end tell
          set tc to count o's myList
          if tc = 0 then return {}
          set i to 1
          set blockArray to {}
          set lastBlockEnd to 0
          repeat while i < tc
                    set N to item i of o's myList
                    repeat with J from (i + 1) to tc
                              set N to N + 1
                              set n1 to item J of o's myList
                              if N is not n1 then
                                        set end of blockArray to ((items i thru (J - 1) of o's charList) as text)
                                        set lastBlockEnd to (J - 1)
                                        exit repeat
                              end if
                    end repeat
                    set i to J
          end repeat
          set end of blockArray to ((items (lastBlockEnd + 1) thru tc of o's charList) as text)
  return blockArray
end getCharIndex
This has all your speed advantages, and still gets me the end result I'm after.

Similar Messages

  • Highlighting Text In Pages 4 and 5: How To/Best Methods?

    Hello Fellow Macers:
    I need advice on the best way to highlight text in Pages 4 and 5:
    I use and prefer Pages 4.3. Yes, I have Pages 5.2, and I find that using it good for some things (dictating into a document), but not so good for others - highlighting text.
    For example, I read tons of documents and like to make notes, comments and especially highlight text according to a color-code scheme that I have used for years.
    For those who use Word, this process of highlighting text is pretty straight-forward:
    You highlight your text and then simply click your highlight box on the Word toolbar (choose a default color, usually yellow) and you're done. If you need to use another color, just use the pull-down, as seen above. The drawback in Word - no Orange, or sectrum option.
    But let's face it - Word as a program is just too much. As someone who has used every word processing program under the sun, I find Pages excellent. It does what I need.
    However, I find highlighting text in Pages clumsy. Here's what I need to do to highlight text in Pages 4.3:
    Go to your toolbar, and then choose a highlight/background color. It's more labor intensive then Word, above, but you do have the option of more colors.
    Then there's this method, where basically you get to simply use yellow as your color:
    IMHO both are clumsy methods compared to Word, but have become even more so in Pages 5.2....
    Lots of clicks to achieve simple color highlights.
    Am I missing something, or is there a simpler way, in either Pages 4 or Pages 5, to achieve the above results???
    Any suggestions appreciated!

    Dear VikingOSX:
    As Ed MacMahon once said (before your time?) re Pages 5 Character Fill Color: "You are correct, sir!!"
    The gear icon IS where the function that I'm talking about is located. Still, it's annoying.
    Q: Why?
    A: Because from a work-flow perspective as someone who is highlighting it makes no sense. We in the Western world read and write from top to bottom, left to right. Making a mark on a doc should follow the same flow.
    You should not have to keep looking over to the right side of the doc to find your function. Logically it should be at the top (e.g., toolbar), on the left, or perhaps some contextual pop-up option.
    Furthermore, it's more "labor intensive" from this point of view: In Word, once you set the color (which, granted, you may need to change from time to time IF you, like me, use multiple colors as part of your highlighting workflow), you just click the box after highlighting the text - 1 click vs 2, and possibly 3.
    The "richer color" part of my comment was just to note that a flaw, in my view, of Word's basic scheme is that:
    1) It lacks an Orange. And we've all used an Orange highlighter at somepoint, right? Or perhaps, you were like me in High School and threw them instead. But you get the point...and
    2) Having a wider pallatte available as an option - which you do in Apple's scheme - is nice. I use color coding/highlighting to make notes and save time. The colors tell me what's important, what's next important, key terms, etc. Also, for example, if I wanted to outline my highlighting notes, the color scheme (which has a heirarchy based on color, right?) allows me to do this quickly because it's already there.
    As for the Dictation function working on Pages '09/4.3 - again, you are correct, sir! And that's great to know. I had tried to use it before when the new Pages 5 (hitting fn twice on Pages 4.3) but it didn't activate. I tried it after reading your response and Bingo! It's Alive! So thanks for that tip.
    Your keyboard shortcut scheme is an interesting idea, and I may try it as an experiment. My intuition is that it will not work as straight-forwardly as Word's toolbar pull-down will, but I might be wrong. We'll see.
    And yes, I do use Pages '09 (4.3) instead of Pages 5 for exactly this reason (see my discussion)
    Great reply! Thanks!

  • How do I highlight text in Pages?

    How do I highlight text in Pages - none of the menus, nor the 'Help' function seems to help...

    It's tough searching for something when you can't guess the terms to use. It's called a "background color." You can choose a color from the format bar.

  • How do you highlight text in Pages for iPad? The document I want to create and highlight is a pages document

    I am trying to create a document in Pages for iPad and want to highlight various bits of text but can not find a function in the inspector for this. Can anyone help,please?

    Hi stuartdj,
    I'm not finding a way to highlight text as one can do with iBooks (hold and swipe for black text, background yellow for example).
    One option is to actually color the text itself.
    Select the text, then in Style tools, tap the Font name at the top which shows the Text Options sheet where there is a Color option.  Would changing the font color to Red, Green or Purple accomplish your goals?
    If you do want black text on a colored background (highlighted) it would need to be text in a text box where you can change the fill (background) color.  Style, Style Options, Fill.  But this is not inline text and doesn't flow like just coloring the font itself.
    ivan

  • How do you highlight text in Pages v5?

    Hi guys,
    Was struggling a bit with the recent changes from Pages 5 and was wondering how we could highlight text (using different colours etc)?
    Thanks !

    Your question is answered in the following post:
    https://discussions.apple.com/message/23519269#23519269

  • Why can i not highlight text in pages in different colours?

    Dear Apple Dev.
    Why is there;
    a. no easy way to highlight text,
    b. no easy way to chage the colour or the highlighting?
    APPLE PLEASE ADD THIS MOST BASIC USEFUL FEATURE THAT MS WORD - and I do hate using ms word - and use pages at every opportunity, has been able to do for probably going on 20 years...
    Huge fan. need the basic work functionality.
    Sincerely,
    Frustrated.

    bryce_1 wrote:
    NOTE: you can highight an entire paragraph. not one word, or one sentence. infrastructure is there. but not functionality.
    Are you sure?
    Peter

  • Highlighting text in pages

    I used to have a "a" icon available to back highlight text.  Recently, it disappeared.  The only highlighting alternative I get is "none".  What can I do to fix this?

    Use the button in the format bar as in this screenshot.

  • How do I find highlighted text, one-by-one, in a document?

    Hi,
    I have highlighted many lines, texts at lot of places in a document.
    I just want to "find" the hightlighted text one by one (like a find-next) so that I can just view the important information immediately.
    Till now I have been scrolling and looking for highlighted text, which is definitely not a good way to do in a medium-large sized document.
    Thanks
    Saurabh

    Open the Comment pane on the right. There's a list of all comments there.
    If you click on one of them, the display will "jump" to it.
    On Wed, Feb 27, 2013 at 11:14 AM, saurabhagarwal_2000 <

  • Hot do you highlight text in Pages?

    I am trying to highlight specific text in Pages, like one word or one line. I found how to highlight the entire paragraph, but I only want to be able to highlight one line or a couple of words at a time. Is there a way to highlight specific text in Pages instead of the entire paragraph? Thanks
    Macbook Pro 15"   Mac OS X (10.4.9)  
    Macbook Pro 15"   Mac OS X (10.4.9)  

    Not exactly sure if it's what you want but you could change the text colour by double-clicking the word, opening the colour palette then crayons & selecting the colour required.
    Or leave the text black, click on Inspector, click on Graphic Inspector, click shadow & choose colour (e.g red), degrees 350, offset 0%, blur 1pt, opacity 100%. This gives black text with a red background. Adjust these settings to suit.
    Steve

  • Replace text with image using Applescript in InDesign CS5

    Hi to everyone, i'm looking for some suggestions to resolve my problem.
    I've to replace some strings with jpeg images stored on my pc
    Here is the code to replace two strings with the new ones.
    tell application "Adobe InDesign CS5"
              set myDocument to active document
              set myPage to page 1 of myDocument
              set stringsToReplace to {"111", "222"}
      repeat with iterator from 1 to (count stringsToReplace)
           set find text preferences to nothing
           set change text preferences to nothing
           set myFoundItems to nothing
           set element to item iterator of stringsToReplace
          if element is "111" then
               set find what of find text preferences to "111"
               set change to of change text preferences to "ONE"
               set myFoundItems to change text
               display dialog ("Found : " & (count myFoundItems) & " occurences of " & element)
          else if element is "222" then
               set find what of find text preferences to "222"
               set change to of change text preferences to "TWO"
               set myFoundItems to change text
               display dialog ("Found : " & (count myFoundItems) & " occurences of " & element)
          end if
              end repeat
      set find text preferences to nothing
      set change text preferences to nothing
    end tell
    Can you hel me?
    Thanks in advance.

    Hello, I have a couple of questions for you… How come you have strings in text frames… Would you not be better off using labels for this…? ( thats how I would do this ). Off the top of my head I think you will need to remove any text content from the frame to be able to change it's content type… only then will you be able to place a graphic… How are you associating your strings with the required image files… Do you have this in some extenal file Excel or FMP.

  • Highlighting text in JEditorPane using HTMLEditorPane

    I am using JEditorPane extending HTMLEditorkit. In the HTMLEditorkit when we use the span tag for highlighting the text of a line, it is just not showing it. It starts the color and ends it there and then. It looks like a small dot and doesnot spans the whole line.
    Is this a bug or something in JAVA 1.3.1. If anyone of you knows how to do it please let me know.
    I need it for a project which has a deadline.
    Thanks.

    JEditorPane is not rendering and HTMLDocument.HTMLReader is not reading the SPAN tag. In other words, the SPAN tag is not supported in Java. You can still use it by overriding the mentioned classes. My application SimplyHTML gives an example of how this can be done. Please visit http://www.lightdev.com/dev/sh.htm
    Ulrich

  • How do I highlight text on pages 5.5.2?

    Im trying to highlight specific parts of an essay question, however I cannot find an option to do so. Somebody please send help.

    The ability to place a true highlight behind text is on the Advanced Options pop over, not in the Insert menu as the Highlight menu item. You can apply this text background fill color by either clicking on the color well color itself, or by clicking the rainbow color chooser button adjacent.

  • Highlighting text in Pages for ipad?

    Is it coming or did I just waste 10 bucks? I need to be able to highlight corrections on students assignments... Changing font colors is used for one type of correction, and highlighting is for another type of grammatical correction.

    Since  no one seems to be jumping in with any suggestions here, I am going to make a few comments.
    First, I don't have Pages for iOS, because I only have an iPod Touch, and I wouldn't want to try editing a Page document on that. So I can't test anything.
    Since Pages for Mac supports highlighting as a character style, it is quite possible that Pages for iOS also supports it, but lacks a way to create it. If that is the case, you can create a document on a Mac using the highlighting styles you want, import that document to your iPad, and copy the style from that document to apply to the corrections in the student assignments.

  • Dynamic Text in Pages using Numbers file as input

    I was wondering if it is possible to have cells or text boxes in Pages that are dynamic and are filled
    in automatically using a source cell from a linked Numbers file as the input.
    I have proposals to write that always use a specific Numbers file to crunch the math, but I want
    to present certain parts of the Numbers file in a formal written proposal and don't want to redo
    everything by hand when I make small changes to the projects financials.
    Thanks!!! So glad I switched!

    There is no feature allowing us to link two different documents.
    You may try to achieve your goal creating the Text document in a Numbers sheet using text boxes.
    Yvan KOENIG (from FRANCE vendredi 19 décembre 2008 12:41:29)

  • Is it possible to highlight text in Pages

    I have a document that I am working on with a friend. I would like to be able to direct his attention to particular words or phrases by highlighting them, e.g., by placing them in a yellow background.
    Is this possible in Pages? If so, how do I do it?
    Thanks,
    Eric WEir

    Thanks, Walt. I was aware that they can be hidden, but for what I'm working on -- a document that's been under constant revision for a while -- I find it helpful occasionally to the document with the comments.
    But thanks just the same.
    Sincerely,
    Eric Weir

Maybe you are looking for

  • Intel Mac Mini, Firefox and Java

    I'm running an Intel Mac Mini using Firefox; mostly it's fine, but sometimes it behaves as if it and Java Applets have had a falling out. I have made sure that I have the very latest version of JRE to go with OSX, but when I go to http://www.f1.com a

  • Arch Linux as a server: advantages and disadvantages?

    Hello, I am about to create a hosting server for a great number of websites. Since i never actually did it myself it will be relatively new to me. For that reason I want to use Arch distro because I like it, understand it and have most experience wit

  • Field Symbol not assign

    hi experts, can you help me on this on. data: dref type ref to data,                      drefc type ref to data. field-symbols: <itab type standard table,                         <ctab> type standard table. create data: dref type standard table of (

  • [SOLVED] Mouse buttons stopped working

    I have a Logitech Marble Mouse (connected via a USB to PS/2 adapter).  It has no scrollwheel, so I set up the small "extra" pair of buttons to toggle "scroll mode" in certain applications (like Firefox & Chrome).  When activated, moving the trackball

  • Which app of mp3 songs can work in iphone4s!

    kindly tell me which app of mp3 songs can work in iphone4s