Copy link to clipboard
Copied
Dear all, please help me on this
Need Illustrator scipt to round number at x and y cordinate, so that I can export artboards with exact dimension
Thanks @Ton Frederiks. In which case, this should round the left and top positions of artboards.
var ABs = app.activeDocument.artboards;
for (var i = 0; i < ABs.length; i++) {
var ABR = ABs[i].artboardRect;
var dx = Math.round(ABR[0]) - ABR[0];
var dy = Math.round(ABR[1]) - ABR[1];
ABs[i].artboardRect = [
Math.round(ABR[0]), Math.round(ABR[1]),
ABR[2] + dx, ABR[3] + dy
];
}
Copy link to clipboard
Copied
Rearrange All Artboards will align them to round pixel coordinates. You could make an action and record: Object > Artboards > Rearrange All Artboards.
Copy link to clipboard
Copied
Rearranging all artboards disrub my visual placement of other artobards as well, looking something will work on selected artboards by the script
Copy link to clipboard
Copied
The X and Y cordinates don't actually affect the size of your output. However, you can use the Align tool to align your artboard(s) to another artboard (Select both artboards and the align options appear)
Copy link to clipboard
Copied
The X and Y cordinates don't actually affect the size of your output. However, you can use the Align tool to align your artboard(s) to another artboard (Select both artboards and the align options appear)
By @chrisg11235813
They do, if they contain fractions, rounding will occur and additional pixels will be generated.
Copy link to clipboard
Copied
I always figured that when you move the X or Y positions, the Width and Height shift with the dimentions remaining static (?)
Copy link to clipboard
Copied
You seem to be using pixels. Illustrator scripting uses points. Converting points to pixels involves multiplying by a repeating decimal and you'll always end up with a decimal. So what you want is not possible.
Copy link to clipboard
Copied
1 point in Illustrator equals 1 pixel.
The default is 72 ppi which is the same as 72 points in an inch.
No complex conversions are necessary.
Copy link to clipboard
Copied
Thanks @Ton Frederiks. In which case, this should round the left and top positions of artboards.
var ABs = app.activeDocument.artboards;
for (var i = 0; i < ABs.length; i++) {
var ABR = ABs[i].artboardRect;
var dx = Math.round(ABR[0]) - ABR[0];
var dy = Math.round(ABR[1]) - ABR[1];
ABs[i].artboardRect = [
Math.round(ABR[0]), Math.round(ABR[1]),
ABR[2] + dx, ABR[3] + dy
];
}
Copy link to clipboard
Copied
Perfect @femkeblanco works like a charm!
Copy link to clipboard
Copied
And it is a much better solution than Rearranging Artboards, which does not work when there is only a single artboard and it shuffles the artboards around which is (as j.khakase mentioned) not always what you want.
Copy link to clipboard
Copied
@femkeblanco I was too quick calling it perfect 😞
It is almost perfect...
It does round the coordinates, but leaves the artwork in the original place, which can give faint anti-aliased pixels at the edges.
Rearrange Artboards has the option to Move artwork with Artboard. If that could be part of the script, it would be perfect.
Copy link to clipboard
Copied
@Ton Frederiks Unfortunately, there is no quick fix for this. Artwork will have to be moved item by item.
Copy link to clipboard
Copied
Would resetting the rulers have the same effect as setting the top left corner to a round number?
Copy link to clipboard
Copied
@chrisg11235813 Yes, but only for that particular artboard, other artboards can be still off. Rearrange Artboards fixes it for all artboards.
Copy link to clipboard
Copied
@femkeblanco That's unfortunate, you were so close to the perfect solution.
Copy link to clipboard
Copied
Thank you so much @femkeblanco and all for your valuable comments.
The above pice of code works fine for me. and I get the exact what I want.
Copy link to clipboard
Copied
Be careful, it is perfect for empty artboards, but check the edges if there is artwork on the artboards.
Copy link to clipboard
Copied
I just observe one more thing, the artwork doesn't move even respective check mark selected before run the script. and get 1 and half pixcel gap at edges as shown below image
Copy link to clipboard
Copied
i think you're thinking about mm here. there's no clean conversion from pt to mm.
Copy link to clipboard
Copied
It might be too late for this, but here is a video that might work. you can quickly "snap to" the pixel grid. moving artwork with your artboards.
Copy link to clipboard
Copied
basically, select artboards. Object>Make Pixel Perfect
Copy link to clipboard
Copied
The problem with this is that it not only moves but resizes your artboards. A 540 pixel artboard not aligned to the grid can become a 541 pixel artboard when made pixel perfect.