选择渐变对象
var doc = app.activeDocument;
//如果文档的 路径对象 0~6000之间 就执行
if (doc.pathItems.length > 0 && doc.pathItems.length < 6000) {
var gradientColorCount = 0;
var gradientList = new Array();
var err = 0;
for (i = 0; i < doc.pathItems.length; i++) {//循环开始修改颜色
var p = doc.pathItems[i];
//判断 p对象 填充类型是渐变
if (p.fillColor.typename == "GradientColor") {
gradientColorCount++;
gradientList.push(p);
}
}
//提示设计 是否有渐变描边
if (gradientColorCount > 0) {
alert("该文档有:" + gradientColorCount + "个渐变填充");
for(i = 0;i<gradientList.length;i++){
gradientList[i].selected = true;
}
}
}
选择文本对象
下面是一个关于获取文档中选择的一个对象是不是文本对象的代码,如果是则返回文本对象,如果不是就弹出提示对话框,有兴趣的可以下载试用看看。
get TextFrame objects in the current document.
(function f() { // try to get the TextFrame
if (!selection[0] && !selection.typename) alert ('No selection!'); // no selection
if (selection[0]) { // object mode
if (selection.length > 1) alert ('So many selection!');
if (selection[0].typename === 'GroupItem') { // try to get TextFrame from GrouItem
if (selection[0].pageItems.length > 1) alert('It\'s a complex group!');
if (selection[0].pageItems[0].typename !== 'TextFrame') {
alert ('Selection in group doesn\'t a Text Frame!');
}
return selection[0].pageItems[0];
}
if (selection[0].typename !== 'TextFrame') alert ('Selection doesn\'t a Text Frame!');
return selection[0];
} else if (selection.typename) { // text mode
return selection.parent.textFrames[0];
}
throw new Error();
}());
选择混合对象
下面是一个一次选择当前文档中所有的混合对象的脚本
选择当前文档中所有的混合Blend对象
function select_all_blends(){ //选择当前文档中所有的混合Blend对象
var doc = app.activeDocument;
var plug = doc.pluginItems;
var plugLen = plug.length;
doc.selection = null;
var delMe, newPlugLen, finalSelection=[];
for(var i=0; i<plug.length; i++){
delMe = plug[i].duplicate(doc,ElementPlacement.PLACEATEND);
delMe.selected = true;
executeMenuCommand('Path Blend Release');
delMe = doc.selection;
newPlugLen = doc.pluginItems.length;
app.cut();
if(newPlugLen == plugLen){
//found a blend
finalSelection.push(plug[i]);
}
}
for(var i=0; i<finalSelection.length; i++){
finalSelection[i].selected = true;
}
}
取消选择
方法一:
function deselectAll() {
for (var i = 0; i < activeDocument.layers.length; i++) {
activeDocument.layers[i].hasSelectedArtwork = false;
}
}
方法二:
selection=null;
方法三:
// cs6+
executeMenuCommand ( 'deselectall' );
选择间隔对象
var selectedObjects = app.activeDocument.selection;
if (selectedObjects.length > 0) {
var selectedX = selectedObjects[0].position[0];
var selectedWidth = selectedObjects[0].width;
var targetWidths = [];
for (var i = 2; i <= selectedWidth; i += 2) {
targetWidths.push(i * selectedWidth);
}
for (var i = 0; i < app.activeDocument.layers.length; i++) {
var layer = app.activeDocument.layers[i];
for (var j = 0; j < layer.pageItems.length; j++) {
var item = layer.pageItems[j];
if (item.position[0] === selectedX || isEvenMultipleOfSelectedWidth(item.position[0] - selectedX, targetWidths)) {
item.selected = true;
}
}
}
}
else {
alert("请选中至少一个对象。");
}
function isEvenMultipleOfSelectedWidth(value, targetWidths) {
for (var i = 0; i < targetWidths.length; i++) {
if (value % targetWidths[i] === 0) {
return true;
}
}
return false;
}