Skip to main content
hilukasz
Known Participant
September 4, 2012
Answered

add page numbers via script

  • September 4, 2012
  • 1 reply
  • 2059 views

I want to try to add page numbers to artboard based on position of selected text. I saw someone had made a script similar to this, but I need to do it based on selected text. Here is my solution, but for some reason it is not adding it based on artboard coordinates. I ran into this a while ago, and setting the rule to artboards fixed it, here it does not seem to have same effect.

#target illustrator

// global variables

////////////////////////////////////////////////////

// Set up varaibles

////////////////////////////////////////////////////

var pageNumber = app.activeDocument.artboards.getActiveArtboardIndex() + 1,

    doc = app.activeDocument;

//make sure only 1 item is selected

if(selectedItems.length > 1 || selectedItems.length < 1) {

    alert('please select only one item'}

else {

    var selectionTopPosition = doc.selection[0].top,

        selectionLeftPosition = doc.selection[0].left;

    doc.selection[0].remove();

    doc.selection = null;

    //remove selection

    //alert(selectionLeftPosition+" "+selectionTopPosition);

}

//create page numbers layer

var pageNumbersLayer = doc.layers.add();

pageNumbersLayer.name = "page numbers";

var pageNumbersLayerGroup = pageNumbersLayer.groupItems.add();

// main loop

//loop through artboards

for(i = 0; i < app.activeDocument.artboards.length; i++){

    app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;

    doc.artboards.setActiveArtboardIndex(i);

    alert('active artboard is '+i);

    var pageNumber = doc.artboards.getActiveArtboardIndex() + 1;

    var pageCount = i + 1;

    var textRef = pageNumbersLayerGroup.textFrames.add();

    textRef.contents = pageCount.toString();

    textRef.top = selectionTopPosition;

    textRef.left = selectionLeftPosition;

    pageCount++;

}

This topic has been closed for replies.
Correct answer Larry G. Schneider

Have you tried to set the coordinate system for the specific doc.

Actually it worked for me with this

doc = app.activeDocument;

selectedItems = doc.selection;

I started with a single string (text) at the number location on artboard 1.

1 reply

Larry G. Schneider
Community Expert
Larry G. SchneiderCommunity ExpertCorrect answer
Community Expert
September 4, 2012

Have you tried to set the coordinate system for the specific doc.

Actually it worked for me with this

doc = app.activeDocument;

selectedItems = doc.selection;

I started with a single string (text) at the number location on artboard 1.

hilukasz
hilukaszAuthor
Known Participant
September 5, 2012

oh wow, I'm a dummy! I somehow forgot to add that variable back in when I was piecing it together since I was testing it in pieces. thanks for the extra set of eyes! fixed it.

also I had to push the line:

app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;

into the first conditional, this is what was originally messing it up. I realized I got the position too late with wrong coordinate system. works perfect now.

ZebraTyme
Participant
December 3, 2016

How can I get my script to note artboard coordinates and not document coordinates? This solves my problem, thank you.