首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PathItems生成未定义的is不是photoshop脚本中的对象

PathItems生成未定义的is不是photoshop脚本中的对象
EN

Stack Overflow用户
提问于 2021-02-20 04:20:12
回答 1查看 87关注 0票数 2

我设法写了一个VB脚本,我正在尝试使用ExtendScript ToolKit在Photoshop CS4 Extended上运行该脚本。它打开一个(全黑的) .png文件,并在其上放置一小行。

我说我成功了,因为我肯定不是程序员,只是通过在线阅读这个主题,使用Microsoft Access中的一些(非常基本的) VBA知识,以及复制粘贴。

我正在尝试的代码如下:

代码语言:javascript
运行
复制
//Save preferences2
var originalUnit=app.preferences.rulerUnits
preferences.rulerUnits=Units.PIXELS   

var fileName = 1;// Typically this is just a number 1.

var fileNameSeries = "/D/" + fileName

//Open .png to imprint upon
var fileRef=new File(fileNameSeries + ".png")
var docRef = app.open(fileRef)   

var lineArray1 = new PathPointInfo
lineArray1.Kind = 2 ;// for PsPointKind --> 2 (psCornerPoint)
lineArray1.Anchor = Array(100, 100)
lineArray1.LeftDirection = lineArray1.Anchor
lineArray1.RightDirection = lineArray1.Anchor
var lineArray2 = new PathPointInfo
lineArray2.Kind = 2
lineArray2.Anchor = Array(150, 200)
lineArray2.LeftDirection = lineArray2.Anchor
lineArray2.RightDirection = lineArray2.Anchor
//Next create a SubPathInfo object, which will hold the line array
//in its EntireSubPath property.
var lineSubPathArray = new SubPathInfo
lineSubPathArray.Operation = 2 ;// for PsShapeOperation --> 2 (psShapeXOR)
lineSubPathArray.Closed = false
lineSubPathArray.EntireSubPath = lineArray1
//create the PathItem object using Add. This method takes the SubPathInfo object
//and returns a PathItem object, which is added to the pathItems collection
//for the document.
//var myPathItem = new PathItem
myPathItem = docRef.PathItems.Add("A Line", lineSubPathArray)
// stroke it so we can see something
myPathItem.StrokePath(2) //'for PsToolType --> 2 (psBrush)
}
fileName++

fileNameSeries = "/D/ " + fileName


pngFile = new File(fileNameSeries)

pngSaveOptions = new PNGSaveOptions()
pngSaveOptions.interlaced = false
app.activeDocument.saveAs(pngFile, pngSaveOptions, true)


//dispose 
  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)


//Retrieve original preferences
preferences.rulerUnits=originalUnit

我被一个语法错误卡住了。行:myPathItem = docRef.PathItems.Add("A Line", lineSubPathArray)产生一个"undefined is not a object“。

谁能告诉我我做错了什么。

EN

回答 1

Stack Overflow用户

发布于 2021-02-20 17:02:42

在Photoshop中编码路径并不有趣。它没有很好的文档记录,而且与illustrator不同的是,它很难得到很少的东西。

您很接近,但可能与JavaScript和VB混淆。

本质上,路径是一个数组,然后您将路径信息推送到该数组中。因此,以老式的方式构建路径,从第一个元素开始,从零开始,使用lineArray[0].anchor = new Array(100, 100);等等。

奇怪的是,我不能用lineSubPathArray[0].operation = 2;构建路径,只能使用下面的代码。lineSubPathArray[0].operation = ShapeOperation.SHAPEXOR;

需要注意的一点是Photoshop路径项目必须有唯一的名称。不像层,所以这可能对你没有帮助。

要构建您的路径,您需要:

代码语言:javascript
运行
复制
// create the array of PathPointInfo objects
// Declare a new array called lineArray
var lineArray = new Array();

// Firstly, add PathPointInfo() constructor to the array
lineArray.push(new PathPointInfo());

// declare various components to the array
// of which there are four types
// .kind - determines what type of corner
// .anchor - these are path points
// in order to given them x,y co-orrdinates we have to declare
// another new array with them
// and lastly
// direction - specifically leftDirection and rightDirection
// these are tangents to the bezier curve.
// Truthfully, don't understand how the numbers relate to how
// the tangents look in Photoshop
// by declaring a tangent as as anchor ie = lineArray[0].anchor
// the tangents are tucked away and are at the same point as
// the path point


lineArray[0].kind = 2 ;// for PsPointKind --> 2 (psCornerPoint)
lineArray[0].anchor = new Array(100, 100);
lineArray[0].leftDirection  = lineArray[0].anchor;
lineArray[0].rightDirection = lineArray[0].anchor;

// For the second point (and every subsequent point) 
// we need to push PathPointInfo() to the line array

lineArray.push(new PathPointInfo());

//  Since arrays are zero based, the first element
// of the lineArray is lineArray[0]
// the next point is lineArray[1]

lineArray[1].kind = 2
lineArray[1].anchor = new Array(150, 200);
lineArray[1].leftDirection   = lineArray[1].anchor;
lineArray[1].rightDirection =  lineArray[1].anchor;


// Next create a SubPathInfo object, which will hold the line array
// in its EntireSubPath property.
// It's similar to the above, but why it's needed is beyond me

var lineSubPathArray = new Array();
lineSubPathArray.push(new SubPathInfo());
lineSubPathArray[0].operation = ShapeOperation.SHAPEXOR;
// operation, not kind and therefore needs to be declared with ShapeOperation
// Uning an int just didn't work for me in PS CC 2018
lineSubPathArray[0].closed = false;
lineSubPathArray[0].entireSubPath = lineArray;


// create the path item, giving a unique name, 
// passing subpath to the add method
var myPathItem = docRef.pathItems.add("A Line", lineSubPathArray);

// stroke it so we can see something using the preset brush tool
myPathItem.strokePath(ToolType.BRUSH); 

此外,在运行脚本后,路径仍处于选中状态。一段时间后这可能会变得很痛苦。您可以使用此函数取消选择它:

代码语言:javascript
运行
复制
function deselectPath()
{
  // =======================================================
  var id630 = charIDToTypeID( "Dslc" );
  var desc154 = new ActionDescriptor();
  var id631 = charIDToTypeID( "null" );
  var ref127 = new ActionReference();
  var id632 = charIDToTypeID( "Path" );
  ref127.putClass( id632 );
  desc154.putReference( id631, ref127 );
  executeAction( id630, desc154, DialogModes.NO );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66284477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档