Skip to main content
Mohkhasa
Participating Frequently
June 25, 2022
Answered

[Help] Illustrator Scale script by user defined percentage

  • June 25, 2022
  • 2 replies
  • 1292 views

Hello,

 

This is my first post here,

I am trying to create my first script to scale objects from each object's origin(center), not the overall origin.

The script goes like this:

1. You select the objects you want to scale

2. Run the script

3. Enter the scale value (just like the Scale tool in illustrator) (e.g. 50%)

4. The selected objects will be scaled by the value you entered. (e.g. 50%)

 

I can't figure out how to connect the value you enter to the scale function part. (textNewScale)

var totalMatrix = concatenateScaleMatrix( scaleMatrix, textNewScale, textNewScale);

- line 60 in the below script 

 

Here is the full script

(function () {

    // The script code goes inside this anonymous function.
    // Reusable UI variables
    var g; // group
    var p; // panel
    var w; // window

    // Permanent UI variables
    var btnCancel;
    var txtScaleSizeInput;
    var btnOk;
    var txtFolderInput;
    var textNewScale
    textNewScale = 

    // CREATE USER INTERFACE

    w = new Window("dialog", "Adobe Script Tutorial 1");
    p = w.add("panel");
    g = p.add("group");
    
    txtScaleSizeInput = g.add("edittext", undefined, textNewScale, {
        borderless: "true",
        name: textNewScale,
        
    });

    txtScaleSizeInput.preferredSize = [100, -1];
    g.alignChildren = "center";
   
    g = w.add("group");
    g.alignChildren = "center";
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");
    


    // UI EVENT HANDLERS
    btnOk.onClick = function () {
        w.close(1);
    };

    btnCancel.onClick = function () {
        w.close(0);
    };

    // SHOW THE WINDOW
    // w.show();

    if (w.show() == 1) {
        // alert("OK was clicked");
        process();
    }

    function process() {
            if ( app.documents.length > 0 ) {
                var moveMatrix = app.getTranslationMatrix ( 0.5, 1.5 );
                var scaleMatrix = app.getScaleMatrix (100, 100);
                var totalMatrix = concatenateScaleMatrix( scaleMatrix, textNewScale, textNewScale);
                var doc = app.activeDocument;
                for ( i = 0; i < activeDocument.selection.length; i++ ) {
                    activeDocument.selection[i].transform( totalMatrix );
                }
            }
    }

})();

 

Thank you

This topic has been closed for replies.
Correct answer femkeblanco

I've made two changes to your script. 

(1)  I've got the input from the eddittext box by using txtScaleSizeInput.text.

(2)  I've used the resize() function instead of matrices.

 

(function () {

    // The script code goes inside this anonymous function.
    // Reusable UI variables
    var g; // group
    var p; // panel
    var w; // window

    // Permanent UI variables
    var btnCancel;
    var txtScaleSizeInput;
    var btnOk;
    var txtFolderInput;
    var textNewScale = "100";

    // CREATE USER INTERFACE

    w = new Window("dialog", "Adobe Script Tutorial 1");
    p = w.add("panel");
    g = p.add("group");
    
    txtScaleSizeInput = g.add("edittext", undefined, textNewScale, {
        borderless: "true",
        name: textNewScale,
        
    });

    txtScaleSizeInput.preferredSize = [100, -1];
    g.alignChildren = "center";
   
    g = w.add("group");
    g.alignChildren = "center";
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");
    


    // UI EVENT HANDLERS
    btnOk.onClick = function () {
        w.close(1);
    };

    btnCancel.onClick = function () {
        w.close(0);
    };

    // SHOW THE WINDOW
    // w.show();

    if (w.show() == 1) {
        // alert("OK was clicked");
        process();
    }

    function process() {
            if ( app.documents.length > 0 ) {
                var doc = app.activeDocument;
                for ( i = 0; i < activeDocument.selection.length; i++ ) {
                    var scaleX = scaleY = Number(txtScaleSizeInput.text);
                    activeDocument.selection[i].resize(scaleX, scaleY, undefined, undefined, undefined, undefined, undefined, Transformation.CENTER);
                }
            }
    }

})();

 

2 replies

femkeblanco
femkeblancoCorrect answer
Legend
June 25, 2022

I've made two changes to your script. 

(1)  I've got the input from the eddittext box by using txtScaleSizeInput.text.

(2)  I've used the resize() function instead of matrices.

 

(function () {

    // The script code goes inside this anonymous function.
    // Reusable UI variables
    var g; // group
    var p; // panel
    var w; // window

    // Permanent UI variables
    var btnCancel;
    var txtScaleSizeInput;
    var btnOk;
    var txtFolderInput;
    var textNewScale = "100";

    // CREATE USER INTERFACE

    w = new Window("dialog", "Adobe Script Tutorial 1");
    p = w.add("panel");
    g = p.add("group");
    
    txtScaleSizeInput = g.add("edittext", undefined, textNewScale, {
        borderless: "true",
        name: textNewScale,
        
    });

    txtScaleSizeInput.preferredSize = [100, -1];
    g.alignChildren = "center";
   
    g = w.add("group");
    g.alignChildren = "center";
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");
    


    // UI EVENT HANDLERS
    btnOk.onClick = function () {
        w.close(1);
    };

    btnCancel.onClick = function () {
        w.close(0);
    };

    // SHOW THE WINDOW
    // w.show();

    if (w.show() == 1) {
        // alert("OK was clicked");
        process();
    }

    function process() {
            if ( app.documents.length > 0 ) {
                var doc = app.activeDocument;
                for ( i = 0; i < activeDocument.selection.length; i++ ) {
                    var scaleX = scaleY = Number(txtScaleSizeInput.text);
                    activeDocument.selection[i].resize(scaleX, scaleY, undefined, undefined, undefined, undefined, undefined, Transformation.CENTER);
                }
            }
    }

})();

 

Mohkhasa
MohkhasaAuthor
Participating Frequently
June 25, 2022

Wow, that was fast. thank you so much for the help!

 

another question tho. would be possible when you run the script the text field is already highlighted? so you can immediately enter the numbers

femkeblanco
Legend
June 25, 2022

Yes, change the default value (line 14) to an empty string

var textNewScale = "";

and add this after you have created the edittext box to make it active

txtScaleSizeInput.active = true;

 

Mohkhasa
MohkhasaAuthor
Participating Frequently
June 25, 2022

Thank you for your reply, the script above works fine if you change "textNewScale" to a number (e.g. 50 for 50%). but that will make it fixed, I want the user to input the desired scaling percentage but I can't figure out how.