5 Replies Latest reply on Aug 7, 2015 12:25 PM by sidneydavenport

    A little help to combine two js scripts

    Level 1
      Dear all,

      I have made a script selecting a Color Setting preset and would like to combine it with a script updating the links in the current doc. I've been fiddling with functions but that didn't work. I'm very much a beginner, please be patient :-)

      script setCMS:

      var ListLength = app.colorSettings.cmsSettingsList.length;
      var ColorList = app.colorSettings.cmsSettingsList;
      var mySetting = 'Europe General Purpose 2';
      var found = false;

      for(var a = 0; a < ListLength; a++)
      {
      if(ColorList[a] == "Europe General Purpose 2")
      {
      found = true;
      alert('found');
      app.colorSettings.cmsSettings = mySetting;
      }
      }
      if (found==false)
      {
      alert("Warning: <" + mySetting + "> can't be found");
      }

      script updateLinks:

      var theLinks = app.documents[0].links;

      for (var i = 0; i < theLinks.length; i++)
      {
      theLinks[0].update();
      }

      Best regards,
      Rasmus
        • 1. Re: A little help to combine two js scripts
          try67 MVP & Adobe Community Professional
          Basically you can simply put the second script directly after the first and it should work just fine, but your idea of using functions is better because it's always better to seperate operations which are not related to one another or that run independently. You can do it like so:

          // BEGIN COMBINED SCRIPT
          SelectColorSetting();
          UpdateLinks();

          function SelectColorSetting() {
          var ListLength = app.colorSettings.cmsSettingsList.length;
          var ColorList = app.colorSettings.cmsSettingsList;
          var mySetting = 'Europe General Purpose 2';
          var found = false;

          for(var a = 0; a < ListLength; a++) {
          if(ColorList[a] == "Europe General Purpose 2") {
          found = true; alert('found');
          app.colorSettings.cmsSettings = mySetting;
          }
          }
          if (found==false) {
          alert("Warning: <" + mySetting + "> can't be found");
          }
          }

          function UpdateLinks() {
          var theLinks = app.documents[0].links;

          for (var i = 0; i < theLinks.length; i++) { theLinks[0].update(); }
          }

          // END COMBINED SCRIPT
          • 2. Re: A little help to combine two js scripts
            Level 1
            Hi try,

            Greeeat, thank you for the lesson in functions :-)

            Can you help me with one last little thing: In the UpdateLinks function I would like to display an alert if the variable theLinks is empty. But I can't seem to get the syntax correct (do I target .length or is theLinks a string, the info from the object model in ESTK is very minimal IMO).

            Cheers from Denmark,
            Rasmus
            • 3. Re: A little help to combine two js scripts
              try67 MVP & Adobe Community Professional
              Hi Rasmus,

              You're welcome. Your variable theLinks is an array, so if it's empty (ie. there are no links in the document), its length would be 0. So what you should do is:

              function UpdateLinks() {
              var theLinks = app.documents[0].links;
              if (theLinks.length==0) {alert("There are no links to update.")}
              else {
              for (var i = 0; i < theLinks.length; i++) { theLinks[i].update(); } } }

              PS - I just now noticed that you had a mistake in your original script. It said:
              theLinks[0].update();
              which would cause only the first link to update. In order to update all links you need to change it to the script above.
              • 4. Re: A little help to combine two js scripts
                Level 1
                Wonderful! :-)

                Cheerios!
                Rasmus
                • 5. Re: A little help to combine two js scripts
                  sidneydavenport Level 1

                  Thank you, try67,

                   

                  Even though I don't understand js, between your script and the ExtendScript error messages, I was able to SUCCESSFULLY combine my two scripts.