Skip to main content
Participating Frequently
April 22, 2025
Question

How to Access Guides or Slices via Script in Photoshop?

  • April 22, 2025
  • 2 replies
  • 295 views

Hi everyone,

I'm working on a script in Photoshop that aims to automatically split a document into separate layers or images based on either guides or slices. I can manually create the guides or slices, and I can see them in the document, but when I try to access them via script, it doesn't seem to work.

Here's what I've tried:

  • I confirmed that guides or slices are already present in the PSD.

  • I attempted to loop through document properties or use ActionManager code to get guides or slices.

  • For guides, I'm not sure how to access their positions.

  • For slices, I couldn't find any scripting API that allows access at all.

My questions:

  1. Is it possible to read guides via script (ExtendScript or UXP)?
  2. Can I access slice information via script in any way?
  3. If not, is there a workaround to split regions based on guides?

Any advice or code snippets would be greatly appreciated!

Thanks in advance!

2 replies

c.pfaffenbichler
Community Expert
Community Expert
April 22, 2025
// 2025, use at your own risk; 
if (app.documents.length > 0 && app.activeDocument.guides.length > 0) {
var theGuides = getGuidesSquare();
alert ("\nvertical guides\n"+theGuides[0].join("\n")+"\n\nhorizontal guides\n"+theGuides[1].join("\n"))
};
////// get guides //////
function getGuidesSquare () {
// set to pixels;
var myDocument = app.activeDocument;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// collect guides;
var theVerticalArray = new Array;
var theHorizontalArray = new Array;
for (var m = 0; m < myDocument.guides.length; m++) {
if (myDocument.guides[m].direction == Direction.HORIZONTAL) {theHorizontalArray.push(Number(myDocument.guides[m].coordinate))}
else {theVerticalArray.push(Number(myDocument.guides[m].coordinate))}
};
theHorizontalArray.sort(sortByDate);
theVerticalArray.sort(sortByDate);
app.preferences.rulerUnits = originalRulerUnits;
return [theVerticalArray, theHorizontalArray];
function sortByDate(a,b) {
if (Number(a)<Number(b)) return -1;
if (Number(a)>Number(b)) return 1;
return 0;
};
};