Copy link to clipboard
Copied
Ok, I was fed up with the slices in Photoshop scripting, since they seem to discourage a lot of people, considering how many posts on this community and outside don't have a proper solution.
After a few wrong dives I did find the descriptor nesting of some basic slice info, that can be used for exports, selecting with descriptors by ID and other needs. To simplify the matter for the next poor guy facing the problem of programatically finding it, I am sharing my function:
/**
* Retrieve list of slice info with ActionManager's descriptors as normal objects
*
* A slice descriptor may expose these but not always all values
*
* sliceID : DescValueType.INTEGERTYPE => stored as id [number]
* groupID : DescValueType.INTEGERTYPE => stored as group [number]
* origin : DescValueType.ENUMERATEDTYPE => stored as origin [string]
* name : DescValueType.STRINGTYPE => stored as name [string]
* type : DescValueType.ENUMERATEDTYPE => stored as type [string]
* bounds : DescValueType.OBJECTTYPE => stored as top,left,bottom,right [UnitValues]
* --- Bottom info was skipped, since I did not need it for now but you can see the descriptor API documentation for how to handle other types ---
* url : DescValueType.STRINGTYPE
* null : DescValueType.STRINGTYPE
* message : DescValueType.STRINGTYPE
* altTag : DescValueType.STRINGTYPE
* cellTextIsHTML : DescValueType.BOOLEANTYPE
* cellText : DescValueType.STRINGTYPE
* horzAlign : DescValueType.ENUMERATEDTYPE
* vertAlign : DescValueType.ENUMERATEDTYPE
* bgColorType : DescValueType.ENUMERATEDTYPE
* topOutset : DescValueType.INTEGERTYPE
* leftOutset : DescValueType.INTEGERTYPE
* bottomOutset : DescValueType.INTEGERTYPE
* rightOutset : DescValueType.INTEGERTYPE
*
* @Param {Document} doc the document to work on
*/
function getSliceList(doc){
var original_doc = activeDocument;
activeDocument = doc;
// slices are reachable via the document descriptor
var doc_ref = new ActionReference();
doc_ref.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
doc_descriptor = executeActionGet(doc_ref);
// the doc descriptor has a reference to the slice list
slice_descriptors = doc_descriptor.getObjectValue(stringIDToTypeID("slices")).getList(stringIDToTypeID("slices"));
// prepare a list to return
var slice_list = new Array();
for(var s = 0; s < slice_descriptors.count; s++){
// each slice's info is again a aggreagation of descriptors
var current_slice = slice_descriptors.getObjectValue(s);
// the bounds are even on a lower level
var slice_bounds = current_slice.getObjectValue(stringIDToTypeID("bounds"));
// for simpler processign we store individual slice info as an object
var slice_object = {
id: current_slice.getInteger(stringIDToTypeID("sliceID")),
group: current_slice.getInteger(stringIDToTypeID("groupID")),
type: typeIDToStringID(current_slice.getEnumerationValue(stringIDToTypeID("type"))),
origin: typeIDToStringID(current_slice.getEnumerationValue(stringIDToTypeID("origin"))),
// since names are optional, we start with placeholder
name: "NO NAME",
// we store bounds as unitvalues to keep it ready for processing
top: new UnitValue(slice_bounds.getInteger(stringIDToTypeID("top")), "px"),
left: new UnitValue(slice_bounds.getInteger(stringIDToTypeID("left")), "px"),
bottom: new UnitValue(slice_bounds.getInteger(stringIDToTypeID("bottom")), "px"),
right: new UnitValue(slice_bounds.getInteger(stringIDToTypeID("right")), "px")
};
// proccess optional values
if (current_slice.hasKey(stringIDToTypeID("name"))){
slice_object.name = current_slice.getString(stringIDToTypeID("name"));
}
// store object in list
slice_list.push(slice_object);
}
// switch to original document
activeDocument = original_doc;
// return the list
return slice_list;
}
Copy link to clipboard
Copied
Thank you for sharing your time and knowledge to help the scripting community, you contribution is greatly appreciated!
Copy link to clipboard
Copied
Hi all. Just wondering if anyone managed to run the code above? When I try to do it, a warning with no text shows up. After closing it photoshop crashes.
The problem is with this line:
doc_descriptor = executeActionGet(doc_ref);
I've tried it on PS 2021 and 2022. Maybe something has changed when it comes to getting the document descriptor?
Thank you in advance.
Copy link to clipboard
Copied
Hey,
not sure if it is still relevant but just copy pasted my upper code and run it in 2021 as getSliceList(activeDocument) as a quick test. It works normally, no errors. Try to catch the error:
try{
// your problematic doc
getSliceList(doc);
}catch(e){
// for more errror printing see the error object properties: e.message, e.line, e.filename, ...
alert(e);
}