How would you write a script to move all points of all selected paths to the nearest (non-pixel grid) grid point?

Sample before and after alignment

e.g. I want all of the points of each polygon/path to snap to the nearest point on a custom grid. Mine is setup as 1 grid unit = 11.338px or 4mm, but ideally the script would extend to any unit value


// set grid units as pixels - in my case 11.338px = 8mm = 1 grid unit

units = 11.338 

// Nested loop through all polygons/paths, then all points in each polygon

For each Polygon {

    For each Point P in Polygon {

    // move the Point x, y to the nearest grid point in units
        P.x = units * Round(P.x/units) 
        P.y = units * Round(P.y/units)

Does this make sense? It's been a while since I've done any programming. I've also never scripted for Illustrator, so not exactly sure how to implement this.

LINK: I've looked at the post by @KromStern but not sure how to implement based on that thread. How to align all selected points to a grid?

1 Answer 1


Here you go, make sure the paths that you want to snap are selected:

  // jooSnapToDocumentGrid.jsx

#target illustrator


function main(){
    var grid = getDocumentGrid();
    snapSelectedPathToDocumentGrid(activeDocument.selection, grid); 

function snapSelectedPathToDocumentGrid(sel, grid){
    for(var i = 0; i < sel.length; i++){
        try {
            var pp = sel[i].pathPoints;
            for(var j = 0; j < pp.length; j++){
                var p = pp[j];
                p.leftDirection = nearestGrid(p.leftDirection, grid);
                p.rightDirection = nearestGrid(p.rightDirection, grid);
                p.anchor = nearestGrid(p.anchor, grid);       
        } catch(err) { }

function getDocumentGrid(){
    var prf = app.preferences;
    var ticks = prf.getIntegerPreference('Grid/Horizontal/Ticks');
    var spacing = prf.getRealPreference('Grid/Horizontal/Spacing');
    return spacing/ticks;    

function nearestGrid(anchor, grid) {
    return [Math.round(anchor[0] / grid) * grid, 
            Math.round(anchor[1] / grid) * grid ];
  • This is great, @joojaa! Thank you for the code! I've tested your script on a few polygons as a selection and it works flawlessly, aligning points to the grid. Unfortunately, when I select my hundreds of polygons, I'm getting an error message (Error:21: undefined is not an object, Line 15) >> I'm guessing there must be a problem with my selection (e.g. an unclosed path or some non path object that I am not seeing on my artboard that is not a valid object for your script. Will have to take a closer look. Thanks again, you rock! Dec 17, 2015 at 16:36
  • @Johnnystackexchange Yeah well i dont check for object types you should add check to see if the selected object is a path Item or wrap a try catch block around the loop.
    – joojaa
    Dec 17, 2015 at 16:39
  • @Johnnystackexchange try now didn't do any testing on it
    – joojaa
    Dec 17, 2015 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.