3 Replies Latest reply on Jul 23, 2011 2:42 PM by John Hawkinson

    Applying styles to place assest from AppleScript. CS5.5

    JJ0330 Level 1

      We are running a script that based on a csv file, loads various elements from a library then imports XML.  An example of these elements might be designed to have 4 separate item records within it and each item record might contain a few text boxes and an image and each of these items have xml tags.  To better understand, think of  a supermarket circular where they might have 4 meat items in one section with a description, size, price and an image.  We've broken it into different sections and based on data coming from our database we can relate to a specific element in our indesign library and know how many items will need to be loaded in that section.  This all works fine so far and is pretty cool how it loads but we are looking to maybe pass a basic page theme which might be both object and paragraph styles so we'd like to apply these styles through our script because it is possible that different elements might get different styles in same load or even same elements might be re-used in same load and have different styles so really can't define elements with styles we'd like or associate specific xml tages with styles because might be different in each element.

       

      I can in Applescript apply paragraph and object styles to by either index like applying style x to paragraph 1 but would like to do it to element that we placed on page.  Here is an example of our script to maybe better understand.  Look for comments where we place our assets(elements) on page.  Looking to how we can apply style to say paragraph 1 or text frame 1 of placedAsset says can't get text frame 1 or paragraph 1 of group xxx.

       

      Tried variations of:

      tell application "Adobe InDesign CS5.5"
      set myDocument to active document
      set myPage to active page of active window
      set placedAsset to place asset asset "7" of library "GT.indl" on myDocument
      move placedAsset to {0.65, 0.6}
      tell myDocument
        set myStyle to paragraph style "Style1"
        set objStyle to object style "objStyle"
      --This works for just applying styles by index
        tell paragraph 1 of text frame 1
         apply paragraph style using myStyle
        end tell
        tell text frame 1
         apply object style using objStyle
        end tell
      --This gives error described above
        tell text frame 1 of placedAsset
         apply object style using objStyle
        end tell
      end tell
      end tell

       

       

      ----------------------------------------------------------------------------------------
      Script which runs now but is not set to apply styles yet:

      ----------------------------------------------------------------------------------------

      try
      set CSVstr to "Please locate your CSV file..."
      set CSV_File to (choose file with prompt CSVstr) as text
      set csvData to read file CSV_File
      set csvEntries to paragraphs of csvData
      tell application "Adobe InDesign CS5.5"
        set myDocument to active document
        tell XML view preferences
         set show attributes to false
         set show structure to true
         set show tagged frames to false
         set show tag markers to false
         set show text snippets to true
        end tell
        tell XML import preferences
         set allow transform to false
         set create link to XML to false
         set ignore unmatched incoming to true
         set ignore whitespace to true
         set import CALS tables to true
         set import style to merge import
         set import text into tables to false
         set import to selected to false
         set remove unmatched existing to false
         set repeat text elements to true
        end tell
        set myPage to active page of active window
        set AppleScript's text item delimiters to ","
        repeat with i from 1 to count csvEntries
         set csvEntry to csvEntries's item i
         set {fldElement, fldLocationX, fldLocationY} to csvEntry's text items
         --  display dialog fldElement
         set placedAsset to place asset asset fldElement of library "GTElements.indl" on myDocument
         move placedAsset to {fldLocationX, fldLocationY}
      --This is where we'd like to apply styles to placed asset contents. This placed asset will have a few text frames and at
      --least one image.  We would just pass extra fields in our csv file with names of styles to be applied


        end repeat
        set AppleScript's text item delimiters to {""}
       
        tell myDocument
         set XMLstr to "Please locate your XML file..."
         set XML_File to (choose file with prompt XMLstr)
         import XML from XML_File
        end tell
        tell application "Adobe InDesign CS5.5"
         set find text preferences to nothing
         set change text preferences to nothing
         set find what of find text preferences to "mykzlplx"
         set change to of change text preferences to ""
         tell active document
          change text
         end tell
         set find text preferences to nothing
         set change text preferences to nothing
         set find what of find text preferences to "Â"
         set change to of change text preferences to ""
         tell active document
          change text
         end tell
         set find text preferences to nothing
         set change text preferences to nothing
        
        end tell
       
      end tell
      end try

        • 1. Re: Applying styles to place assest from AppleScript. CS5.5
          John Hawkinson Level 5

          It would be a lot easier to help you if you were more succint. You should practice narrowing down the problem to the smallest possible case that demonstrates it. This is a good skill for two reasons: (1) It makes it easier to find the problem yourself (2) It makes it easier for others to help you.

           

          I believe your problem is you are asking for the first textframe of the placed asset:

           

          set placedAsset to place asset asset "7" of library "GT.indl" on myDocument

          tell myDocument
          --This gives error described above
            tell text frame 1 of placedAsset
             apply object style using objStyle
            end tell
          end tell
          end tell

          At least in JavaScript, the place command returns an array of objects, without regard to type. I suspect that's true in AS as well.

          So you probably want something like "tell first item of placedAsset" rather than "text frame 1". And if you want to make sure it is a text frame, because there are other things in the placedAsset, you'll need to use a filter. ("first item of placedAsset whose constructor is textFrame" or something like that).

           

          Again, my applescript is really rusty, but I think that's the general idea.

          • 2. Re: Applying styles to place assest from AppleScript. CS5.5
            JJ0330 Level 1

            Thanks, I'm sorry if I gave too much detail.  I'm new to not only indesign and applescript and even the Mac:) 

            • 3. Re: Applying styles to place assest from AppleScript. CS5.5
              John Hawkinson Level 5

              Not a problem. It's also appreciated if you can mark correct answers (and helpful answers) to give points to those who reply.