• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

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

Enthusiast ,
Feb 21, 2024 Feb 21, 2024

Copy link to clipboard

Copied

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);

 

 

 

TOPICS
Actions and scripting

Views

127

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Feb 22, 2024 Feb 22, 2024

Copy link to clipboard

Copied

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"))
};

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2024 Feb 22, 2024

Copy link to clipboard

Copied

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"))
    }
};
};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 23, 2024 Feb 23, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2024 Feb 23, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 23, 2024 Feb 23, 2024

Copy link to clipboard

Copied

LATEST

So simple, match and using one stored string makes a lot of sense since I'm just looking for duplicates, and it's still being very specific what to look for on creation. My mind always jumps to non existent extendscript functions like in_array,  but this accomplishes the same with the tools given. Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 22, 2024 Feb 22, 2024

Copy link to clipboard

Copied

guides.removeAll()

Then create the four you need.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines