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

作者:胡说八道转稿 来源:飞特网会员投稿教程 浏览次数:加载中... 网友评论 0

飞特的朋友们大家好。为了让大家更好的学习ai我像四爷建议了把ai栏目细分。这样更方便大家学习ai了。当然也感谢四爷了。辛苦了。今天这个ai脚本教程比较实用。该脚本可以通过设定好的数值来选择文档相关路径对象。
数值可以设为小于、等于或大于,选择方式分为尺寸、面积、宽、高四种。
按面积选择时,描边等不会被计算在内;包含复合路径会按整体数值计算选择。
包含锁定对象时,文档上所有的对象都会被解锁。
注意:如果符合条件的对象在其他层上且图层被锁定,会出现脚本错误。illustrator脚本实现按数值选择路径对象 飞特网 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 ;cp++ )
{
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 ;x++ )
{
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 ;x++ )
{
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<max;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) )
{
obj.selected=false;
return;
}
}

function copyarr(a){
var a2 = [];
var len = a.length;
for (var ca=0; ca<len;ca++ )
{
a2.push(a[ca]);
}

return a2;
}
function roundoff(x){
var place = 100;
x = math.round(x*place);
x /= place;
return x;
}



[错误报告] [推荐] [收藏] [打印] [关闭] [返回顶部]

  • 飞特网大部分教程属网友投稿,并不代表飞特网立场。如果您的教程在飞特网发布后没有注明作者和出处请联系我们处理。
    飞特网会员发布的所有作品版权规作者所有,未经作者同意,任何人不得将其用于商业目的;
    如发现本站有含色情等违反国家法律法规内容的图片及涉及个人肖像权及版权的内容,请即时通知我们,飞特网将立刻删除相关内容.本站原创内容,欢迎转载.请注明出处和作者,谢谢!
    访问本站推荐使用分辨率1280*1024,internet explorer7.0浏览器。