Referencing User Entered Variable

Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Referencing User Entered Variable

Viewing 7 reply threads
  • Author
    Posts
    • #101898
      Glen Hart
      Member

      Hi I hope someone can help. I am currently trying to create a script to resize objects based on a user entered percentage in a dialog box. When i test the code with a specific value and change var myScaleFactor = 67 it works fine. However as soon as I change it to the variable “resizePer” it comes up with the following error even if the user entered variable is also 67. Thanks in advance, any advice will be greatly appreciated :)

      Using CS6 on Mac
      Error Dialog Box
      Javascript Error!
      Error Number: 11265
      Error String: This value would cause one or more objects to leave the pasteboard.

      Source: {doc.pageItems[i].horizontalScale = doc.pageItems[i].verticalScale = myScaleFactor;}


      var pageWidth = app.activeDocument.documentPreferences.properties.pageWidth;
      var widthRounded = Math.round(pageWidth);
      var pageHeight = app.activeDocument.documentPreferences.properties.pageHeight;
      var heightRounded = Math.round(pageHeight);

      var box = new Window('dialog', "Resize Document");

      box.panel = box.add('panel', undefined, "Enter Percentage to resize");
      var resizePer = box.panel_text1 = box.panel.add('edittext', undefined, "");
      box.panel_text1.value = true;
      box.panel_text1.characters = 14;
      box.panel_text1.active = "true";

      box.panel2 = box.add('panel', undefined, "Current FIle Dimensions");
      box.panel2.group = box.panel2.add('group', undefined, );
      box.panel2.group.orientation='column';

      box.panel2.group.text1 = box.panel2.group.add('statictext', undefined, "Page Width:"+widthRounded+"mm");
      box.panel2.group.text1 = box.panel2.group.add('statictext', undefined, "Page Height:"+heightRounded+"mm");
      box.panel2.group.resizeObj = box.panel2.group.add('button',undefined, "Proceed");
      box.panel2.group.resizeObj.onClick = function(){
      box.close();
      }

      box.show();
      var doc = app.activeDocument;
      var myObjectStyle = doc.objectStyles.itemByName("RoundalFrame");
      var myScaleFactor = resizePer - 0;
      var doc = app.activeDocument;

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

      if(doc.pageItems[i].appliedObjectStyle == myObjectStyle)
      {doc.pageItems[i].horizontalScale = doc.pageItems[i].verticalScale = myScaleFactor;}
      }

    • #101899
      Loic Aigon
      Member

      resizePer isn’t a number when you want to use it. It’s a reference to a scriptUI component.
      At the very least resizePer.text would return the contents of the input. But to proper use it I would force convert it to a number: Number(resizePer.text);
      Eventually you should add a control over typing in to ensure, nothing but numbers can be typed in.

    • #101901
      Loic Aigon
      Member

      A possible improvement:


      var pageWidth = app.activeDocument.documentPreferences.properties.pageWidth;
      var widthRounded = Math.round(pageWidth);
      var pageHeight = app.activeDocument.documentPreferences.properties.pageHeight;
      var heightRounded = Math.round(pageHeight);

      var box = new Window('dialog', "Resize Document");

      box.panel = box.add('panel', undefined, "Enter Percentage to resize");
      var resizePer = box.panel.add('edittext', undefined, "");
      /*
      resizePer.onChanging = function(){
      if ( !/\d+/.test ( resizePer ) ) {
      return;
      }
      $.writeln( "&gt;"+resizePer.text );
      }*/

      resizePer.addEventListener ("keydown", function (k) {

      if (
      !/\d/.test( k.keyName )
      || resizePer.text.length==3
      || (resizePer.text.length==0 &amp;&amp; k.keyName=="0" )) {
      k.preventDefault();
      k.stopPropagation();
      box.panel2.group.resizeObj.enabled = false;
      }
      });

      resizePer.addEventListener ("keyup", function (k) {
      box.panel2.group.resizeObj.enabled = resizePer.text.length&gt;0;
      });

      resizePer.value = true;
      resizePer.characters = 14;
      resizePer.active = "true";

      box.panel2 = box.add('panel', undefined, "Current FIle Dimensions");
      box.panel2.group = box.panel2.add('group', undefined, );
      box.panel2.group.orientation='column';

      box.panel2.group.text1 = box.panel2.group.add('statictext', undefined, "Page Width:"+widthRounded+"mm");
      box.panel2.group.text1 = box.panel2.group.add('statictext', undefined, "Page Height:"+heightRounded+"mm");
      box.panel2.group.resizeObj = box.panel2.group.add('button',undefined, "Proceed");
      box.panel2.group.resizeObj.onClick = function(){
      box.close(1);
      }
      box.panel2.group.resizeObj.enabled = false;

      if ( box.show()==1 ) {
      var doc = app.activeDocument;
      var myObjectStyle = doc.objectStyles.itemByName("RoundalFrame");
      var myScaleFactor = Number ( resizePer.text )/100;
      var doc = app.activeDocument;
      const CS = CoordinateSpaces.INNER_COORDINATES;
      const AP = AnchorPoint.CENTER_ANCHOR;
      const RM = ResizeMethods.MULTIPLYING_CURRENT_DIMENSIONS_BY;

      doc.pageItems.everyItem().resize ( CS, AP, RM, [myScaleFactor, myScaleFactor] );
      }

    • #101903
      Glen Hart
      Member

      Thank you Loic, That makes a lot of sense. I will have another look at it shortly, I never knew about the different ways of interpreting var’s as digits.

    • #101904
      Loic Aigon
      Member

      I realized I removed the control on the applied Object style but you will easily set that back.

    • #101937
      Glen Hart
      Member

      Hi Loic, Thank you again for all your help this week, sorry i couldn’t properly get back to you earlier (other projects came up) I have since been trying to incorporate your revised script style. I have a few questions though that I’m hoping you can help me with. Firstly, it still doesn’t actually run the script, it loads up the dialog box and it allows you to enter a value for the variable “resizePer” but it initially had a greyed out “PROCEED” button which I could not use. Commenting out box.panel2.group.resizeObj.enabled = false; resolved this and the button appears normally now but unfortunately it still doesn’t do anything and the only way to remove the dialog box is to ESC out of it. Are you able to offer any advice on what may be causing this? I have tried making a few adjustments but nothing so far has work.

      In addition I have been able to updated my initial script so this at least works but I’m conscious your version seem much more practical moving forward and I’m keen to learn how to how make these more parctical.

      Questions
      1 – What does box.panel2.group.resizeObj.enabled = false; actually do/mean?
      2 – what does the 1 in the box.close(1); / box.show()==1 ) actually do/mean?
      3 – where you reference the co-ordinates at the end. listed below. Do you need to list the coordinate spaces first or would it also work just reference anchorpoint, If so is there a benefit to doing this?

      const CS = CoordinateSpaces.INNER_COORDINATES;
      const AP = AnchorPoint.CENTER_ANCHOR;

    • #101952

      Besides of your questions, I tested Loics script and it seems, that the forum software made some typos:

      1. resizePer.text.length==0 && k.keyName=="0" should be resizePer.text.length==0 && k.keyName=="0"
      2. resizePer.text.length>0; should be resizePer.text.length;
      

      With those 2 modifications, the proceed-button is active, when you enter a value in the field above.

      Edit: Hm, I’m not able to copy the typos here, but if you compare the original code, you will see where the problems are …

    • #101970
      Loic Aigon
      Member

      Indeed, the forum engine did replace some characters thus corrupting the script.

      To answer your questions :

      1 – What does box.panel2.group.resizeObj.enabled = false; actually do/mean?
      The underlaying idea is to make the action button non clickable if the user hasn’t entered any valid number. That’s a convenient way of preventing bugs and force user to check twice what he is typing in. The button will become clickable if the user enters a valid number.

      2 – what does the 1 in the box.close(1); / box.show()==1 ) actually do/mean?
      This is a return code that helps you knowing which button did the user actually click. In your case you have only one button, but you could have two or three and it’s convenient to ensure user did click on “run” button.

      3 – where you reference the co-ordinates at the end. listed below. Do you need to list the coordinate spaces first or would it also work just reference anchorpoint, If so is there a benefit to doing this?
      No you need to set the coordinate space first because it will let the script knows in which system has the geometry to be executed. Actually this is well documented:
      https://www.indesignjs.de/extendscriptAPI/indesign-latest/#PageItem.html#d1e207514__d1e210583

      Note that the script isn’t bullet proof in the sense that objects could be locked or on a locked layer…

Viewing 7 reply threads
  • You must be logged in to reply to this topic.
>