5 Replies Latest reply on Apr 11, 2009 2:59 PM by John Kallios

    JS CS4 splitting the document path

    John Kallios Level 2

      I'm trying to split the document file path so I may navigate up one or two folders and then into a specific subfolder.

       

      When I try to split the "myDoc.filePath" it errors. If I split "myDoc.name" it works correctly.

       

       

      var myDoc = app.activeDocument;
      var myDocPath = myDoc.filePath;
      var mySplit = myDoc.name.indexOf ("_");
      var myFileName = myDoc.name.slice (0,mySplit);
      var myPdfFolder = myFileName + "pdf";
      var mySavePath = myDocPath
      var mySplit2 = myDoc.filePath.indexOf (myFileName);
      var myFinalPath = myDoc.filePath.slice (0,mySplit2) + myFileName + "/" + myPdfFolder;
      alert(myFinalPath);
      Can someone shed light on how to split the document path?
      Thanks,
      John

       

       

      Document path: ~desktop/N123456/working/N123456_01

       

      Note: the last folder name does not always exist. Often the "working" folder is the last folder. Working folder and N123456pdf reside on the same level.

       

      Goal is to change the save to document path: ~desktop/N123456/N123456pdf

        • 1. Re: JS CS4 splitting the document path
          Dave Saunders Level 4

          You're going about this the hard way.

           

          Better to work with the file's parent property (which is the folder that holds the file), and then it's parent.

           

          The specific issue you're running into is that slice works on strings and you're trying to use it on a file object.

           

          Dave

          • 2. Re: JS CS4 splitting the document path
            John Kallios Level 2

            >>You're going about this the hard way.

             

            I knew that even if I was ignorant of any other way to go about it.

             

            Thanks for pointing the way.

             

            (btw, since I'm always logged off when I return to these fora, I know I'm going to click on the "report abuse" button by accident when I mean to reply. I mention this because I almost reported your post as abuse. I hope there is a confirmation box before it goes through.)

            • 3. Re: JS CS4 splitting the document path
              John Kallios Level 2

              OK, this does return what I'm after.

               

              var myDoc = app.activeDocument;
              var myDocPath = myDoc.filePath;
              var mySplit = myDoc.name.indexOf ("_");
              var myFileName = myDoc.name.slice (0,mySplit);
              var myPdfFolder = myFileName + "pdf";
              var mySavePath = myDocPath
              var myRootPath = myDocPath.parent.fsName
              var mySplitPath = myRootPath.indexOf (myFileName);
              var myFinalPdfPath = myRootPath.slice (0,mySplitPath) + myFileName + ("/") + myPdfFolder
              alert(myFinalPdfPath);
              However, I am still interested if in an example of a simpler way to go about it.
              Thank you again Dave. I have much more reading to do.

              • 4. Re: JS CS4 splitting the document path
                Dave Saunders Level 4

                Here's some code I wrote recently to find a folder named "Snippets" starting from the location of a document. It first looks in the same folder and then its parent and on up until it reaches the top of the file hierarchy:

                 

                //DESCRIPTION: Search for Snippets folder from Document location
                (function() {
                    if (app.documents.length > 0) {
                        var myFolder = locateRelatedFolder(app.documents[0].filePath, "Snippets");
                        if (myFolder == null) {
                            alert("Unable to locate Snippets folder");
                            return;
                        } else {
                            alert("Snippets folder is: " + myFolder);
                        }
                    }
                    function locateRelatedFolder(folder, name) {
                        while (folder != null) {
                            var relatedFolder = Folder(folder + "/" + name);
                            if (relatedFolder.exists) return relatedFolder;
                            folder = folder.parent;
                        }
                    } // end locateRelatedFolder
                }())

                 

                I've enclosed the code in an anonymous function. Don't let that put you off. You could just delete the first line after the description and the last line.

                 

                Dave

                1 person found this helpful
                • 5. Re: JS CS4 splitting the document path
                  John Kallios Level 2

                  Thanks again,

                   

                  I need to digest it for a while to understand it.