7 Replies Latest reply on Aug 22, 2008 9:25 AM by (vkramji)

    Get Text with Applied Style

    Level 1
      Hi all,

      I am using InDesign CS3 on OS 10.5. I'm trying to write something in Applescript that can look at a document and give me the text that has a certain applied style.

      For example, if a document contained the text "Hello, world" and had an applied paragraph style called "Greeting," then what I want is to use my script to tell that document something like this:

      tell application "Adobe Indesign CS3"
      set theDoc to document 1
      set theText to get every paragraph of theDoc whose applied paragraph style is "Greeting"
      end tell
      theText

      -->should return: "Hello, world"

      Obviously, I tried that script, but it doesn't work. I get the error "Adobe InDesign CS3 got an error: Cant get every paragraph of document "MyDocument.indd" whose applied paragraph style = "Greeting"."

      I'm not sure if I am just using the wrong commands or what. Any help is greatly appreciated!
        • 1. Re: Get Text with Applied Style
          Level 1
          Hi Hayden,

          The trouble is, theDoc doesn't have paragraphs. If you open the InDesign dictionary and look at the document object, you'll see text frames, and you'll see stories, but no paragraphs. To get all of the paragraphs, you'd use something like this:

          set theText to every paragraph of every story

          But getting every paragraph of every story whose applied paragraph style is a specific style is harder to do using a filter ("whose") clause, because it has multiple conditions. Shane can probably do it, but I'm not having much luck. All the same, why bother? InDesign has a perfectly good find text method that will do what you want. Something like this:

          tell application "Adobe InDesign CS3"
          --Clear the find text preferences
          set find text preferences to nothing
          set applied paragraph style of find text preferences to paragraph style "Greeting" of document 1
          tell document 1
          set myFoundItems to find text
          end tell
          end tell

          Thanks,

          Ole
          • 2. Re: Get Text with Applied Style
            Level 1
            Ole,

            Truly, thou art a juggernaut of scripting knowledge! That did exactly what I wanted. I just tweaked it a bit so that myFoundItems would return as a string and it gave me the text I wanted.

            I knew I was not doing something right. I didn't realize that InDesign's find text feature would do what I wanted. This forum has been so helpful! Thanks!!
            • 3. Re: Get Text with Applied Style
              sstanleyau Level 4
              As Ole says, using find is usually a more sensible approach for this. But<br />for the record, this should work:<br /><br />tell document 1<br />set theStyle to paragraph style "Greetings"<br />set theText to every paragraph of every story whose applied paragraph style<br />is theStyle <br /><br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
              • 4. Re: Get Text with Applied Style
                Level 1
                Hi Shane,

                I tried that, actually, and doesn't work for me.

                Thanks,

                Ole
                • 5. Re: Get Text with Applied Style
                  sstanleyau Level 4
                  On 14/8/08 8:33 AM, "Olav Kvern" <member@adobeforums.com> wrote:<br /><br />> I tried that, actually, and doesn't work for me.<br /><br />Odd. I just tested it here and it works fine for me. Did you use a reference<br />to the style or just its name? That makes a difference.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
                  • 6. Re: Get Text with Applied Style
                    Level 1
                    Hi fellas,

                    I have another question. How would the syntax change if I was doing this in CS instead of CS3? I used what Ole wrote and I changed the syntax for CS, but I keep getting the error "InDesign CS got an error: Can't get find text of document 1." The words "find text" are then highlighted in the script.

                    Any ideas? Thanks!
                    • 7. Re: Get Text with Applied Style
                      Hi I am also looking for the same type of example. i am using IDCS3, windows and VB. i have some text with specified character format. i need to get the text with specified character format. please help to write a script to get the text. if you could help me for this it will e great. thanks.