illustrator脚本 关于选择

选择文本对象

下面是一个关于获取文档中选择的一个对象是不是文本对象的代码,如果是则返回文本对象,如果不是就弹出提示对话框,有兴趣的可以下载试用看看。

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

取消选择

方法一:

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;
}

你可能感兴趣的:(illustrator,javascript,前端)