4 Replies Latest reply on Mar 12, 2017 6:38 AM by mauricior6328708

    Help with a script to automate a task!

    mauricior6328708 Level 1

      Hello, everyone!

      I have a script here and need to modify it to work as follows:

       

      1- (Process 1) Open a document to start a task.

      2- (Process 2) If the document is already open, I do not need to open it again to repeat the task!

       

      I just need to skip the (Process 1) if the document is already open!

      Maybe this seems a very silly thing, but fundamental to my tasks. Thank you!

      Here is my script:

      #target photoshop
      cTID = function(s) { return app.charIDToTypeID(s); };
      sTID = function(s) { return app.stringIDToTypeID(s); };
      function Open() {
      
      
       // Process 1
        function step1(enabled, withDialog) {
          if (enabled != undefined && !enabled)
            return;
          var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
          var desc1 = new ActionDescriptor();
          desc1.putInteger(cTID('DocI'), 4205);
          executeAction(cTID('Opn '), desc1, dialogMode);
       };
      
      
        // Process 2
        function step2(enabled, withDialog) {
          if (enabled != undefined && !enabled)
           return;
         alert('Here starts the Task!')
        };
        step1(true, true); // Open
        step2();// Start
      };
      
      
      Open.main = function () {
        Open();
      };
      Open.main();
      
      
        • 1. Re: Help with a script to automate a task!
          Jarda Bereza Level 4

          You can check all opened documents.

           

          var documents = app.documents;

          for(var i = 0;i<documents.length;i++){

              try{

               alert(documents[i].fullName);

              }

              catch (e){

                  alert(e);

              }

          }

           

          And do something if document paths are same.

          • 2. Re: Help with a script to automate a task!
            mauricior6328708 Level 1

            Hi Jarda Bereza Thanks for the attention! I understood here how to run your script. Not quite what I need. Maybe you did not understand the question.

            It would have to be something like this:

             

            If the document has already been opened: Run the script from "Process 2" (Line 19)

            Else the document was not open: Run the script from "Process 1" (Line 08)

             

            I think it's better to understand!

            Thank you.

            • 3. Re: Help with a script to automate a task!
              greenrookie Level 3

              Like this?

               

              #target photoshop  
              cTID = function(s) { return app.charIDToTypeID(s); };  
              sTID = function(s) { return app.stringIDToTypeID(s); };  
              
              if (app.documents.length > 0) {
                  step2();
                  }
                  else {step1(); 
                      step2();}
              
               // Process 1  
                function step1(enabled, withDialog) {  
                  if (enabled != undefined && !enabled)  
                    return;  
                  var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);  
                  var desc1 = new ActionDescriptor();  
                  desc1.putInteger(cTID('DocI'), 4205);  
                  executeAction(cTID('Opn '), desc1, dialogMode);  
               };  
              
                // Process 2 
                function step2(enabled, withDialog) {  
                  if (enabled != undefined && !enabled)  
                   return;  
                 alert('Here starts the Task!')  
                };  
              
              
              • 4. Re: Help with a script to automate a task!
                mauricior6328708 Level 1

                That's right, perfect!

                Now I can understand how this process is done greenrookie. Thanks for the help brother!