Skip to main content
wckdtall
Inspiring
February 21, 2024
Question

Check/Create Guides If They Don't Exist - Photoshop Scripting

  • February 21, 2024
  • 3 replies
  • 385 views

I created a script that creates guides in the current document, the problem was if you don't check to see if they exist first, it'll keep making the same guide over and over again. Was trying to use switch cases, for logic on horizontal vs vertical, and may have gotten a bit too verbose, looping through multiple arrays.

Is there a more straight forward way to accomplish checking the document for guides and creating if they don't exist?

 

 

function psGuideCheck(guideToCheck, gDir) {
    var aDG = app.activeDocument.guides;
    var gResult = false;
    if (aDG.length > 0) {
        for (var i = 0; i < aDG.length; i++) {
            if (guideToCheck == Math.round(aDG[i].coordinate.as('px')) && gDir == aDG[i].direction) {
                var gResult = true;
                break;
            }
        }
    }
    return gResult;
}
function psCreateGuides(cropLeft, cropTop, cropRight, cropBottom) {
    var actDocG = app.activeDocument.guides;
    var yLoc = [Math.round(cropTop), Math.round(cropBottom)];
    var xLoc = [Math.round(cropLeft), Math.round(cropRight)];
    var yDir = Direction.HORIZONTAL;
    var xDir = Direction.VERTICAL;
//Loop Through Horizontal guides
    for (var i = 0; i < yLoc.length; i++) {
        var yPxGd = UnitValue(yLoc[i], "px").as('px'); //converts to the right value, and removes " px" from the end
        if (psGuideCheck(yPxGd,yDir) == false) actDocG.add(yDir, UnitValue(yLoc[i], "px"));
    }
//Loop Through Vertical Guides
    for (var i = 0; i < xLoc.length; i++) {
        var xPxGd = UnitValue(xLoc[i], "px").as('px'); //converts to the right value, and removes " px" from the end
        if (psGuideCheck(xPxGd,xDir) == false) actDocG.add(xDir, UnitValue(xLoc[i], "px"));
    }
}
psCreateGuides(-75, -75, 75, 75);

 

 

 

This topic has been closed for replies.

3 replies

Legend
February 22, 2024

guides.removeAll()

Then create the four you need.

c.pfaffenbichler
Community Expert
Community Expert
February 22, 2024

This should be slightly quicker, using AM-code would probably speed it up a bit more.

// 2024, use at your own risk;
if (app.documents.length > 0) {
var myDocument = activeDocument;
var theGuides = myDocument.guides;
var theString = new String;
for (var n = 0; n < myDocument.guides.length; n++) {
    theString = theString+"\n"+String(theGuides[n].direction)+", "+String(Number(theGuides[n].coordinate))
};
var newGuides = [[Direction.VERTICAL, -75], [Direction.HORIZONTAL, -75], [Direction.VERTICAL, 75], [Direction.HORIZONTAL, 75]];
for (var x = newGuides.length-1; x >= 0; x--) {
    if (theString.match(newGuides[x][0]+", "+newGuides[x][1]) == null) {
        myDocument.guides.add(newGuides[x][0], UnitValue(newGuides[x][1], "px"))
    }
};
};

 

wckdtall
wckdtallAuthor
Inspiring
February 23, 2024

Thanks! I wanted to see if I could do it with AM, but I'm not sure how to create a function that would return a true false for existing guides.

Legend
February 23, 2024
var g = app.activeDocument.guides;
for(var i = 0; i < g.length; i++){
   Window.alert(g[i].direction + g[i].coordinate);
   }

I'd probably sort by direction then coordinate and see if any of my new guides matched.

c.pfaffenbichler
Community Expert
Community Expert
February 22, 2024

You could test if this runs quicker. 

edited

// 2024, use at your own risk;
var myDocument = activeDocument;
var theGuides = myDocument.guides;
var newGuides = [[Direction.VERTICAL, -75], [Direction.HORIZONTAL, -75], [Direction.VERTICAL, 75], [Direction.HORIZONTAL, 75]];
for (var x = newGuides.length-1; x >= 0; x--) {
    var theCheck = false;
    var theCount = theGuides.length-1;
    while (theCheck == false && theCount >= 0) {
        if (theGuides[theCount].direction == newGuides[x][0] &&  Number(theGuides[theCount].coordinate) == newGuides[x][1]) {
            newGuides.splice(x,1);
            theCheck = true
        };
        theCount--
    }
};
for (var n = 0; n < newGuides.length; n++) {
    myDocument.guides.add(newGuides[n][0], UnitValue(newGuides[n][1], "px"))
};