#target 'illustrator'
#targetengine com.adobe.illustrator.demo.flashplayer//目标软件是ai
if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0 ){
var doc = app.activeDocument;//当前文档
var lw=0.2*2.834646; //刀线粗 0.15 mm
var docSpots = activeDocument.spots;
var isExist=false;
for(var j=0;j < docSpots.length; j++){
if (docSpots[j].name.toString() =="刀线"){
spot1=docSpots[j];
isExist=true;
break
}
}
if (!isExist){newCMYKColor = new CMYKColor();
newCMYKColor.cyan = 1;
newCMYKColor.magenta = 100;
newCMYKColor.yellow = 100;
newCMYKColor.black = 1;
// Create the new spot
var newSpot = app.activeDocument.spots.add();
newSpot.name = "刀线";
newSpot.color = newCMYKColor;
newSpot.colorType = ColorModel.SPOT
var newSpotColor = new SpotColor();
newSpotColor.spot = newSpot;
// Use the spot color to set the fill color
}
else{
var newSpotColor = new SpotColor();
newSpotColor.spot = spot1;
}
//以上是建立DC
//建立图层
var isExist=false;
for(i=0;i<doc.layers.length;i++){
if(doc.layers[i].name=='刀线图层'){
cutlayer=doc.layers[i];
isExist=true;
break}
}//设置图层名称
if(!isExist){
cutlayer=doc.layers.add(); //新建图层
cutlayer.name = '刀线图层';
}
g = cutlayer.groupItems.add();
var frontPath = app.activeDocument.selection;
for(var i=0;i<frontPath.length;i++){
apply_G_DC(frontPath[i]);
frontPath[i].move(g,ElementPlacement.PLACEATEND)
}
function apply_G_DC(the_Obj){
if (the_Obj.constructor.name=="GroupItem"&&the_Obj.clipped==false){
try {
var N_sub_obj = the_Obj.pageItems.length;
for (var i = 0; i < N_sub_obj; i += 1) {
apply_G_DC(the_Obj.pageItems[i]);
}
} catch(error) {
return;
}
}
else if (the_Obj.constructor.name=="CompoundPathItem"){
try {
var N_sub_obj1 = the_Obj.pathItems.length;
for (var i = 0; i < N_sub_obj1; i += 1) {
apply_G_DC(the_Obj.pathItems[i]);
}
} catch(error) {
return;
}
}