Illustrator用のExtendScriptです.
画像に合番を振ります.

目次

  1. 効果
  2. コード

効果

  1. Illustratorにおいて,各リンク・埋め込み画像に合番を振ります.
  2. 合番で対応する画像ファイル名がテキストファイルに書き出されます.(デフォルトではmacOSデスクトップ.必要に応じて変数saveDirを書き換えればよい.)

コード

recursiveLayerUnlock(activeDocument);

var placedItems = activeDocument.placedItems;
var rasterItems = activeDocument.rasterItems;
selectImages(placedItems);
selectImages(rasterItems);

var sel = activeDocument.selection;
for (var i = 0; i < sel.length; i++){
makeRectangle(sel, i);
putSequence(sel, i);
listFileName(sel, i);
}

//---以下、関数---
//レイヤーアンロック
function recursiveLayerUnlock(l){
for (var i = 0; i < l.layers.length; i++){
l.layers[i].locked = false;
if (hasSubLayer(l)) {
recursiveLayerUnlock(l.layers[i]);
}
}
}

function hasSubLayer(l){
return l.layers.length > 0
}

//画像選択
function selectImages(imgs){
for (var i = 0; i < imgs.length; i++){
try{
imgs[i].locked = false;
imgs[i].selected = true;
}catch(e){}
}
}

//画像範囲の明示(枠囲み)
function makeRectangle(sel, i){
var gb = sel[i].geometricBounds;
var x = gb[0];
var y = gb[1];
var w = gb[2] - x;
var h = y - gb[3];
var rect = activeDocument.pathItems.rectangle(y, x, w, h);
rect.filled = false;
rect.stroked = true;
rect.strokeWidth = 1;
}

//合番を振る
function putSequence(sel, i){
var tf = activeDocument.textFrames.add();
tf.contents = formatSeq(i);
tf.paragraphs[0].size = 18;
tf.top = calcMiddle(sel[i], tf);
tf.left = calcCenter(sel[i]);
tf.paragraphs[0].justification= Justification.CENTER;
}

function formatSeq(i){
return "(" + parseInt(i + 1) + ")";
}

function calcCenter(ph){
return (ph.geometricBounds[0] + ph.geometricBounds[2])/2;
}

function calcMiddle(ph, tf){
return (tf.geometricBounds[1] + tf.geometricBounds[3] + ph.geometricBounds[1] + ph.geometricBounds[3])/2;
}

//合番=ファイルパス対応を作る
function listFileName(sel, i){
var ls = []
ls[i] = sel[i].file.fullName;
saveDir = Folder.desktop.fullName + "/"; //出力先は適宜書き換えてください
saveFilePath = saveDir + app.activeDocument.name + ".txt";
for(var i in ls){textWriter(saveFilePath, parseInt(i + 1) + "\t" + sel[i].file.fullName);}
}

function textWriter(filepath, str){
var fileObj = new File(filepath);
fileObj.open("a");
fileObj.encoding = "UTF-8";
fileObj.lineFeed = "Unix"; //改行コードLF
fileObj.writeln(str);
fileObj.close();
fileObj = null;
}