Skip to main content
rcraighead
Legend
March 16, 2017
Question

Script to set artboard to selected art, etc.

  • March 16, 2017
  • 1 reply
  • 4568 views

In my new work I REALLY need to learn Javascript for AI. I am trying to read through the official guide but am impatient.

Can someone create a script that would:

  1. Create rectangle based on user input
  2. Set artboard to rectangle size
  3. Scale rectangle horizontally to 1/4 current size and aligned to right

There is MUCH more I would like the script to do, but I realize I need to learn something and not just ask for someone to do it.

I'm also interested in suggestions for learning Javascript for AI.

Also, what resource would you suggest for hiring a freelance AI/Javascript guru?

Thank you,

Ray

This topic has been closed for replies.

1 reply

Inspiring
March 17, 2017

Hey!

Here's an example that should do those three things, I've added some (pretty sparse) comments but let me know if you have questions about any parts in particular.

#target illustrator

function test(){

    // if there are no documents open, alert the user and exit

    if( app.documents.length == 0 ) {

        alert( "Please open a document!" );

        return;

    }

   

    // get a ref to the current document

    var doc = app.activeDocument;

    var artboard = doc.artboards[0];

   

    // flag for user input

    var validInput = false;

   

    var promptMessage = "Please enter the size of the rectangle in inches";

    var dims, rectWidth, rectHeight;

   

    // this section of code will be repeated until either of the two flags are set equal to true

    while( validInput == false ) {

   

        // prompt for the dimensions

        dims = prompt( promptMessage, "8.5 x 11" );

       

        // if the dims var is equal to null, assume the user clicked the cancel button and exit early

        if( dims === null ) {

            return;

        }

       

        // otherwise, assume the user clicked OK, try to parse the input

        else {

           

            // turn the string into an array, splitting on the letter 'x'

            dims = dims.split('x');

           

            // if there are only two elements in the array

            if( dims.length == 2 ) {

           

                // try to convert them to number values

                rectWidth = parseFloat( dims[0] );

                rectHeight = parseFloat( dims[1] );

               

                // if neither value is equal to NaN (not a number), then set the flag to let us out of the loop

                if( !isNaN( rectWidth ) && !isNaN( rectHeight ) )

                    validInput = true;

            }

        }

       

        // if we have to prompt again, update the message to have some more info

        promptMessage = "Please enter the size of the rectangle in inches\nValues should be in the format '<width> x <height>'"

    }

   

    // if we made it out of the loop, assume that the values are valid numbers!

   

    // convert the numbers to points and make sure they are both positive

    rectWidth = Math.abs( rectWidth * 72 );

    rectHeight = Math.abs( rectHeight * 72 );

   

    // get current artboard dimensions

    var bounds = artboard.artboardRect;

   

    // add new rectangle                                    top                   left                  width           height

    var rect = doc.pathItems.rectangle( bounds[0], bounds[1], rectWidth, rectHeight );

    artboard.artboardRect = rect.geometricBounds;

   

    // scale rectangle, numbers

    rect.resize(

            25,                // scale horizontal in %

            100,               // scale vertical in %

            true,              // change position

            false,             // change Fill Pattern

            false,             // change Fill Gradient

            false,             // change Stroke Pattern

            1,                 // change Line Widths

            Transformation.RIGHT );  // scale about which point

   

}

test();

This script has examples of properties and functions, as well as conditional statements and loops. All of the functions and properties, such as the 'documents' property of the App class and the 'rectangle' function of the PathItems class, should be listed in the "Scripting Reference: Javascript" PDF.

How experienced are you with JavaScript in general? If you are still relatively new, I would suggest digging into that first. It will make the AI-specific docs much more digestible. There are some good intro web resources like khan academy​, code academy​, etc. that help with basic syntax and structure.

Good luck, have a good weekend!

rcraighead
Legend
March 18, 2017

zertle,

Thank you so much for taking the time to write this and give me some guidance on learning Javascript. I have no experience with Javascript so your suggestions are on target. I'll run the script and learn from it then get back with you.

I really appreciate it.

CarlosCanto
Community Expert
Community Expert
March 18, 2017

Hi Ray, check the resources and advice given to other users over the years. In a nutshell, learn javascript somewhere else (links in the threads below), follow the samples in the official guides and best of all, use this forum. Ask questions if you get stuck.

Any great Javascript training available online?

Formal Training?

Re: Learning Javascript

Documentation: What are your go-to guides and docs?