Copy link to clipboard
Copied
Hi All,
I'm new to the illustrator scripting.
But out of my one month exposure to the scripting have explored plenty of things and started my project of writing script to print X,Y coordinates of different objects in an ai file.
My ai file would have different artboards and only for one artboard my script is properly printing the X,Y coordinates properly.
For all other artboards it is taking the coordinate system from the ai file and hence printing wrong.
Can somebody help me to sort this issue out.
Below is my script.
I couldn't attach the .ai file, with this, happy to share my sample file which I am trying to run the script.
Please correct me what is wrong in my script.
// Create an empty dialog window near the upper left of the screen
var dlg = new Window('dialog', 'Design Layout ');
dlg.frameLocation = [100,100];
dlg.size = [250,300];
dlg.intro = dlg.add('statictext', [20,20,150,50] );
dlg.intro.text = '"Select 1 (or) more items"';
var touchAreaCheckBox = dlg.add ("checkbox", [20,50,150,70], " Touch Areas");
dlg.GenerateButton = dlg.add('button', [20,170,150,200], 'Generate', 'CenterCoord');
// number of decimal places
var decimals = 0;
// document
var doc = activeDocument;
function generateDimensions()
{
var totalLayers = doc.layers.length;
createLayer('Touch Area Coordinates');
for ( var i = 0 ; i < totalLayers ; i++)
{
var currentLayer = doc.layers;
if (currentLayer.name == "Touch Areas")
{
currentLayer.hasSelectedArtwork = true;
break;
}
else
{
//TO DO
}
//Unlock the layer if needed
currentLayer.locked = false;
}
getLeftCoord();
}
function createLayer(layerName)
{
try
{
var speclayerCenter =doc.layers[layerName];
}
catch(err)
{
var speclayerCenter = doc.layers.add();
speclayerCenter.name = layerName;
}
}
// measurement line color
var color = new RGBColor;
color.green = 255;
color.blue = 0;
/**
Start the spec
*/
function getLeftCoord()
{
$.writeln(" doc.selection.length:::::",doc.selection.length);
for (var iSelectedItems=0; iSelectedItems<doc.selection.length; iSelectedItems++)
{
var topLeftPosDoc= doc.selection[iSelectedItems].position;
// To get the center coordinates
var topLeftPosArt = doc.convertCoordinate (topLeftPosDoc, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);
var cenPosArt = topLeftPosArt;
cenPosArt[0] = topLeftPosArt[0] + doc.selection[iSelectedItems].width * 0.5;
cenPosArt[1] = Math.abs(topLeftPosArt[1] - (doc.selection[iSelectedItems].height * 0.5));
//specLabel(concatenate(cenPosArt[0],cenPosArt[1]),(topLeftPosDoc[0]+doc.selection[iSelectedItems].width * 0.5),(topLeftPosDoc[1] - (doc.selection[iSelectedItems].height * 0.5)));
// To get the bottom left coordinates
var bottomLeftPosArt= doc.convertCoordinate (topLeftPosDoc, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);
bottomLeftPosArt[1] = Math.abs(bottomLeftPosArt[1] - (doc.selection[iSelectedItems].height ));
specLabelGreenForLeftCoord(concatenate(bottomLeftPosArt[0],bottomLeftPosArt[1]),topLeftPosDoc[0]+10, (topLeftPosDoc[1]- doc.selection[iSelectedItems].height));
}
dlg.close ();
}
function concatenate (value1,value2)
{
var v1 = value1;
var v2 = value2;
v1 = v1.toFixed (decimals);
v2 = v2.toFixed (decimals);
var result = v1 +" ,"+ v2 ;
return result;
}
function specLabelGreenForLeftCoord( val, x, y)
{
var t = doc.textFrames.add();
t.textRange.characterAttributes.size = 12;
var redColor = new RGBColor();
redColor.red = 0;
redColor.green = 255;
redColor.blue = 0;
t.textRange.characterAttributes.fillColor = redColor;
t.textRange.characterAttributes.alignment = StyleRunAlignmentType.center;
var v = val;
t.contents = v;
t.top = y;
t.left = x ;
return t;
}
dlg.GenerateButton.addEventListener ('click', generateDimensions);
dlg.show();
Copy link to clipboard
Copied
can you post a screenshot of the coordinates you're getting? I think i know what's going on, but i want to be sure. Are you getting 0,0 on the first artboard and then something like 800,0?
Are you trying to print the coordinates for each object on an artboard relative to the artboard that it's on?
Copy link to clipboard
Copied
I thought this was already solved, did this thread get duplicated?
Copy link to clipboard
Copied
I thought it was solved and replied to Qwerty_fly but it is not solved yet..
Copy link to clipboard
Copied
any luck with this?
Copy link to clipboard
Copied
Hi William,
Thanks for your concern.
I couldn't achieve this, instead I've written this function to ensure and prompt the user before starting any dimensioning.
***
var artBoardWidth = 800;
var artBoardHeight = 480;
function checkArtBoardOrigin()
{
var artBoards = doc.artboards;
for(i=0; i<artBoards.length; i++)
{
if (artBoards.rulerOrigin[1] < artBoardHeight)
alert("For Artboard index " + (i+1) + ", Bottom Left is at " +(480-artBoards.rulerOrigin[1]) +" Set the ruler origin to BOTTOM LEFT corner of the artboard and re-run the script") ;
}
}
***
Copy link to clipboard
Copied
you don't need a function to check the artboard origin. those are readily available numbers. i don't know how much of your original code you're still using, so i won't try to incorporate this into that, but try this. set your artboard origin variable inside the for loop and then use that for your comparison.. I tried this out and it worked for me. I'd need to see the files you're working with to know whether there are some bugs i didn't foresee. let me know how this goes..
function writeCoords(){
if (app.documents.length>0){
var docRef = app.activeDocument;
var layers = docRef.layers;
var artboards = docRef.artboards;
var x;
var y;
var coordsLayer;
var checkLayer;
var aBTop;
var aBLeft;
var write;
docRef.selection = null;
for(i=0; i< artboards.length; i++){
aBTop = artboards.artboardRect[1];
aBLeft = artboards.artboardRect[0];
try{
coordsLayer = layers.getByName("Coords");
}
catch(e){
coordsLayer = layers.add();
coordsLayer.name = "Coords";
}
try{
checkLayer = layers.getByName("Check Layer");
checkLayer.remove();
checkLayer = layers.add();
checkLayer.name = "Check Layer";
}
catch(e){
checkLayer = layers.add();
checkLayer.name = "Check Layer";
}
docRef.artboards.setActiveArtboardIndex(i);
docRef.selectObjectsOnActiveArtboard();
for (a=0; a<selection.length; a++){
var curObject = selection;
curObject.duplicate(checkLayer);
}
for (b=0; b<checkLayer.pageItems.length; b++){
x= checkLayer.pageItems.left;
y= checkLayer.pageItems.top;
var curX = Math.round(x - aBLeft);
var curY = Math.round(y - aBTop);
write = coordsLayer.textFrames.add();
write.contents = curX + ", " + curY;
write.left = x - 20;
write.top = y + 20;
}
}
checkLayer.remove();
}
}
writeCoords();
Copy link to clipboard
Copied
Hi William,
Thanks for sharing this code.
I tried this code on my .ai files today morning.
It is printing X,Y coordinates on different objects available in artboards but quite many are wrong.
Let me rephrase my requirement.
I'm thru with my script in printing the X,Y coordinates for all the objects in all artboards.
Only concern is I need to ensure if the artboard ruler is set to bottom left or not for all the artboards, if not set have to set them manually before running my script
For few artboards artboard ruler is set at top left which is an issue for my script.
So my exact requirement is to write a function which will do below.
1) Run through all the artboards in the .ai file.
2) Set the bottom left of all the artboards (artboard ruler) to 0,0
IF these above two steps are achieved then I'm through with .....which I feel is not an easy task though....:-(
Copy link to clipboard
Copied
can you post a screenshot of your file? Are you saying that some coordinates are correct and others are incorrect? (if this is the case i suspect there are clipping masks in the artwork which seem to be a problem every time i turn around).
so let me see if i understand your question. You want to write the coordinates from the bottom left corner instead of the top left? It is unnecessary to reset the ruler origin in order to get coordinates based on that point, we simply need to change the equations. it's best to always use the global rulers and simply work out the math to get the coordinates relative to the artboard.
try this version of the script (i changed the math to print coordinates based on bottom left of the artboard rather than the top left). a screenshot would still be very helpful so I can see what your artwork looks like and perhaps find other potential bugs/workarounds.
function writeCoords(){
if (app.documents.length>0){
var docRef = app.activeDocument;
var layers = docRef.layers;
var artboards = docRef.artboards;
var x;
var y;
var coordsLayer;
var checkLayer;
var aBTop;
var aBLeft;
var write;
docRef.selection = null;
for(i=0; i< artboards.length; i++){
aBBot = artboards.artboardRect[3];
aBLeft = artboards.artboardRect[0];
try{
coordsLayer = layers.getByName("Coords");
}
catch(e){
coordsLayer = layers.add();
coordsLayer.name = "Coords";
}
try{
checkLayer = layers.getByName("Check Layer");
checkLayer.remove();
checkLayer = layers.add();
checkLayer.name = "Check Layer";
}
catch(e){
checkLayer = layers.add();
checkLayer.name = "Check Layer";
}
docRef.artboards.setActiveArtboardIndex(i);
docRef.selectObjectsOnActiveArtboard();
for (a=0; a<selection.length; a++){
var curObject = selection;
curObject.duplicate(checkLayer);
}
for (b=0; b<checkLayer.pageItems.length; b++){
x= checkLayer.pageItems.left;
y= checkLayer.pageItems.top;
var curX = Math.round(x - aBLeft);
var curY = Math.round(y - aBBot);
write = coordsLayer.textFrames.add();
write.contents = curX + ", " + curY;
write.left = x - 20;
write.top = y + 20;
}
}
checkLayer.remove();
}
}
writeCoords();
Copy link to clipboard
Copied
Hi William,
You are exactly correct, I cant take a snapshot and send right now.
But we are on the same page..
Only alternative is before generating I am ensuring that the Artboard ruler is set to 0 for every artboards manually and then generating the coordinates via script.
Will share the shortest form of the image if required.
Copy link to clipboard
Copied
Ok, so here's the deal. If you're just getting the 'left' and 'top' coordinates, it will always be relative to the global rulers, which may not even be the top/left corner of your artboard[0].
I think what you're going to want to do is define a variable for the artboardRect[0] (left side) and a variable for artboardRect[1] (top).
var aBL = docRef.artboards[0].artboardRect[0];
var aBT= docRef.artboards[0].artboardRect[1];
var object = (whatever object you are targeting to get the coordinates of)
var leftBounds = object.visibleBounds[0]; // coordinate of the left edge relative to the global ruler
var topBounds = object.visibleBounds[0]; // coordinate of the top edge relative to the global ruler
then you just need to compare aBL and leftBounds.
var objectLeftCoord = leftBounds - aBL;
var objectTopCoord = aBT - topBounds;
then you would use objectLeftCoord to input the text
Does that make sense or was it complete jibberish? I'm sure my variables could have been confusing.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now