8 Replies Latest reply on Jul 15, 2015 3:09 PM by Whatevermajorloser

    How to move activeDocument's position in app.documents[]

    Whatevermajorloser Level 1

      Hello,

       

      Is there a way to move the current document's position in the app.documents array? I've got this function that will transfer part of an image's name (in this case an ID number) from one to the other. The way I've got it set up is that the user has to have at least one image open containing the ID number they want to extract and then they run the script on another image where they want the ID number to go in to. (If that makes sense?)

       

      function connectOu() {
      
          if (app.documents.length <= 1){
          alert ("Oops! \nThere needs to be at least one other image open with the PID number you want this transferred to.")
          } else {
      
          var originalPid = activeDocument.name
          var originalPath = activeDocument.path
          var originalLength = activeDocument.name.length
          var originalType = originalPid.substring(originalLength - 6,originalLength - 4)
          var whatType = prompt ('What type of image should this be?', originalType)
      
              if (whatType === null) {
      
          } else {
      
          activeDocument.save()
          activeDocument.close() 
          var fileRef = new File(originalPath + "/" + originalPid)
          var docRef = app.open (fileRef) 
      
          activeDocument = app.documents[0]
      
          var newPid = activeDocument.name.substring(0,6)
          var newPath = activeDocument.path
          var middlePart = activeDocument.name.substring(6, activeDocument.name.length - 6)
      
          activeDocument = documents.getByName(originalPid) 
      
          var saveFile = new File(newPath+ "/" + newPid + middlePart + whatType + ".tif");
          activeDocument.saveAs(saveFile, TiffSaveOptions,false,Extension.LOWERCASE);
          }
      }
      }
      

       

      Right now the script closes the activeDocument and reopens it so that it is always the last one in the array due to the fact that the script targets the first image in the array for the extraction. Doing it this way means the script is a little slow and I'd love to speed it up by simply moving the activeDocument's position instead of opening and closing it.

       

      I appreciate any help I can get!

       

      Thanks.

        • 1. Re: How to move activeDocument's position in app.documents[]
          c.pfaffenbichler Level 9

          Why close the document at all?

          You can define a variable

          var originalFile = activeDocument;

          and use that to select the document again if necessary.

          activeDocument = originalFile;
          • 2. Re: How to move activeDocument's position in app.documents[]
            Whatevermajorloser Level 1

            Hi c.pfaffenbichler,

             

            Thanks for your response. Isn't the order of the documents in the array determined by the order in which they were actually opened? I don't think switching between them changes that, does it? So I think, If I went with your suggestion, I'd have a problem if the user had only opened two documents, the first one being the one they actually run the script on. In that case, the script would just select app.documents[0] and would just copy the ID Number from the image's filename that's the activeDocument and nothing would change..

             

            Thanks

            • 3. Re: How to move activeDocument's position in app.documents[]
              c.pfaffenbichler Level 9

              Could you please post screenshots that clarify the actual task?

              Why you employ Save As, do you want to keep two versions of the file/s?

              • 4. Re: How to move activeDocument's position in app.documents[]
                Whatevermajorloser Level 1

                Yes, of course. I'm not sure how to clarify the task with screenshots, but lets say you have two images open.

                 

                Screen Shot 2015-07-15 at 09.39.00.png

                The script is run by the user on the current document (568452_mrp_fr), which selects 576231_mrp_ou (because it always selects app.documents[0]), extracts the number from it and replaces the number in the other image with that number. In this situation, the script runs fine (without having to close the document) because 576231 was opened first (and is thus position 0 in the documents[] array) and then 568452 was open after.

                 

                However, the reason I close the document and reopen is to ensure that the document the script being run on is never position [0] in the array. This could happened in the even that the document the user wants to run the script on is opened first.

                 

                Hope that makes sense? And yes, I'd like to keep both versions (which it currently does).

                • 5. Re: How to move activeDocument's position in app.documents[]
                  c.pfaffenbichler Level 9

                  If the issue is selecting the document that is not the active one you could use other approaches.

                  For example get the documents’ itemIndex’ with Action Manager code and use those to select the files later on.

                   

                  What is supposed to happen when more than 2 images are open?

                  • 6. Re: How to move activeDocument's position in app.documents[]
                    Whatevermajorloser Level 1

                    If there are more than two images open, it's the same. The ID numbers are product ID's, and sometimes an image is shared between two products, which requires taking one that has already been retouched for one product and changing it's ID number to the other products.

                     

                    I'm not quite sure what you mean by getting the documents itemIndex with Action Manager code? I'm still very new to javascript - are you able to give me an example?

                    • 7. Re: How to move activeDocument's position in app.documents[]
                      c.pfaffenbichler Level 9
                      If there are more than two images open, it's the same. The ID numbers are product ID's, and sometimes an image is shared between two products, which requires taking one that has already been retouched for one product and changing it's ID number to the other products.

                      Huh?

                      Should the operation you scripted be performed on all open images or not?

                       

                      I'm not quite sure what you mean by getting the documents itemIndex with Action Manager code?

                      This would in turn make all the documents active that are not the active one at the start:

                      // 2015, use it at your own risk;

                      #target photoshop

                      if (app.documents.length > 1) {

                      // determine active doc’s id;

                      var ref = new ActionReference();

                      ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("documentID"));

                      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                      var docDesc = executeActionGet(ref);

                      var theActiveID = docDesc.getInteger(stringIDToTypeID("documentID"));

                      // get application stuff;

                      var ref = new ActionReference();

                      ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("numberOfDocuments"));

                      ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                      var applicationDesc = executeActionGet(ref);

                      var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfDocuments"));

                      // determine the ids and indices of the open docs;

                      for (var m = 1; m <= theNumber; m++) {

                      var ref = new ActionReference();

                      ref.putIndex( charIDToTypeID("Dcmn"), m);

                      var docDesc = executeActionGet(ref);

                      var theID = docDesc.getInteger(stringIDToTypeID("documentID"));

                      var theIndex = docDesc.getInteger(stringIDToTypeID("itemIndex"));

                      var theName = docDesc.getString(stringIDToTypeID("title"));

                      // if other than the original one;

                      if (theID != theActiveID) {

                        var ref = new ActionReference();

                        ref.putIdentifier(charIDToTypeID('Dcmn'), theID);

                        var desc = new ActionDescriptor();

                        desc.putReference(charIDToTypeID('null'), ref);

                        executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);

                        alert ("now "+theName+" is active")

                        }

                      };

                      };

                      • 8. Re: How to move activeDocument's position in app.documents[]
                        Whatevermajorloser Level 1

                        Sorry if that was unclear. There would only ever be one image that needs its number replacing amongst all open images. All the other images aside from that image that are open are ones that already have the correct number. Sorry, it's just something that comes up in the way that we work and it's difficult to explain:

                         

                        Product A with ID number X has 4 images that need retouching.

                        Retoucher opens Product A's images, and retouches them.

                        Retoucher checks Retouch Notes and sees that Product A also uses one of Product B's images with ID Number Y.

                        Retoucher opens said Product B's image and needs to replace it's ID number with Product A's Number X. (This is so that on the upload, all 5 images will show in Product A's page)

                        Retoucher runs script to replace Number Y with Number X by extracting the number from the other already opened images.

                         

                        Hope that makes sense now. Sorry for the late reply - got pretty busy at work. Your answer was a little confusing for me, but I've figured out a workaround. Using script listener, I simply tabbed to the next document and used what it spat out:

                         

                            var idslct = charIDToTypeID( "slct" );
                            var desc10 = new ActionDescriptor();
                            var idnull = charIDToTypeID( "null" );
                                var ref4 = new ActionReference();
                                var idDcmn = charIDToTypeID( "Dcmn" );
                                ref4.putOffset( idDcmn, 1 );
                            desc10.putReference( idnull, ref4 );
                            var idDocI = charIDToTypeID( "DocI" );
                            desc10.putInteger( idDocI, 1496 );
                        executeAction( idslct, desc10, DialogModes.NO );
                        
                        

                         

                        I'm using this now instead of opening and closing the document and it works great!

                         

                        I really appreciate your help - I'm sorry I didn't make it very easy due to my poor explanation of the problem.