// JPG 的文件夹 需要自己设置, 是jpg的位置
var jpaPath = "C:\\Users\\Administrator\\Desktop";
main(jpaPath);
function main(jpaPath){
//获取当前文件的文件名
var jpgName = activeDocument.name.substring(0, activeDocument.name.lastIndexOf("."));
if(jpgName == ''){
alert('请先保存文件再运行!');
return;
}
//弹出一个提示框 让用户选择 是否置入
//var embed = prompt('是否置入 1=置入 0=不置入',1);
var embed = 1;
var data = {
jpgPath: jpaPath, //jpg的路径
name: jpgName + ".jpg", //图片的名称
};
//获取图层名称 与 每个图层的极限坐标
var artboardPositionList = getArtboardsNames();
//创建一个图层并且激活
var layer = createLayer("jpg");
for (i = 0; i < artboardPositionList.length; i++) {
var tempJpgFile = data.jpgPath+"/"+jpgName+"_"+artboardPositionList[i].name+".jpg";
if(!File(tempJpgFile).exists){
var tempIndex = "00"+(i+1);
tempIndex = tempIndex.substring(tempIndex.length-2,tempIndex.length);
tempJpgFile = data.jpgPath+"/"+jpgName+"-"+tempIndex+".jpg";
}
tempJpgFile = decodeURI(tempJpgFile)
var rect = artboardPositionList[i].rect;
var width = artboardPositionList[i].width;
var height = artboardPositionList[i].height;
importJpg({jpgPath:tempJpgFile,x:rect[0],y:rect[1],w:width,h:height,embed:embed});
}
//把新建的图层移动到最下面
layer.zOrder(ZOrderMethod.SENDTOBACK);
showWindow("运行完毕!!!");
}
/**
* 创建一个图层并激活
* @param layername
*/
function createLayer(layername) {
var doc = app.activeDocument;//当前文档
var layer=doc.layers.add();
layer.name = layername;
layer.zOrder(ZOrderMethod.BRINGTOFRONT);
return layer;
}
/**
* 获取画板的位置以及名称
* @returns {any[]}
*/
function getArtboardsNames() {
var doc = app.activeDocument;//当前文档
var arr = new Array();
for(i=0;i<doc.artboards.length;i++){
arr.push({
name:doc.artboards[i].name,
rect:doc.artboards[i].artboardRect,
width:doc.artboards[i].artboardRect[2]-doc.artboards[i].artboardRect[0],
height:doc.artboards[i].artboardRect[1]-doc.artboards[i].artboardRect[3]
})
}
return arr;
}
//取消选择
function deselect(){
var sels = activeDocument.selection;
for(i = 0; i<sels.length;i++){
sels[i].selected = false;
}
}
/**
* 导入jpg
* @param jpgFilePath
*/
function importJpg(data) {
var aFile = File(data.jpgPath);
if(aFile.exists){
var pt = 72.0/25.4;
var myPlacedItem = app.activeDocument.placedItems.add();
myPlacedItem.file = aFile;
myPlacedItem.position = Array(data.x+(data.w-myPlacedItem.width)/2,data.y-(data.h-myPlacedItem.height)/2);
if(data.embed==1){
myPlacedItem.embed();
}
}else{
alert("路径不存在");
}
}
//一个只显示 1秒的 窗口
function showWindow(title){
var x = 1920/2;
var y = 100;
var w = 200;
var win=new Window('palette',title,[x-w,y,x+w,y]);
win.show();
$.sleep(800);
win.close();
}
//添加文本 用于测试
function addText(content) {
var doc = app.activeDocument;
var textRef = doc.textFrames.add();
textRef.textRange.characterAttributes.size = 12;
textRef.contents = content;
textRef.textRange.paragraphAttributes.justification = Justification.CENTER;
}