亲宝软件园·资讯

展开

illustrator脚本实现按数值选择路径对象教程

软件云 人气:0

软件云的朋友们大家好。为了让大家更好的学习AI我像四爷建议了把AI栏目细分。这样更方便大家学习AI了。当然也感谢四爷了。辛苦了。今天这个AI脚本教程比较实用。该脚本可以通过设定好的数值来选择文档相关路径对象。

数值可以设为小于、等于或大于,选择方式分为尺寸、面积、宽、高四种。

按面积选择时,描边等不会被计算在内;包含复合路径会按整体数值计算选择。

包含锁定对象时,文档上所有的对象都会被解锁。

注意:如果符合条件的对象在其他层上且图层被锁定,会出现脚本错误。

要用“文件-脚本”来使用,以CS3为例应放在"C:\Program Files\Adobe\Adobe Illustrator CS3\预设\脚本"下。

当然,也可以放置在其他文件夹中,通过Ctrl+F12定位到脚本所在处来使用。

复制以下全部文字到记事本中,保存为以.js结尾的脚本文件(如:脚本.js)即可。


/////////////////////////////////////////////////////////////////
//Select Paths By Size v.4.0.0.0 -- CS, CS2, CS3,CS4 (Change extension from 'jsx' to 'js' to run in CS)
//>=--------------------------------------
// Selects all Path Objects, smaller/larger than given threshold
// Only works for path and compound path objects.
//
// If paths are selected, script runs on selected items only,
// If nothing is selected, script runs on all paths in document.
// ::: Updates in V2 :::
// • Rebuilt to select based on Size, Area, Width or by Height.
// • Added "same size as" option
// • If one object is selected, starts threshold at that size.
// • Added Tool Tips
// ::: Updates in V3 :::
// • Added status indicator
// • Rebuilt processing engine so it's faster and more accurate
// ::: Updates in V4 :::
// • Added progress bar (CS3 and above)
// • Speed increase
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( john@wundes.com ) www.wundes.com
//copyright full text here: http://www.wundes.com/js4ai/copyright.txt
////////////////////////////////////////////////////////////////// /* Notes: */
function Loadbar(x){
this.versionNum= app.version.split(".")[0] ;
if (this.versionNum >= 13){
this.w = new Window('window', "Processing...", undefined, {independent:true});
this.w.frameLocation = [600,500];
this.w.tracker = this.w.add ('statictext' , [15,15,250,35], "processing");
this.w.prog = this.w.add ('progressbar' , [15,15,250,35], 0, x);
//this.w.show();
}
}
Loadbar.prototype.close = function(){
if (this.versionNum >= 13){
this.w.close();
}
}
Loadbar.prototype.hide=function(){
if (this.versionNum >= 13){
this.w.hide();
}
}
Loadbar.prototype.show=function(){
if (this.versionNum >= 13){
this.w.show();
}
}
Loadbar.prototype.update=function(x){
if (this.versionNum >= 13){
this.w.prog.value = x;
}
}
Loadbar.prototype.label=function(x){
if (this.versionNum >= 13){
this.w.tracker.text = x;
}
} if (app.documents.length > 0)
{
var doc = app.activeDocument;
var sel = doc.selection;
var defaultValue = .05;
if (sel.length == 1)
{
var seed = (sel[0].width + sel[0].height) /2;
if (seed != 0 || seed != null)
{
defaultValue = seed;
}
} var OK = 1;
var by = "none";// flag for what method to walk objects selection/doc
var tolerance = .005;
var DISP_COLOR = new RGBColor();
DISP_COLOR.red = 0;
DISP_COLOR.green = 100;
DISP_COLOR.blue = 0;
var myVersion = Math.floor(version.split(".")[0]); if (myVersion == 12 || myVersion == 11)
{
try{
//if version is CS2
var tVar = prompt("Enter the numeric threshold, use '>' for greater than and '<' for less than,\n i.e.; '>50' would mean 'greater than 50', to match exactly, just enter a number.","<"+defaultValue);// dlg.alertBtnsPnl2.slide.value;
if (tVar == null)
{
OK=3;
throw("end");
}
var tArr = tVar.split("");
var threshold = tVar;
var GT = false;
var LT = false;
var EQ = false;
if (tArr[0] == "<")
{
LT = true;
threshold = tVar.substr(1);
} else if (tArr[0] == ">")
{
GT = true;
threshold = tVar.substr(1);
} else {
EQ = true;
}
var CP_on = confirm("是否包含复合路径?");
var includeLocked = confirm("是否包含锁定对象?");
var dVal = prompt("Enter method for comparison, ('s'=size,'w'=width,'h'=height,'a'=area)","s");
if (dVal == null)
{
OK=3;
throw("end");
}
var dims = "size";
if (dVal == "w")
{
dims = "width";
} else if (dVal == "h")
{
dims = "height";
}else if (dVal == "a")
{
dims = "area";
}
}
catch(e){
// use "OK" values instead...
} } else if (myVersion > 12)
{
//use advanced functionality
//--------------------------Dialog Box Code --------------------------------------------------------
var dlg = new Window('dialog', '选择指定路径对象');
dlg.location = [500,50];
// [startX, StartY, EndX, EndY]
dlg.alertBtnsPnl1 = dlg.add('group', undefined, 'Path Objects Size:');
// Radio Buttons for Larger than, or smaller than
(dlg.alertBtnsPnl1.selectLT = dlg.alertBtnsPnl1.add('radiobutton', [15,15,95,35], '小于' )).helpTip = "选择所有小于滑块设定值的对象。";
(dlg.alertBtnsPnl1.selectSS = dlg.alertBtnsPnl1.add('radiobutton', [15,15,75,35], '等于' )).helpTip = "选择所有等于滑块设定值的对象。";
(dlg.alertBtnsPnl1.selectGT = dlg.alertBtnsPnl1.add('radiobutton', [15,15,95,35], '大于' )).helpTip = "选择所有大于滑块设定值的对象。"; dlg.alertBtnsPnl1.selectLT.value = true;
dlg.alertBtnsPnl1.orientation='row';
// Set Size of threshold
dlg.alertBtnsPnl2 = dlg.add('group',undefined, 'Threshold:');
(dlg.alertBtnsPnl2.slide = dlg.alertBtnsPnl2.add('slider', [25,15,165,39], 'Set size of threshold object:')).helpTip = "拖动滑块以设定.";
dlg.alertBtnsPnl2.slide.value = defaultValue;
(dlg.alertBtnsPnl2.titleEt = dlg.alertBtnsPnl2.add('edittext', [100,15,160,35], dlg.alertBtnsPnl2.slide.value)).helpTip = "输入界限值";
dlg.alertBtnsPnl2.titleEt.text = roundOff(defaultValue);
(dlg.alertBtnsPnl2.titleSt = dlg.alertBtnsPnl2.add('statictext', [15,15,35,35], 'px')).helpTip = "单位:像素";
dlg.alertBtnsPnl2.orientation='row'; (dlg.dimsPnl = dlg.add('panel', undefined, '指定选择方式:')).helpTip = "设定选择对象的方式";
dlg.dimsPnl.orientation='row';
(dlg.dimsPnl.selectS = dlg.dimsPnl.add('radiobutton', [15,15,65,35], '尺寸' )).helpTip = "(宽*高)/2";
(dlg.dimsPnl.selectA = dlg.dimsPnl.add('radiobutton', [15,15,65,35], '面积' )).helpTip = "对象内部面积值。\n描边、阴影等不被计算;";
(dlg.dimsPnl.selectW = dlg.dimsPnl.add('radiobutton', [15,15,70,35], '宽度' )).helpTip = "根据对象宽度";
(dlg.dimsPnl.selectH = dlg.dimsPnl.add('radiobutton', [15,15,65,35], '高度' )).helpTip = "根据对象高度";
dlg.dimsPnl.selectA.value = true; dlg.dimsPnl.selectS.onClick= setTextS;
dlg.dimsPnl.selectH.onClick= setTextH;
dlg.dimsPnl.selectA.onClick= setTextA;
dlg.dimsPnl.selectW.onClick= setTextW;
// Add a checkbox to control selection
dlg.alertBtnsPnl3 = dlg.add('group', undefined, '');
(dlg.hasBtnsCb = dlg.alertBtnsPnl3.add('checkbox', [25,25,235,39], '是否包含复合路径?')).helpTip="同时选中复合路径。";
(dlg.incLocked = dlg.alertBtnsPnl3.add('checkbox', [25,25,235,39], '是否包含锁定对象?')).helpTip="所有对象都会被解锁!";
dlg.hasBtnsCb.value = true;
dlg.incLocked.value = false;
dlg.alertBtnsPnl3.orientation='column'; dlg.btnPnl = dlg.add('group', undefined, 'Do It!');
dlg.btnPnl.orientation='row';
dlg.btnPnl.buildBtn1= dlg.btnPnl.add('button',[15,15,115,35], '取消', {name:'cancel'});
dlg.btnPnl.buildBtn2 = dlg.btnPnl.add('button', [125,15,225,35], '确定', {name:'ok'});
dlg.alertBtnsPnl2.slide.onChange= sliderChanged;
dlg.alertBtnsPnl2.titleEt.onChanging = eTextChanged;
dlg.btnPnl.buildBtn1.onClick= actionCanceled;
dlg.show(); // Translate dialog here:
var threshold = 1*(dlg.alertBtnsPnl2.titleEt.text);// dlg.alertBtnsPnl2.slide.value;
var CP_on = dlg.hasBtnsCb.value; //false; // ! confirm("Ignore compound path objects?");
var GT = dlg.alertBtnsPnl1.selectGT.value;
var EQ = dlg.alertBtnsPnl1.selectSS.value;
var LT = dlg.alertBtnsPnl1.selectLT.value;
var includeLocked = dlg.incLocked.value;
var dims = "size";
if (dlg.dimsPnl.selectW.value)
{
dims = "width";
} else if (dlg.dimsPnl.selectH.value)
{
dims = "height";
}else if (dlg.dimsPnl.selectA.value)
{
dims = "area";
}
} else {
OK = 2; // alert and close with "version" message
}// end version check //--------------------------Main Code Section -------------------------------------------------------- var dObj = new Date();
var start = dObj.getTime();
var niceTime = (dObj.getMonth( )+1)+"-"+dObj.getDay( )+"-"+dObj.getFullYear( ).toString().substr(2)+"_"+dObj.getHours( )+"."+dObj.getMinutes( )+"."+dObj.getSeconds( );
var topLocked = doc.layers[0].locked;
if(OK==1){
//add a temporary layer for tracking
/* //too slow
var dispLayer = doc.layers.add();
var pointTextRef = dispLayer.textFrames.add();
pointTextRef.textRange.fillColor = DISP_COLOR;
pointTextRef.selected = false;
pointTextRef.top = doc.height/2;
pointTextRef.left = doc.width/2;
*/
var selItems = new Array();
//pointTextRef.contents = "Pre-Processing!!!";
//redraw(); var selLen = sel.length;
if(selLen >1){
by = "selection";
//
var b = new Loadbar(selLen);
b.label("Preprocessing Data...");
b.show();
// pointTextRef.top = selection[0].top;
// pointTextRef.left =selection[0].left;
// redraw();
// deselect Compound path items if not included
var cpLen = doc.compoundPathItems.length;
for (var cp=0;cp<cplen></cplen>{
b.label("Deselecting Compound Pathitem "+cp+" of "+cpLen);
b.update(cp);
if(!CP_on){
doc.compoundPathItems[cp].selected = false;
}
}
var plen = doc.pathItems.length;
// Add selected path items to Array var i = null;
for (var x=0;x <plen></plen> {
b.label("Pre-Processing item "+x+" of "+plen);
b.update(x);
i = doc.pathItems[x];
if (i.selected== true )
{
selItems.push(i);
}
}
} else {
by = "doc";
// // copy pathitems array to selItems
// if they arent part of a compoundPath
var plen = doc.pathItems.length;
var b = new Loadbar(plen);
b.label("Preprocessing Data...");
b.show();
// Add selected path items to Array
var i = null;
for (var x=0;x <plen></plen> {
i = doc.pathItems[x];
if (!CP_on && i.parent.typename == "CompoundPathItem")
{
continue;
}else if (!includeLocked && (i.layer.locked == true || i.locked==true))
{
continue;
} else {
selItems.push(i);
} // end 'if'
} // end 'for'
} // end else if (myVersion <= 12 && selItems.length > 1000)
{
alert(" Go get a coffee, this is going to take a while..."); }
selectBelow(selItems);
// -----------------cleanup and analytics--------------------------
//pointTextRef.remove();
// redraw();
b.close();
var fObj = new Date();
var finish= fObj.getTime();
var totalSeconds = (finish-start)/1000;
var minutes = Math.floor(totalSeconds/60);
var seconds = totalSeconds%60;
alert(doc.selection.length+"个对象被选择,耗时"+minutes+"分,"+Math.round(seconds)+"秒。");
//dispLayer.remove();
} // end cancel test
} else {
if (OK==2)
{
alert("该脚本在Illustrator CS及其以上版本才能运行!");
} else if(OK ==3){
//die quitly
}
else {
alert("必须打开一个文档才能运行本脚本!");
}
} //----------------------------------------------Nobody here but us functions--------------------------------------------------------- //-------------------------------Dialog box functions---------------------------v.13 >
function actionCanceled() {
OK = 3;
dlg.hide();
}
function sliderChanged() {
dlg.alertBtnsPnl2.titleEt.text = dlg.alertBtnsPnl2.slide.value;
} function setTextS() {
//if single item selected, change default to items Size
if (doc.selection.length == 1)
{
dlg.alertBtnsPnl2.titleEt.text = roundOff(Math.abs( (doc.selection[0].width+doc.selection[0].height)/2));
} }
function setTextH() {
//if single item selected, change default to items Height
if (doc.selection.length == 1)
{
dlg.alertBtnsPnl2.titleEt.text = roundOff(doc.selection[0].height);
}
}
function setTextA() {
//if single item selected, change default to items Area
if (doc.selection.length == 1)
{
dlg.alertBtnsPnl2.titleEt.text = roundOff(Math.abs( doc.selection[0].area));
}
}
function setTextW() {
//if single item selected, change default to items Width
if (doc.selection.length == 1)
{
dlg.alertBtnsPnl2.titleEt.text = roundOff(doc.selection[0].width);
}
}
function eTextChanged() {
dlg.alertBtnsPnl2.slide.value = dlg.alertBtnsPnl2.titleEt.text;
}
//-------------------------------End Dialog box functions---------------------------v.13 >
// common functionality: function selectBelow(sel){
var MAX = sel.length;
for (var x=0;x{
// alert("now processing:"+sel[x].typename);
if(sel[x].locked == true){
if (includeLocked)
{
sel[x].locked = false;
} else {
continue;
}
}
try{
sel[x].selected = true;
}
catch(e){
// on a locked layer...
} //=========normal pathitem=============
if (sel[x].typename == "PathItem")
{
//pointTextRef.top = sel[x].top;
//pointTextRef.left = sel[x].left;
//pointTextRef.contents = "Processing item "+x+" of "+MAX;
if (myVersion <= 12){redraw();}
//redraw();
b.label("Processing item "+x+" of "+MAX);
b.update(x);
b.show();
checkThreshold(sel[x],dims);
} else {
//not a pathitem, dont care...
sel[x].selected = false;
}
}
}
function checkThreshold(obj,prop){
//if larger than
if( CP_on == false && obj.parent.typename == "CompoundPathItem"){
obj.selected=false;
return;
}
var p = 0;
if(prop == "size"){
//square root of area... (Need abs because in compound shapes, the area can be negative
p = Math.abs( (obj.width+obj.height)/2);
} else {
p = Math.abs(eval("obj."+prop));
}
// deselect if less than
if (GT && p < threshold+tolerance) // actually creating a tolerance for DEselection, meaning objects same size will be booted.
{
obj.selected=false;
return;
}
// deselect if larger than
if (LT && p > threshold-tolerance)
{
obj.selected=false;
return;
}
// deselect if exactly same size as:
if (EQ && (p <threshold-tolerance p=""></threshold-tolerance> threshold+tolerance) )
{
obj.selected=false;
return;
}
} function copyArr(a){
var a2 = [];
var len = a.length;
for (var ca=0; ca{
a2.push(a[ca]);
}
return a2;
}
function roundOff(x){
var place = 100;
x = Math.round(x*place);
x /= place;
return x;
}

加载全部内容

相关教程
猜你喜欢
用户评论