14 Replies Latest reply on Jul 28, 2010 7:35 AM by r_olsen

    [AS] Iterate over page names throws exception

    r_olsen

      Cheers,

       

      Something's wrong with my AS export script. Looping over pages and getting the name

      of current page will return "1" for page[0] but the exception for the following pages:

       

      Can't make name of item 2 of {«class page» id 179 [snip] of "test.indd" of application

      "Adobe InDesign CS3"} into type string

       

      Can you help the AS rookie?

       

       

      (* 
           Export all open documents to single PDF documents.
           v1.0 07/2010
           Rasmus Olsen
           
      *)
      -- choose a folder and cast to string
      set myFolder to (choose folder) as string
      -- set timeout to 10 minutes
      with timeout of (10 * 60) seconds
           tell application "Adobe InDesign CS3"
                try
                     -- get PDF export presets as strings
                     set presets to name of PDF export presets
                     -- get selected list item as string
                     set selected to {choose from list presets} as string
                     -- iterate over documents
                     repeat with doc in documents
                          set docname to name of doc
                          -- remove extension via newname
                          set newname to text 1 thru -6 of docname
                          if (count of pages of doc) > 1 then
                               -- doc's got more than one page
                               set docpages to pages of doc
                               -- iterate over pages
                               repeat with aPage in docpages
                                    -- need help with this:
                                    -- get page name as string trows an exception with all pages except page 1
                                    set pagename to name of aPage as string
                                    display dialog pagename
                                    tell doc
                                         -- export page with pagename suffix
                                         export format "Adobe PDF" to (myFolder & newname & "_" & pagename & ".pdf") using selected without showing options
                                         close saving no
                                    end tell
                               end repeat
                          else
                               tell doc
                                    -- export page
                                    export format "Adobe PDF" to (myFolder & newname & ".pdf") using selected without showing options
                                    close saving no
                               end tell
                          end if
                     end repeat
                     -- catch..
                on error e
                     display dialog e
                end try
           end tell
      end timeout
      
        • 1. Re: [AS] Iterate over page names throws exception
          sstanleyau Level 4

          It may need some parens, as in:

           

                                        set pagename to (name of aPage) as string

          although the "as string" is redundant. But you can simplify your loop a bit, by using something like:
          repeat with i from 1 to count of pages
          set pagename to name of page i
          ...

          • 2. Re: [AS] Iterate over page names throws exception
            r_olsen Level 1

            Hey Stanley,

             

            The () did the trick - thanks!

             

            Rasmus

            • 3. Re: [AS] Iterate over page names throws exception
              r_olsen Level 1

              sstanleyau wrote:

               

              But you can simplify your loop a bit, by using something like:
              repeat with i from 1 to count of pages
              set pagename to name of page i
              ...

               

              repeat with i from 1 to count (pages of doc)
                   set pagename to name of page i
                   display dialog pagename
              end repeat 
              

               

              Adobe InDesign CS3 got an error: Can't get name of page 1.

               

              Like that?

               

              Rasmus

              • 4. Re: [AS] Iterate over page names throws exception
                sstanleyau Level 4

                More like:

                 

                set pagename to name of page i of doc

                • 5. Re: [AS] Iterate over page names throws exception
                  r_olsen Level 1

                  Hi Stanley, something wierd happens. Using your code I can get the dialog

                  to display the different page names, but when trying to export I'm struck

                  by the error:

                   

                  Adobe InDesign CS3 got an error: Can't get name of page 2 of item 1 of every document

                   

                  repeat with i from 1 to count (pages of doc)
                       set pagename to (name of page i of doc)
                       display dialog pagename
                       tell PDF export preferences
                            set page range to pagename
                            set view PDF to false
                       end tell
                       -- outcomment the following tell block to display all page names
                       tell doc
                            -- export page with pagename suffix
                            export format "Adobe PDF" to (myFolder & newname & "_" & pagename & ".pdf") using selected without showing options
                            close saving no
                       end tell
                  end repeat
                  

                   

                   

                  UPDATE:

                  Problem solved - I'm closing the document after the first export iteration. No wonder the page name is unavailable.. Stupid stupid

                   

                   

                   

                  Message was edited by: r_olsen

                  • 6. Re: [AS] Iterate over page names throws exception
                    r_olsen Level 1

                    Hey Stanley, one last problem to overcome: close multiple documents

                     

                    tell application "Adobe InDesign CS3"
                         repeat with i from 1 to (count documents)
                              close document i saving no
                         end repeat
                    end tell
                    

                     

                    Running this with two open documents sparks the error:

                    Adobe InDesign CS3 got an error: Can't get document 2.

                     

                    I've tried solving this be using a negative increment:

                     

                     

                    tell application "Adobe InDesign CS3"
                         repeat with i from -1 to (count documents)
                              close document i saving no
                         end repeat
                    end tell

                     

                    this however reverses the problem:

                    Adobe InDesign CS3 got an error: Can't get document 0.

                     

                    How would you do this?

                     

                    Thanks,

                    Rasmus

                    • 7. Re: [AS] Iterate over page names throws exception
                      Muppet Mark-QAl63s Level 4

                      You can NOT have 'document 0' AppleScript is NOT zero based. So…

                       

                      tell application "Adobe InDesign CS2"

                      repeat with i from (count documents) to 1

                      close document i saving no

                      end repeat

                      end tell

                       

                      or use the 'by' parameter in your loop positive or negative number. Without the 'by' a value of 1 is assumed…

                      • 8. Re: [AS] Iterate over page names throws exception
                        sstanleyau Level 4

                        You could just close document 1 every time. In fact, you can probably use:

                         

                        close every document saving no

                        1 person found this helpful
                        • 9. Re: [AS] Iterate over page names throws exception
                          r_olsen Level 1
                          function(){return A.apply(null,[this].concat($A(arguments)))}

                          Muppet Mark wrote:

                           

                          You can NOT have 'document 0' AppleScript is NOT zero based. So…

                           

                           

                          tell application "Adobe InDesign CS2"

                          repeat with i from (count documents) to 1

                          close document i saving no

                          end repeat

                          end tell

                           

                           

                          or use the 'by' parameter in your loop positive or negative number. Without the 'by' a value of 1 is assumed…

                           

                          Mr. Muppet - nothing happens when I run your lines?

                          I use CS3 - dunno if that makes a difference.

                           

                          Do you have an example of how by is used?

                          • 10. Re: [AS] Iterate over page names throws exception
                            r_olsen Level 1
                            function(){return A.apply(null,[this].concat($A(arguments)))}

                            sstanleyau wrote:

                             

                            You could just close document 1 every time. In fact, you can probably use:

                             

                            close every document saving no

                             

                            Greetings,

                             

                            Closing document 1, or active document won't work (same error about page name not available):

                            --pseudo code
                            repeat doc in documents
                                 repeat pages in doc
                                      export page
                                 end repeat
                                 close document 1/active document
                            end repeat
                            
                            

                             

                            However closing every document at the end works. This isn't perfect when batch exporting many documents though.

                            • 11. Re: [AS] Iterate over page names throws exception
                              Muppet Mark-QAl63s Level 4

                              In AppleScript your objects are of range 1 thru (count or length of objects). So you can't supply 'document object' with a value of '0' as a valid target.

                               

                              Here are some quick examples of using and not using the 'by' parameter…

                               

                              -- From lower to higher values using by parameter (positive value)

                              repeat with i from 3 to 9 by 3

                              display dialog i giving up after 2

                              end repeat

                               

                              -- From higher to lower values using by parameter (negative value)

                              repeat with i from 10 to 2 by -2

                              display dialog i giving up after 2

                              end repeat

                               

                              -- NO by parameter so 1 is assumed (as long as we go lower to higher values)

                              repeat with i from 1 to 5

                              display dialog i giving up after 2

                              end repeat

                               

                              -- NO by parameter (this will fail must use 'by' for higher to lower values)

                              repeat with i from 20 to 18

                              display dialog i giving up after 2

                              end repeat

                              1 person found this helpful
                              • 12. Re: [AS] Iterate over page names throws exception
                                r_olsen Level 1

                                Problem solved with by

                                 

                                tell application "Adobe InDesign CS3"
                                     repeat with i from (count documents) to 1 by -1
                                          tell document i to close saving no
                                     end repeat
                                end tell
                                

                                 

                                Thanks to Mark and Stanley.

                                 

                                Rasmus

                                • 13. Re: [AS] Iterate over page names throws exception
                                  Muppet Mark-QAl63s Level 4

                                  The problem is NOT one of AppleScript. When you are removing objects or closing documents the indexes all get re-shuffled… 2 becomes 1, 3 becomes 2 etc. One way in which you can loop is to start at the higher end and reverse down that way 'i' remains in range and no re-shuffle… Glad you have your solution…

                                  • 14. Re: [AS] Iterate over page names throws exception
                                    r_olsen Level 1

                                    Yes that's true. But I'm just a rookie, and apparently you can't substract 1 from i when messing with the length of an array (in a positive increment).

                                     

                                    You'll have to do a negative increment using by ;-)