学习AI JS脚本的学习笔记
你的第一个脚本:var myDocument = app.documents.add(); //新建一个文档
var myTextFrame = myDocument.textFrames.add();//新建一个文本框
myTextFrame.position = ;//定义文本框的位置
myTextFrame.contents = "Hello World!"//文本框输入的内容
你的第二个脚本
var docRef = app.activeDocument;//获取当前文档
var docWidth = docRef.width //定义当前文件宽度
var frameRef = docRef.textFrames //获取文档文本
frameRef.width = docWidth//让文本宽度改为文档宽度
你的第三个脚本
myObject.rotate(30, undefined, undefined, true);
引用对象
var frontMostDocument = activeDocument;
var frontMostDocument = app.activeDocument;
访问对象
var myStyles = app.activeDocument.graphicStyles;
var firstStyle = app.activeDocument.graphicStyles;
var styleName = myStyles.name
var styleName = firstStyle.name
var styleName = app.activeDocument.graphicStyles.name
alert ( myStyles.length );
var lastStyle = myStyles[ myStyles.length - 1 ];
创建对象
var myDoc = app.documents.add()
var myLayer = myDoc.layers.add()
var myColor = new CMYKColor()
使用选择
var seIectedObjects = app.activeDocument.seIection;
var topObject = app.activeDocument.seIection;
alert(topObject.typename)
使用文本框
var rectRef = docRef.pathItems.rectangle(700, 50, 100, 100);
var areaTextRef = docRef.textFrames.areaText(rectRef);
var myDoc = documents.add();
var myPathItem1 = myDoc.pathItems.rectangle(244, 64, 82, 76);
var myTextFrame1 = myDoc.textFrames.areaText(myPathItem1);
var myPathItem2 = myDoc.pathItems.rectangle(144, 144, 42, 116);
var myTextFrame2 = myDoc.textFrames.areaText(myPathItem2);
// use the nextFrame property to thread the text frames
myTextFrame1.nextFrame = myTextFrame2;
var sText = "This is two text frames linked together as one story, with text
flowing from the first to the last. This is two text frames linked together as one
story, with text flowing from the first to the last. This is two text frames linked
together as one story. ";
myTextFrame1.contents = sText;
redraw();
路径
指定一系列的XY座标
var myDoc = app.activeDocument;
var myLine = myDoc.pathItems.add();
//set stroked to true so we can see the path
myLine.stroked = true;
myLine.setEntirePath([, , ]);
var myDoc = app.activeDocument;
var myLine = myDoc.pathItems.add();
//set stroked to true so we can see the path
myLine.stroked = true;
var newPoint = myLine.pathPoints.add();
newPoint.anchor = ;
//giving the direction points the same value as the
//anchor point creates a straight line segment
newPoint.leftDirection = newPoint.anchor;
newPoint.rightDirection = newPoint.anchor;
newPoint.pointType = PointType.CORNER;
var newPoint1 = myLine.pathPoints.add();
newPoint1.anchor = ;
newPoint1.leftDirection = newPoint1.anchor;
newPoint1.rightDirection = newPoint1.anchor;
newPoint1.pointType = PointType.CORNER;
var newPoint2 = myLine.pathPoints.add();
newPoint2.anchor = ;
//giving the direction points different values
//than the anchor point creates a curve
newPoint2.leftDirection =;
newPoint2.rightDirection = ;
newPoint2.pointType = PointType.CORNER;
var myDoc = app.activeDocument;
var myLine = myDoc.pathItems.add();
myLine.stroked = true;
myLine.setEntirePath( [, ]);
var newPoint = myDoc.myLine.pathPoints.add();
newPoint.anchor = ;
newPoint.leftDirection = newPoint.anchor;
newPoint.rightDirection = newPoint.anchor;
newPoint.pointType = PointType.CORNER;
创建一个矩形
var myDocument = app.documents.add()
var artLayer = myDocument.layers.add()
var rect = artLayer.pathItems.rectangle( 144, 144, 72, 216 );
创建一个多边形
var myDocument = app.documents.add()
var artLayer = myDocument.layers.add()
var poly = artLayer.pathItems.polygon( 144, 288, 72.0, 7 );
使用透视网格
从一个文档复制选定项目到新的文档
var newItem;
var docseIected = app.activeDocument.seIection;
if ( docseIected.length > 0 ) {
// Create a new document and move the seIected items to it.
var newDoc = app.documents.add();
if ( docseIected.length > 0 ) {
for ( i = 0; i < docseIected.length; i++ ) {
docseIected.seIected = false;
newItem = docseIected.duplicate( newDoc,
ElementPlacement.PLACEATEND );
}
}
else {
docseIected.seIected = false;
newItem = docseIected.parent.duplicate( newDoc,
ElementPlacement.PLACEATEND );
}
}
else {
alert( "Please seIect one 0r more art objects" );
}
复制描边群组
if ( app.documents.length > 0 ) {
docseIection = app.activeDocument.seIection;
if ( docseIection.length > 0 ) {
newGroup = app.activeDocument.groupItems.add();
for ( i = 0; i < docseIection.length; i++ ) {
newItem = docseIection.duplicate();
newItem.moveToBeginning( newGroup );
}
brush4 = app.activeDocument.brushes;
brush4.applyTo( newGroup );
}
}
填充颜色
if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0) {
frontPath = app.activeDocument.pathItems;
// Set color values for the CMYK object
newCMYKColor = new CMYKColor();
newCMYKColor.black = 0;
newCMYKColor.cyan = 30.4;
newCMYKColor.magenta = 32;
newCMYKColor.yellow = 0;
// Use the color object in the path item
frontPath.filled = true;
frontPath.fillColor = newCMYKColor;
}
关闭文档
if ( app.documents.length > 0 ) {
aiDocument = app.activeDocument;
aiDocument.close( SaveOptions.DONOTSAVECHANGES );
aiDocument = null;
}
默认填充和描边
if ( app.documents.length == 0 ) {
doc = app.documents.add();
}
else {
doc = app.activeDocument;
}
doc.defaultFilled = true;
doc.defaultStroked = true;
创建一个新文件
app.documents.add( DocumentColorSpace.RGB );
导出EPS 这是一个导出EPS的函数,如何让选中的文档导出EPS呢?
function exportFileAsEPS (destFile) {
var newFile = new File(destFile);
var saveDoc;
if ( app.documents.length == 0 )
saveDoc = app.documents.add();
else
saveDoc = app.activeDocument;
var saveOpts = new EPSSaveOptions();
saveOpts.cmykPostScript = true;
saveOpts.embedAllFonts = true;
saveDoc.saveAs( newFile, saveOpts );
}
导出JPG 这是一个导出JPG的函数,如何让选中的文档导出JPG呢?
function exportFileToJPEG (dest) {
if ( app.documents.length > 0 ) {
var exportOptions = new ExportOptionsJPEG();
var type = ExportType.JPEG;
var fileSpec = new File(dest);
exportOptions.antiAliasing = false;
exportOptions.qualitySetting = 70;
app.activeDocument.exportFile( fileSpec, type, exportOptions );
}
}
导出PSD 这是一个导出PSD的函数,如何让选中的文档导出PSD呢?
function exportFileToPSD (dest) {
if ( app.documents.length > 0 ) {
var exportOptions = new ExportOptionsPhotoshop();
var type = ExportType.PHOTOSHOP;
var fileSpec = new File(dest);
exportOptions.writeLayers = true;
exportOptions.resolution = 150;
app.activeDocument.exportFile( fileSpec, type, exportOptions );
}
}
导出TIF 这是一个导出TIF的函数,如何让选中的文档导出TIF呢?
function exportFileToPSD (dest) {
if ( app.documents.length > 0 ) {
var exportOptions = new ExportOptionsTIFF();
var type = ExportType.TIFF;
var fileSpec = new File(dest);
exportOptions.resolution = 150;
exportOptions.byteOrder = TIFFByteOrder.IBMPC;
exportOptions.IZWCompression = false;
app.activeDocument.exportFile( fileSpec, type, exportOptions );
将底层设置为最高层
if (documents.length > 0) {
countOfLayers = activeDocument.layers.length;
if (countOfLayers > 1) {
bottomLayer = activeDocument.layers;
bottomLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
}
else {
alert("The active document only has only 1 layer")
}
}
全屏
if ( app.documents.length > 0 ) {
app.documents.views.screenMode = ScreenMode.FULLSCREEN;
}
if ( app.documents.length > 0 ) {//至少开启一个档案
docseIection = app.activeDocument.seIection;//选取对象的变数
if ( docseIection.length > 0 ) {//如果选取变数大于0
newGroup = app.activeDocument.groupItems.add();//则增加新群组
for ( i = 0; i < docseIection.length; i++ ) {//for循环将
选取对象一一复制至群组中
newItem = docseIection.duplicate();//复制语法
newItem.moveToBeginning( newGroup );//移动语法
}
brush4 = app.activeDocument.brushes;//档案上笔刷面板排序第2的笔刷(脚本的排序要从0算起,也就是就是第一个)
brush4.applyTo( newGroup );//将上述指定的剐刷套用到群组里面
}
}
画两条直线
var myDoc = app.activeDocument;
var myLine1 = myDoc.pathItems.add();
var myLine2 = myDoc.pathItems.add();
myLine1.stroked = true;
myLine2.stroked = true;
myLine1.setEntirePath([, ]);
myLine2.setEntirePath([, ]);
画一条直线,并加上注册色
doc = app.activeDocument;
theseIect = doc.seIection;
x=theseIect.left;
y=theseIect.top;
w=theseIect.width;
h=theseIect.height;
s=50;//标尺距离物体边沿宽度
tsize=40; //标尺字体大小
redColor = new CMYKColor();//标尺用红色
redColor.black = 00;
redColor.cyan = 0;
redColor.magenta =100;
redColor.yellow = 100;
var defaultStrokeColor=doc.swatches.color;
var g1 = doc.groupItems.add();//将标尺群组
var line1=g1.pathItems.add(); //画高度标尺
line1.setEntirePath( new Array(
new Array(x-s, y),
new Array(x-s, y-h),
)
) ;
line1.strokeColor= defaultStrokeColor;
画两条线并加上注册色
var myDoc = app.activeDocument;
var myLine1 = myDoc.pathItems.add();
var myLine2 = myDoc.pathItems.add();
myLine1.stroked = true;
myLine2.stroked = true;
myLine1.setEntirePath([, ]);
myLine2.setEntirePath([, ]);
var defaultStrokeColor=myDoc.swatches.color;
myLine1.strokeColor= defaultStrokeColor;
myLine2.strokeColor= defaultStrokeColor;
画两条线并加上注册色,并设长度为2mm,并将它们群组 ,并把描边粗细为0.1mm
var myDoc = app.activeDocument;
var g1 = myDoc.groupItems.add();//将两条直线群组
var myLine1=g1.pathItems.add(); //建立群组中的一条直线
var myLine2=g1.pathItems.add(); //建立群组中的另一条直线
var pointTomm=2.834645 ; //设定单位为mm
myLine1.stroked = true;
myLine2.stroked = true;
myLine1.setEntirePath([, ]);
myLine2.setEntirePath([, ]);
var defaultStrokeColor=myDoc.swatches.color;
myLine1.strokeColor= defaultStrokeColor;
myLine2.strokeColor= defaultStrokeColor;
var defaultStrokeWidth=0.1*pointTomm
myLine1.strokeWidth= defaultStrokeWidth;
myLine2.strokeWidth= defaultStrokeWidth;
导出JPG
exporFiletToJPEG ('E:\我的.jpg');
function exportFileToJPEG (dest) {
if ( app.documents.length > 0 ) {
var exportOptions = new ExportOptionsJPEG();
var type = ExportType.JPEG;
var fileSpec = new File(dest);
exportOptions.antiAliasing = true;//抗锯齿
exportOptions.artBoardClipping = true;//画板
exportOptions.qualitySetting = 100;//品质
app.activeDocument.exportFile( fileSpec, type, exportOptions );
}
} 楼主笔记还未整理好上传?还是已经上传忘记插入附件了? 好教材,赞一个。 楼主还是要慢慢的来教,例如第一讲是直线。那就说直线的属性,画法。这样就系统全面了~
很好的教程,没有中文说明不然看不明白 很受用!!! 本帖最后由 sunwen 于 2016-6-25 19:51 编辑
友情支持...
{:1_134:}对没有基础而又想学的人从哪开始搞起?
{:1_124:}有点点明白了 {:1_162:} 嗯,挺好的,能看懂一点,不错 发一个这方面的教程。
页:
[1]
2