To answer how to adapt the code to work with a range of subpaths. It may be more a matter of style but I wouldn't. I would modify that( or similar code ) into a function that creates a new path from one subpath. I then would create another function that can work with a range of subpaths by calling the first function as many time as needed. For example( it would be better if there was more error checking )
- Code: Select all
#target photoshop
splitSubPaths( app.activeDocument.pathItems[0], 0, 1 );// assumes there is an open document and the first pathItem has two subpaths.
/*///////////////////////////////////////////////////////////////////////////////////////////////////////
// Function: makeNewPathFromSubpath
// Usage: creates a new path from subpath
// Input: pathItem, Number-index of subpath, String(optional)
// Return: pathItem - the new pathIems or -1 if error
// Notes: adapted from code by c.pfaffenbichler
// http://forums.adobe.com/thread/965359?tstart=0
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
function makeNewPathFromSubpath( thePath, subPathIndex, pathName ){
try{
var theSubPath = thePath.subPathItems[subPathIndex];
var lineSubPathArray = new Array ();
var lineArray = new Array ();
for (c = 0; c < (theSubPath.pathPoints.length); c++) {
lineArray[c] = new PathPointInfo;
lineArray[c].kind = theSubPath.pathPoints[c].pointKind;
lineArray[c].anchor = theSubPath.pathPoints[c].anchor;
lineArray[c].leftDirection = theSubPath.pathPoints[c].leftDirection;
lineArray[c].rightDirection = theSubPath.pathPoints[c].rightDirection;
}
lineSubPathArray[0] = new SubPathInfo();
lineSubPathArray[0].closed = theSubPath.closed;
lineSubPathArray[0].operation = theSubPath.operation;
lineSubPathArray[0].entireSubPath = lineArray;
undefined == pathName ? pathName = 'temp':pathName;
return app.activeDocument.pathItems.add(pathName, lineSubPathArray);
}catch(e){ return -1;}
};
/*///////////////////////////////////////////////////////////////////////////////////////////////////////
// Function: splitSubPaths
// Usage: creates new paths from subPaths
// Input: pathItem, Number, Number
// Return: -1 if error
// Notes:
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
function splitSubPaths( thePath, startSubPathIndex, endSubPathIndex ){
try{
for( var sp = startSubPathIndex; sp<= endSubPathIndex;sp++ ){
var tempPath = makeNewPathFromSubpath( thePath, sp,thePath.name+'_'+sp );
}
}catch(e){ return -1;}
};
To try to explain paths in general. What can be scripted when working with paths is very limited. You can work around some of those limits by extracting the path info and creating new paths from that data, modifying it if needed.
When working with existing paths you work with what Adobe call collections. Collections are similar to arrays but are not the same. They can be worked by index like arrays but have different methods. The collections used for paths are pathItems, subPathItems, and pathPoints. The pathItems collection is a collection of path objects. subPathItems is a collection of subPaths of a path. etc. A path can have one or more suppaths. A subPath can have more or more pathPoints.
When creating a path you do work with arrays. You need at least one array to hold the subPathInfo object. That object has properties for pathType(open/closed), pathOperation( add, subtract,etc ) and an array of pathPointInfo object. pathPointInfo objects have properties for kind( smooth, corner ), and arrays for anchor, left handle, and right handle.
It takes some working with paths to get a handle on all that is involved. But it helps to remember that existing paths use collections of objects describing the existing path. To create a path you use arrays of objects that describe the path you want to create.
One final note. Some subPaths also have subPaths( I think those paths come from Illustrator or other vector apps ). The DOM does not work with those 'subSubPaths'. So it is possible that you can run into a path that you can't extract all the data from.