7 Replies Latest reply on Dec 6, 2008 12:32 PM by Roy McCoy

    Access indesign files using a script

    Roy McCoy Level 1
      > If you want to do it as a learning exercise, however, one can
      > understand that.

      Particularly since that's now what I want to do myself. I make one ID
      file with "His name's Tom.", "His name's Dick." and "His name's
      Harry." on three lines, and another file similarly with "Her name's
      Mary.", "Her name's Betty." and "Her name's Sue." "Tom" and "Sue" have
      the character style "Client" applied. So how to script the extraction
      of the "Client" names and write them to "clients.txt" on the Mac
      desktop? I'm now trying to do that with AppleScript. It seems very
      basic and shouldn't take long.
        • 1. Re: Access indesign files using a script
          Roy McCoy Level 1
          Okay, I got this far anyway:

          tell application "Adobe InDesign CS4"
          set find text preferences to nothing
          set change text preferences to nothing
          set applied character style of find text preferences to "Client"
          set theFinds to find text
          set theNames to ""
          set theFile to choose file
          repeat with j from 1 to (count of theFinds)
          set theNames to theNames & item j of theFinds & return
          end repeat
          open for access theFile with write permission
          write theNames to theFile
          close access theFile
          end tell

          Two unsolved problems were:

          (1) I couldn't get theFile set to "clients.txt" on the desktop.

          set theFile to (document file "clients.txt" of folder "Desktop" of
          folder "Home" of folder "Users" of startup disk of application "Finder")

          with and without the parentheses, gave the syntax alert "Expected
          â,â but found â"â."
          Previous attempts had failed, which is why I tried copying the
          selection info on the file from the results pane in Script Editor.

          (2) I couldn't figure out how to get the script to append to the end
          of the had-to-be-chosen "clients.txt" file rather than replacing its
          contents.


          Thanks,

          Roy McCoy
          Rotterdam, NL
          • 2. Re: Access indesign files using a script
            Level 1
            for starters, I can't imagine that accessing your text file while talking to InD is a good thing.

            I don't have CS4 to test, but here's a shot:

            tell application "Adobe InDesign CS4"
            set find text preferences to nothing
            set change text preferences to nothing
            set applied character style of find text preferences to "Client"
            set theFinds to find text
            set theNames to ""
            changeFile(theFinds, theNames)
            end tell

            on changeFile(theFinds, theNames)
            set theFile to choose file
            repeat with j from 1 to (count of theFinds)
            set theNames to theNames & item j of theFinds & return
            end repeat
            open for access theFile with write permission
            write theNames to theFile
            close access theFile
            return theNames
            end changeFile
            • 3. Re: Access indesign files using a script
              Level 1
              Hi Roy, plain*clothes,

              Take a look at the other thread. There are several problems to solve in this--you have to use a recursive routine to reliably find the page containing the text, you have to sort the results, etc. It's a bit more complicated than one might think.

              Thanks,

              Ole
              • 4. Re: Access indesign files using a script
                sstanleyau Level 4
                On 5/12/08 12:26 AM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> (1) I couldn't get theFile set to "clients.txt" on the desktop.<br />> <br />> set theFile to (document file "clients.txt" of folder "Desktop" of<br />> folder "Home" of folder "Users" of startup disk of application "Finder")<br /><br />That's a Finder reference, but you're not in the Finder here. Just use a<br />colon delimited path, or something like:<br /><br /> set theFile to ((path to desktop) as text) & "clients.txt"<br /><br />> (2) I couldn't figure out how to get the script to append to the end<br />> of the had-to-be-chosen "clients.txt" file rather than replacing its<br />> contents.<br /><br />    write theNames to theFile starting at eof<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                • 5. Re: Access indesign files using a script
                  Roy McCoy Level 1
                  Shane Stanley wrote:

                  >> (1) I couldn't get theFile set to "clients.txt" on the desktop.
                  >>
                  >> set theFile to (document file "clients.txt" of folder "Desktop" of
                  >> folder "Home" of folder "Users" of startup disk of application
                  >> "Finder")
                  >
                  > That's a Finder reference, but you're not in the Finder here.

                  Durn!

                  > Just use a colon delimited path, or something like:
                  >
                  > set theFile to ((path to desktop) as text) & "clients.txt"

                  I did try to do that, but apparently I did something - several things -
                  wrong. I remember I did get a colon-delimited path to work, but then
                  I couldn't get the names to copy to it right.

                  What is path to desktop other than text?

                  >> (2) I couldn't figure out how to get the script to append to the end
                  >> of the had-to-be-chosen "clients.txt" file rather than replacing its
                  >> contents.
                  >
                  > write theNames to theFile starting at eof

                  Oh, I thought eof was something specially in the Scripting Guide menu
                  script. Trying again, whatever...

                  Not yet, I'm afraid. The previous script still works:

                  tell application "Adobe InDesign CS4"
                  set find text preferences to nothing
                  set change text preferences to nothing
                  set applied character style of find text preferences to "Client"
                  set theFinds to find text
                  set theNames to ""
                  set theFile to choose file
                  repeat with j from 1 to (count of theFinds)
                  set theNames to theNames & item j of theFinds & return
                  end repeat
                  open for access theFile with write permission
                  write theNames to theFile
                  close access theFile
                  end tell

                  Only adding "starting at eof" it should still work... and it does.
                  But I'm anticipating trouble when I (again) add your line in place
                  of "set theFile to choose file"... Actually I pretty much know it's
                  not going to work, since I directly copied the line before - so why
                  don't I try the colon-delimited version this time:

                  set theFile to " Great Royzito:Users:Home:Desktop:clients.txt"

                  Right?

                  But no, this gives exactly the same error alert as your line,
                  that it can't make the string into type file. And then afterward,
                  oddly, if I run it again it says "Adobe InDesign CS4 got an error:
                  File Great Royzito:Users:Home:Desktop:clients.txt is already open."
                  So the string apparently serves to open the file, but not to write
                  to and/or close it. What do you make of this? Something else that
                  broke or is different in CS4, perhaps?

                  The file seemed to close only when I quit all applications and
                  force-quit the Finder. Is there a better or quicker way to do it
                  than that?


                  Thanks,

                  Roy
                  • 6. Re: Access indesign files using a script
                    Level 1
                    I got this one working with pc:<br /><br />app.findPreferences = app.changePreferences = null;<br />app.findPreferences.appliedCharacterStyle = 'cbib';<br />var myDoc = app.activeDocument;<br /><br />for (var i = 1 ; i <= 129; i++)<br />{<br />     var win = i.toString();<br />     var lop = myDoc.search( win, true );<br />     if(lop.length > 0)<br />     {<br />          for (a = 0 ; a < lop.length; a++)<br />          {<br />               var dar = lop[a].parentTextFrames[0].parent.name;<br />               var toTextFile = toTextFile + "\r" + "<cbib>" + win + "</cbib>" + "<page>" + dar + "</page>";<br />          }<br />     }<br />}<br /><br />myTextFile();<br /><br />function myTextFile(){<br />   var myFile = new File("c://RefList/" + myDoc.name.match(/(.*)\.[^\.]+$/)[1] + "_cbib" + ".txt");<br />   myFile.open("w");<br />   myFile.encoding = "UTF8";<br />   myFile.write("<cbib> List for " + myDoc.name.match(/(.*)\.[^\.]+$/)[1] + "\r" + "\r" + toTextFile);<br />   myFile.close();<br />   alert("Ok <cbib>!");<br />}
                    • 7. Re: Access indesign files using a script
                      Roy McCoy Level 1
                      Ole wrote:

                      > Take a look at the other thread. There are several problems to solve
                      > in this--you have to use a recursive routine to reliably find the
                      > page containing the text, you have to sort the results, etc. It's a
                      > bit more complicated than one might think.

                      Killjoy. :-|

                      I find your AppleScript much more poetic.


                      Roy