// only for open paths without curves! // only for cornerpoints! // add numbers along an open path that consists of straight segments; // 2016, use it at your own risk; #target photoshop if (app.documents.length > 0) { var myDocument = app.activeDocument; var res = myDocument.resolution; if (myDocument.pathItems.length > 0) { // check for selected path; var thePath = selectedPath2015 (); if (thePath == undefined) {thePath = myDocument.pathItems[0]}; var theStart = 100; var theEnd = 600; var theSteps = 5; // collect path information; var thePath = collectPathInfoFromDesc2012 (myDocument, thePath); //////////////////////////////////// for (var m = 0; m < thePath.length; m++) { var theSubPath = thePath; // determine the length; var theLength = 0; for (var o = 0; o < theSubPath.length - 3; o++) { var thisLength = getDistance (theSubPath[0], theSubPath[o+1][0]); theLength = theLength + thisLength; }; //////////////////////////////////// var thisDist = 0; var theTexts = new Array; var targetDist = (theLength/theSteps); var count = 0; var check = false; $.writeln("length: "+theLength+"\n"+targetDist); var thisPoint = theSubPath[count][0]; var nextPoint = theSubPath[count+1][0]; var points = [thisPoint]; var theSubtract = 0; //////////////////////////////////// while (check == false) { thisDist = getDistance (thisPoint, nextPoint); switch (true) { // if longer; case thisDist > (targetDist - theSubtract): var factor = ((targetDist - theSubtract)/thisDist); var width = nextPoint[0] - thisPoint[0]; var height = nextPoint[1] - thisPoint[1]; var newPoint = [Number(thisPoint[0])+width*factor, Number(thisPoint[1])+height*factor]; $.writeln (points.length +"___\nadd\n"+newPoint+"\n"+nextPoint+"\n"); thisPoint = newPoint; theSubtract = 0; points.push(newPoint); break; // if exact hit; case thisDist == (targetDist - theSubtract): if (points.length < theSteps-1) { points.push(nextPoint); count++; thisPoint = nextPoint; nextPoint = theSubPath[count+1][0]; } else {check = true}; break; // if shorter; case thisDist < (targetDist - theSubtract): theSubtract = theSubtract + thisDist; count++; thisPoint = nextPoint; nextPoint = theSubPath[count+1][0]; break; default: break; }; if (points.length == theSteps) {check = true}; }; // add last point; points.push(theSubPath[theSubPath.length -3][0]); // create type layers; for (var x = 0; x < points.length; x++) { theTexts.push(createTextLayer (theStart+(theEnd - theStart)/theSteps*x, points, res)) }; //////////////////////////////////// } //////////////////////////////////// } }; //////////////////////////////////// ////// collect path info from actiondescriptor, smooth added ////// function collectPathInfoFromDesc2012 (myDocument, thePath) { var originalRulerUnits = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.POINTS; // based of functions from xbytor’s stdlib; var ref = new ActionReference(); for (var l = 0; l < myDocument.pathItems.length; l++) { var thisPath = myDocument.pathItems; if (thisPath == thePath && thisPath.kind == PathKind.WORKPATH) { ref.putProperty(cTID("Path"), cTID("WrPt")); }; if (thisPath == thePath && thisPath.kind != PathKind.WORKPATH && thisPath.kind != PathKind.VECTORMASK) { ref.putIndex(cTID("Path"), l + 1); }; if (thisPath == thePath && thisPath.kind == PathKind.VECTORMASK) { var idPath = charIDToTypeID( "Path" ); var idPath = charIDToTypeID( "Path" ); var idvectorMask = stringIDToTypeID( "vectorMask" ); ref.putEnumerated( idPath, idPath, idvectorMask ); }; }; var desc = app.executeActionGet(ref); var pname = desc.getString(cTID('PthN')); // create new array; var theArray = new Array; var pathComponents = desc.getObjectValue(cTID("PthC")).getList(sTID('pathComponents')); // for subpathitems; for (var m = 0; m < pathComponents.count; m++) { var listKey = pathComponents.getObjectValue(m).getList(sTID("subpathListKey")); var operation1 = pathComponents.getObjectValue(m).getEnumerationValue(sTID("shapeOperation")); switch (operation1) { case 1097098272: var operation = 1097098272 //cTID('Add '); break; case 1398961266: var operation = 1398961266 //cTID('Sbtr'); break; case 1231975538: var operation = 1231975538 //cTID('Intr'); break; default: // case 1102: var operation = sTID('xor') //ShapeOperation.SHAPEXOR; break; }; // for subpathitem’s count; for (var n = 0; n < listKey.count; n++) { theArray.push(new Array); var points = listKey.getObjectValue(n).getList(sTID('points')); try {var closed = listKey.getObjectValue(n).getBoolean(sTID("closedSubpath"))} catch (e) {var closed = false}; // for subpathitem’s segment’s number of points; for (var o = 0; o < points.count; o++) { var anchorObj = points.getObjectValue(o).getObjectValue(sTID("anchor")); var anchor = [anchorObj.getUnitDoubleValue(sTID('horizontal')), anchorObj.getUnitDoubleValue(sTID('vertical'))]; var thisPoint = [anchor]; try { var left = points.getObjectValue(o).getObjectValue(cTID("Fwd ")); var leftDirection = [left.getUnitDoubleValue(sTID('horizontal')), left.getUnitDoubleValue(sTID('vertical'))]; thisPoint.push(leftDirection) } catch (e) { thisPoint.push(anchor) }; try { var right = points.getObjectValue(o).getObjectValue(cTID("Bwd ")); var rightDirection = [right.getUnitDoubleValue(sTID('horizontal')), right.getUnitDoubleValue(sTID('vertical'))]; thisPoint.push(rightDirection) } catch (e) { thisPoint.push(anchor) }; try { var smoothOr = points.getObjectValue(o).getBoolean(cTID("Smoo")); thisPoint.push(smoothOr) } catch (e) {thisPoint.push(false)}; theArray[theArray.length - 1].push(thisPoint); }; theArray[theArray.length - 1].push(closed); theArray[theArray.length - 1].push(operation); }; }; // by xbytor, thanks to him; function cTID (s) { return cTID || cTID = app.charIDToTypeID(s); }; function sTID (s) { return sTID || sTID = app.stringIDToTypeID(s); }; // reset; app.preferences.rulerUnits = originalRulerUnits; return theArray; }; ////// create a path from collectPathInfoFromDesc2012-array ////// function createPath2015(theArray, thePathsName) { var originalRulerUnits = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.POINTS; // thanks to xbytor; cTID = function(s) { return app.charIDToTypeID(s); }; sTID = function(s) { return app.stringIDToTypeID(s); }; var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putProperty(cTID('Path'), cTID('WrPt')); desc1.putReference(sTID('null'), ref1); var list1 = new ActionList(); for (var m = 0; m < theArray.length; m++) { var thisSubPath = theArray; var desc2 = new ActionDescriptor(); desc2.putEnumerated(sTID('shapeOperation'), sTID('shapeOperation'), thisSubPath[thisSubPath.length - 1]); var list2 = new ActionList(); var desc3 = new ActionDescriptor(); desc3.putBoolean(cTID('Clsp'), thisSubPath[thisSubPath.length - 2]); var list3 = new ActionList(); for (var n = 0; n < thisSubPath.length - 2; n++) { var thisPoint = thisSubPath; var desc4 = new ActionDescriptor(); var desc5 = new ActionDescriptor(); desc5.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[0][0]); desc5.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[0][1]); desc4.putObject(cTID('Anch'), cTID('Pnt '), desc5); var desc6 = new ActionDescriptor(); desc6.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[1][0]); desc6.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[1][1]); desc4.putObject(cTID('Fwd '), cTID('Pnt '), desc6); var desc7 = new ActionDescriptor(); desc7.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[2][0]); desc7.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[2][1]); desc4.putObject(cTID('Bwd '), cTID('Pnt '), desc7); desc4.putBoolean(cTID('Smoo'), thisPoint[3]); list3.putObject(cTID('Pthp'), desc4); }; desc3.putList(cTID('Pts '), list3); list2.putObject(cTID('Sbpl'), desc3); desc2.putList(cTID('SbpL'), list2); list1.putObject(cTID('PaCm'), desc2); }; desc1.putList(cTID('T '), list1); executeAction(cTID('setd'), desc1, DialogModes.NO); // name work path; var check = false; var x = activeDocument.pathItems.length - 1; while (check == false) { if (activeDocument.pathItems.kind == PathKind.WORKPATH) { app.activeDocument.pathItems.name = thePathsName; var myPathItem = app.activeDocument.pathItems; check = true }; x-- }; app.preferences.rulerUnits = originalRulerUnits; return myPathItem }; ////// determine selected path, updated 09.2015 ////// function selectedPath2015 () { try { var ref = new ActionReference(); ref.putProperty (stringIDToTypeID("property"), stringIDToTypeID("targetPathIndex")); ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); var docDesc = executeActionGet(ref); return app.activeDocument.pathItems[docDesc.getInteger(stringIDToTypeID("targetPathIndex"))] } catch (e) {return undefined} }; ////// get a distance between two points ////// function getDistance (pointOne, pointTwo) { // calculate the triangle sides; var width = pointTwo[0] - pointOne[0]; var height = pointTwo[1] - pointOne[1]; var sideC = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)); return sideC }; ////// add text layer ////// function createTextLayer (thisText, thePoint, res) { var docRef = activeDocument; var myLayerRef = docRef.artLayers.add(); myLayerRef.kind = LayerKind.TEXT; myLayerRef.name = thisText; var myTextRef = myLayerRef.textItem; myTextRef.kind = TextType.POINTTEXT; myTextRef.size = 12 * 72 / res; myTextRef.font = "Arial"; //Set text colour in RGB values var newColor = new SolidColor(); newColor.rgb.red = 0; newColor.rgb.green = 0; newColor.rgb.blue = 0; myTextRef.color = newColor; // set the position; myTextRef.position = [thePoint[0] * 72 / res, thePoint[1] * 72 / res]; myLayerRef.blendMode = BlendMode.NORMAL; myLayerRef.opacity = 100; myTextRef.useAutoLeading = false; myTextRef.leading = 0; myTextRef.contents = thisText; return docRef.activeLayer }; |