11 Replies Latest reply on Jan 27, 2012 9:01 AM by John Hawkinson

    InDesign CS4 ExtendScript can't report fn.exists status?

    vze26m98 Level 1

      Hi all-

       

      I'm running Windows XP and Indesign CS4 (6.06), and I can't get ExtendScript to tell me that a file exists. It seems to be because the XML of placed files has a different URI than ExtendScript wants to see. Here's the XML of a placed file:

       

      image01.png

       

      Here's some not very attractive code:

       

      var fn = new File('file:///Documents%20and%20Settings/Admin/My%20Documents/SAP-2_2_1-ISSUED%20TO%20CHARLES/Links/Signs/A1104.pdf');
      $.writeln('file:///Documents%20and%20Settings/Admin/My%20Documents/SAP-2_2_1-ISSUED%20TO%20CHARLES/Links/Signs/A1104.pdf');
      $.writeln('exists: ' + fn.exists);
      
      var pictureElement = app.activeDocument.xmlElements[0].xmlElements[4].xmlElements[0].xmlElements[6].xmlElements[0].xmlElements[0];
      fn = new File(pictureElement.xmlAttributes[0].value);
      $.writeln(pictureElement.xmlAttributes[0].value);
      $.writeln('exists: ' + fn.exists);
      
      

       

      And here's the result of running the code:

      image02.png

       

      It looks like ExtendScript will report true for fn.exists if the URL starts with "file:///Documents...", but reports false if the filename starts with "file:///C:/Documents..." which is (sadly) the results that InDesign wants to place into an XML picture href attribute.

       

      As a side note, none of this seems a problem on OSX with CS5.

       

      I know I might not have given folks enough information here, but does this ring a bell with anyone? What's going on?

       

      Best and thanks, Charles

        • 1. Re: InDesign CS4 ExtendScript can't report fn.exists status?
          Marijan Tompa [tomaxxi] Level 4

          Hi Charles,

           

          Here is a quick fix. Change fn variable to this:

           

          fn = new File(pictureElement.xmlAttributes[0].value.toString().replace("file:///", ""));
          

           

          Hope that helps.

           

          --

          Marijan (tomaxxi)

          http://tomaxxi.com

          1 person found this helpful
          • 2. Re: InDesign CS4 ExtendScript can't report fn.exists status?
            John Hawkinson Level 5

            Charles:

            I'm running Windows XP and Indesign CS4 (6.06), and I can't get ExtendScript to tell me that a file exists. It seems to be because the XML of placed files has a different URI than ExtendScript wants to see.

            As tomaxxi implied and I think you yourself said, the syntax for file:// URLs on with DOS driver letters is not universally defined, and ExtendScript and InDesign XML apparently do it differently. I think you're much better off just handing the platform path to the File() constructor.

             

            Aside: Marijan's method, which feels familiar to me (ha ha ha!) doesn't account for files that might actually have file:/// in their filenames somewhere. Hopefully that's not possible.

            • 3. Re: InDesign CS4 ExtendScript can't report fn.exists status?
              vze26m98 Level 1

              Hi John and Marijan-

               

              Thanks for the confirmation and suggested workaround. Pretty amazing such a mismatch existed, but I guess Adobe's a big company...

               

              Best wishes, Charles

              • 4. Re: InDesign CS4 ExtendScript can't report fn.exists status?
                John Hawkinson Level 5

                You should at least give him the Helpful Answer points!

                Pretty amazing such a mismatch existed, but I guess Adobe's a big company...

                Well, XML is really the neglected stepchild of the black sheep of the family, right? Of course, you don't want to know what ExtendScript is in that metaphor...

                • 5. Re: InDesign CS4 ExtendScript can't report fn.exists status?
                  vze26m98 Level 1

                  John Hawkinson wrote:

                   

                  You should at least give him the Helpful Answer points!

                   

                  Apologies- For some reason, I thought there was only one award per question. Hopefully, you'll excuse me as a relative newbie to the forum.

                   

                  Charles

                  • 6. Re: InDesign CS4 ExtendScript can't report fn.exists status?
                    Marijan Tompa [tomaxxi] Level 4

                    John,

                     

                    I really don't see how the file:/// could appear somewhere in the path.

                    Windows doesn't allow any special characters in file names.

                    But in case it happens, Charles can use RegExp search instead of

                    simple string search:

                     

                    fn = new File(pictureElement.xmlAttributes[0].value.toString().replace(/^file\:\/\/\//, ""));
                    

                     

                    --

                    Marijan (tomaxxi)

                    • 7. Re: InDesign CS4 ExtendScript can't report fn.exists status?
                      John Hawkinson Level 5

                      I really don't see how the file:/// could appear somewhere in the path.

                      Yeah, I don't think so either, hence "Hopefully that's not possible."

                      I had tried to find a short snippet that was readable and functional, and sort of gave up. IMO regexps with backslash escapes approach unreadable. Maybe Regexp("^file:///"") is the way to go. Ugh.

                      • 8. Re: InDesign CS4 ExtendScript can't report fn.exists status?
                        vze26m98 Level 1

                        I think I'm far enough out of the woods with this issue that I can add a a caveat:

                         

                        Windows uses drive letters for volume names, and these need to be included in the absolute filename.

                         

                        InDesign CS4 and CS5.5 XML both want to write something like:

                         

                        file:///C:/restofthepath...

                         

                        where ExtendScript wants to see:

                         

                        file:///c/restofthepath...

                         

                        A fix for this would have to take into account additional drives other than the root, eg:

                         

                        file:///z/restofthepath...

                         

                        and so forth.

                         

                        I have no idea whether the volume name needs to be lowercase. I also assume that "file:///restofthepath..." can refer to the root volume (this is the behavior that ExtendScript suggests), but I haven't explicitly tested it.

                         

                        HTH, Charles

                        • 9. Re: InDesign CS4 ExtendScript can't report fn.exists status?
                          John Hawkinson Level 5

                          Charles, are you saying that you cannot give the File constructor a path with a colon, like new FIle("C:/restofpath")? I was under the impression that doing so worked fine, hence tomaxxi's solution of stripping off the leading file:// (or maybe file:///) before calling the constructor?

                          • 10. Re: InDesign CS4 ExtendScript can't report fn.exists status?
                            vze26m98 Level 1

                            Well, my knowledge is conditioned by my particular interest.

                             

                            My situation involves creating placed pictures inside a TextFrame which is reflected in an XML structure. Because I'm interchanging XML data with Filemaker, I have an interest in verifying whether the picture links are preserved during the interchange. So I run through the XML structure after import to InDesign, and verify whether the files listed in the "href" attribute of a picture element actually exist on disk.

                             

                            InDesign wants to use "file:///C:/restof path..." in the XML. It's a product of the placement process and can't be changed without bad consequences AFAICT.

                             

                            ExtendScript can't take this filespec as a string from the XML, and on construction of a new File object with it, recognize that it exists. This is because ExtendScript wants to see "file:///c/restofpath..." Under those conditions, ExtendScript will construct a File object and recognize that the file already exists on disk.

                             

                            I'm not certain whether ExtendScript actually constructs a new File object when the "file:///C:/restofpath..." incantation is used. As far as I got was quizzing the length of the new File, which returned -1. Obviously, reading and writing a file under the circumstances would have bad consequences, because there's a file with the same filespec actually residing at that location. So whether use of the File object ExtendScript returns from the constructor results in overwriting the existing file, or worse, I do not know.

                             

                            HTH, Charles

                            • 11. Re: InDesign CS4 ExtendScript can't report fn.exists status?
                              John Hawkinson Level 5

                              It doesn't look like you answerd me about omiting file://.

                               

                               

                              I would try the .exists() method.

                              Not sure why reading the file is dangerous.