12 Replies Latest reply on Apr 28, 2009 11:20 PM by sstanleyau

    [JS or AS] Applying Paragraph Styles.

    marcusstringer Level 3

      Hi,

      I have a JavaScript From Dave Saunders. Which Takes a list of all Para and Character styles from an InDesign File and make a .txt file.

      ParaList.png

       

      Which works great.

       

      So because I don't understand JavaScript and Very Basic Applescript.

       

      I have made a basic AppleScript which Creates a Document and place the .txt file which the JavaScript created.

       

      I know how to apply a Paragraph Style in AppleScript using the

       

       

      set applied paragraph style of paragraph 1 to ParaName

       

       

      My problem is I don't know how I can match and apply Paragraphs Styles and Character styles from the list generated from the .txt file to the corresponding Paragraph and Character styles in InDesign?

      Picture 2.png

       

      Obviously if it could be all done Via JavaScript then yeah, otherwise AppeScript all do as well.

       

      Any help or ideas would be great.

        • 1. Re: [JS or AS] Applying Paragraph Styles.
          marcusstringer Level 3

          *Bump*

           

          Can anyone shed some light on this for me please

          • 2. Re: [JS or AS] Applying Paragraph Styles.
            Eric @ MCA Level 3

            I've not really experimented with how to get the correct story, but once you get it, this should do the trick.

            tell application "Adobe InDesign CS3"
              tell active document
                repeat with thisParagraph in every paragraph of story 2
                  set thisStyleName to contents of thisParagraph
                  if last character of thisStyleName = return then
                    set thisStyleName to (characters 1 thru -2 of thisStyleName) as text
                  end if
                  set applied paragraph style of thisParagraph to paragraph style thisStyleName
                end repeat
              end tell
            end tell
            • 3. Re: [JS or AS] Applying Paragraph Styles.
              Dave Saunders Level 4

              You need to extract the contents of the paragraphs and trim off the gunk you don't want (return at end and possibly tabs at the beginning -- I don't remember this script.

               

              Something like:

               

              myStory.paragraphs[j].appliedParagraphStyle = myStory.paragraphs[j].contents.slice(0,-1);

               

              if all you have to do is lose the return.

               

              Is that enough to get you under way?

               

              Dave

              • 4. Re: [JS or AS] Applying Paragraph Styles.
                marcusstringer Level 3

                Thanks guys I'll give it a crack and report back.

                 

                BTW Dave I got your script from here

                 

                http://jsid.blogspot.com/2005/08/script-of-day-list-document-styles.html

                • 5. Re: [JS or AS] Applying Paragraph Styles.
                  marcusstringer Level 3

                  So basicly, what I'm trying to do is get the names from the list of paragraph and character styles generated from the .txt file the javascript made.

                  then apply the same styles from the Paragraph Style Pallet to the corasponding names in the .txt file list

                   

                   

                  So this is what I have, which doesn't work, it falls over at getting the "contents" of the first para of myStory.


                  tell application "Adobe InDesign CS3"

                       make new document

                       tell page 1 of document 1

                            make new text frame with properties {geometric bounds:{1, 1, 275, 210}}

                            -- this line below will let me choose which file to place, in this case it's called MyDocumentStyles.txt which the Javascript made

                            set myStory to place (choose file) place point {1, 1} with autoflowing without showing options

                            set thisStyleName to contents of paragraph 1 of myStory

                            if last character of thisStyleName = return then

                                 set thisStyleName to (characters 1 thru -1 of thisStyleName) as text

                            end if

                            set applied paragraph style of thisParagraph to paragraph style thisStyleName

                            

                       end tell

                  end tell

                   

                   

                  • 6. Re: [JS or AS] Applying Paragraph Styles.
                    sstanleyau Level 4

                    You don't set myStory to anything, nor do you set thisParagraph to anything. You also make a frame, but then don't place the story on it. And last, you're not looping through the paragraphs. Try something like this:

                     

                    tell application "Adobe InDesign CS3"

                    make new document

                    tell page 1 of document 1

                    set newFrame to make new text frame with properties {geometric bounds:{1, 1, 275, 210}}

                    -- this line below will let me choose which file to place, in this case it's called MyDocumentStyles.txt which the Javascript made

                    place (choose file) on newFrame

                    set myStory to parent story of newFrame

                    repeat with i from 1 to count of paragraphs of myStory

                    set thisStyleName to contents of paragraph i of myStory

                    if last character of thisStyleName = return then

                    set thisStyleName to text 1 thru -2 of thisStyleName

                    end if

                    set applied paragraph style of paragraph i of myStory to paragraph style thisStyleName

                    end repeat

                    end tell

                    end tell

                    • 7. Re: [JS or AS] Applying Paragraph Styles.
                      marcusstringer Level 3

                      Thanks Shane

                       

                      Oh I didn't think I had to set myStory to anything as it's the only story, so I thought it's would automacticly make it the parent story...learn something new...

                       

                      I also forgot to import the Paragraph Style into the new document which I've fixed,

                      but It falls over at the set applied paragraph line.

                       

                      tell application "Adobe InDesign CS3"

                          

                           --Pick file to import styles from

                           set myFilePath to (choose file)

                           --Create a new document.

                           set mydoc to make document

                           tell mydoc

                                import styles format text styles format from myFilePath

                           end tell

                           tell page 1 of document 1

                                set newFrame to make new text frame with properties {geometric bounds:{1, 1, 275, 210}}

                                -- this line below will let me choose which file to place, in this case it's called MyDocumentStyles.txt which the Javascript made

                                place (choose file) on newFrame

                                set myStory to parent story of newFrame

                                repeat with i from 1 to count of paragraphs of myStory

                                     set thisStyleName to contents of paragraph i of myStory

                                     if last character of thisStyleName = return then

                                          set thisStyleName to text 1 thru -2 of thisStyleName

                                     end if

                                     set applied paragraph style of paragraph i of myStory to paragraph style thisStyleName

                                end repeat

                           end tell

                      end tell

                       

                      • 8. Re: [JS or AS] Applying Paragraph Styles.
                        sstanleyau Level 4

                        Try hanging "tell page 1 of document 1" to simply "tell document 1".

                        • 9. Re: [JS or AS] Applying Paragraph Styles.
                          marcusstringer Level 3

                          Fantastic that worked wonders (although it fell over when it got to the Character Styles, but I'm hopeful I can work that bit out I'm think of replicating the Para section)

                           

                          Anyway thanks Shane, Dave and Eric.

                           

                          P.S. Shane, I'm wondering why having the "tell page 1" would have made a difference? because it's referencing the same thing?

                          • 10. Re: [JS or AS] Applying Paragraph Styles.
                            sstanleyau Level 4

                            > I'm wondering why having the "tell page 1" would have made a difference

                             

                            It's to do with the paragraph style. As it stood, the script was looking for "paragraph style <whatever> of page 1 of document 1", and pages don't have paragraph styles, documents do.

                            • 11. Re: [JS or AS] Applying Paragraph Styles.
                              marcusstringer Level 3

                              Oh...That makes sense.

                               

                              That Flow chart on page 58 of the InDesignCS3_ScriptingGuide_AS.pdf is very confusing then.

                               

                              Also the Script of importing Paragraph Styles on page 74 also doesn't work until you remove all that "Global" Mumbo Gumbo

                              • 12. Re: [JS or AS] Applying Paragraph Styles.
                                sstanleyau Level 4

                                The problem only occurs if you refer to a style by name. The "safest" way is to use references to styles:

                                 

                                tell document 1

                                set someStyle to paragraph style someName

                                ...

                                 

                                Then you can use the reference pretty much anywhere:

                                 

                                tell page 1 of document 1

                                set applied paragraph style of parent story of text frame 1 to someStyle

                                ...