先说下一下执行脚本的方法:
a、如果你习惯了专业的编程工具,搭建一套环境也很方便。本人使用的是notepad++,安装一个NppExec插件,直接按F6,如下设置并保存。
在这里我可以顺便说下AE执行脚本也可以这么玩。唯有一地不同就是AfterFX.exe有很多标记,标记后面带一个参数,下面是几种常用的。
AfterFX.exe
-wf opens AE in watch folder mode
-s script command
-r script
-m u can have 2+ projects open
notepad++执行AE的脚本环境搭建如下设置:
b、文件->脚本->浏览找到.jsx后缀的脚本
c、把脚本放置D:\Program Files (x86)\Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts路径下,这是我的路径。重启Photoshop将会看到如下图(我已设置了快捷键):
PYC_PhotoshopTool.jsx脚本如下,欢迎修改到自己工作流程里(欢迎交流pfkue126@126.com)。
#target photoshop
var win = new Window("dialog", "小彭友工具集pfkue126@126.com"); // bounds = [left, top, right, bottom]
win.btnPanel = win.add("panel", [25,10,255,100], "图层批量重命名 | 基于像素裁切");
win.btnPanel.add("statictext", [10, 20, 320, 35], "前缀名");
newName_ed = win.btnPanel.add("edittext", [50, 20, 110, 38], "");
reName_bt = win.btnPanel.add("button", [10, 45, 110, 70], "重命名");
cutImage_bt = win.btnPanel.add("button", [130, 20, 210, 70], "裁切");
win.btnPanel = win.add("panel", [25,10,255,100], "导入序列/自动分配到图层");
win.btnPanel.add("statictext", [10, 20, 320, 35], "导入路径");
importImagePath_ed = win.btnPanel.add("edittext", [60, 20, 175, 38], "C:\\Users\\Administrator\\Desktop");
importSelectPath_bt = win.btnPanel.add("button", [180, 18, 220, 38], "选择...");
win.btnPanel.add("statictext", [10, 50, 320, 65], "过滤");
filter_ed = win.btnPanel.add("edittext", [40, 50, 100, 68], "*.png");
importImage_bt = win.btnPanel.add("button", [110, 45, 220, 70], "导入");
win.btnPanel = win.add("panel", [25,10,255,100], "导出每层");
win.btnPanel.add("statictext", [10, 20, 320, 35], "导出路径");
exportImagePath_ed = win.btnPanel.add("edittext", [65, 20, 175, 38], "C:\\Users\\Administrator\\Desktop");
exportSelectPath_bt = win.btnPanel.add("button", [180, 18, 220, 38], "选择...");
win.btnPanel.add("statictext", [10, 50, 320, 65], "格式");
format_li = win.btnPanel.add("DropDownList", [40, 50, 100, 68]);
var item = format_li.add ('item', 'png');
item = format_li.add ('item', 'jpg');
format_li.selection = format_li.items[0];
exportImage_bt = win.btnPanel.add("button", [110, 45, 220, 70], "导出");
reName_bt.onClick = function()
{
if (newName_ed.text != null) {
for (i=0; i<app.activeDocument.layers.length;i++)
{
app.activeDocument.layers[i].name = newName_ed.text+"_"+(app.activeDocument.layers.length-i);
}
}
// alert(newName_ed.text);
};
cutImage_bt.onClick = function()
{
cut_image_BasePixel();
alert("裁切已完成!");
};
importSelectPath_bt.onClick = function()
{
var sourceFolder = Folder(importImagePath_ed.text);
var selectPath = sourceFolder.selectDlg("请选择导入的序列所在文件夹");
importImagePath_ed.text = selectPath;
};
importImage_bt.onClick = function()
{
var sourceFolder = Folder(importImagePath_ed.text);
var seq_file = sourceFolder.getFiles(filter_ed.text);
var currentDoc = open(new File(seq_file[0]));
currentDoc.layers[0].name = currentDoc.name;
for (var i = 1;i < seq_file.length; i++) {
fileToLayer(seq_file[i], currentDoc);
}
delName_str();
alert("序列导入成功!");
};
function delName_str() {
for (i=0; i<app.activeDocument.layers.length;i++)
{
var layerName = app.activeDocument.layers[i].name;
app.activeDocument.layers[i].name = layerName.substring(0,layerName.length-4);
}
}
exportSelectPath_bt.onClick = function()
{
var sourceFolder = Folder(exportImagePath_ed.text);
var selectPath = sourceFolder.selectDlg("请选择导入的序列所在文件夹");
exportImagePath_ed.text = selectPath;
};
exportImage_bt.onClick = function()
{
displayAll();
if (exportImagePath_ed.text != null) {
for (i=0; i<app.activeDocument.layers.length;i++)
{
app.activeDocument.layers[i].visible = true;
saveFile = new File(exportImagePath_ed.text+'/'+app.activeDocument.layers[i].name+ '.');
//SavePNG(saveFile, 9);
//SaveJPEG(saveFile, 8);
SaveImage();
app.activeDocument.layers[i].visible = false;
}
}
//alert(typeof(format_li.selection));
//alert(typeof(format_li.selection.toString()));
alert("每层导出成功!");
};
function SaveImage() {
if (format_li.selection.toString() == 'png') {
SavePNG(saveFile, 9);
}
if (format_li.selection.toString() == 'jpg') {
SaveJPEG(saveFile, 8);
}
}
//open file,rename layer, copy layer to newDoc.layer
function fileToLayer(input_file_path, newDoc) {
var file_path = new File(input_file_path);
var doc_temp = open (file_path);
doc_temp.layers[0].name = doc_temp.name;
doc_temp.layers[0].duplicate(newDoc, ElementPlacement.INSIDE);
doc_temp.close (SaveOptions.DONOTSAVECHANGES);
}
function SavePNG(saveFile, compressionValue) {
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.compression = compressionValue;//0-9
pngSaveOptions.interlaced = false;
activeDocument.saveAs(saveFile, pngSaveOptions, true,Extension.LOWERCASE);
}
function SaveJPEG(saveFile, jpegQuality) {
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality; //1-12
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}
function displayAll() {
for (i=0; i<app.activeDocument.layers.length;i++)
{
app.activeDocument.layers[i].visible = false;
}
}
function cut_image_BasePixel() {
// init bounds object
var min_x = app.activeDocument.layers[0].bounds[0];
var min_y = app.activeDocument.layers[0].bounds[1];
var max_x = app.activeDocument.layers[0].bounds[2];
var max_y = app.activeDocument.layers[0].bounds[3];
for ( i=0; i<app.activeDocument.layers.length; i++ )
{
if ( app.activeDocument.layers[i].bounds[0] < min_x ) {
min_x = app.activeDocument.layers[i].bounds[0];
}
if ( app.activeDocument.layers[i].bounds[1] < min_y ) {
min_y = app.activeDocument.layers[i].bounds[1];
}
if ( app.activeDocument.layers[i].bounds[2] > max_x ) {
max_x = app.activeDocument.layers[i].bounds[2];
}
if ( app.activeDocument.layers[i].bounds[3] > max_y ) {
max_y = app.activeDocument.layers[i].bounds[3];
}
}
// 临界条件
if ( min_x <= 0) { min_x = 0; }
if ( min_y <= 0) { min_y = 0; }
if ( max_x <= 0) { max_x = 0; }
if ( max_y <= 0) { max_y = 0; }
if ( min_x >= app.activeDocument.width) { min_x = app.activeDocument.width; }
if ( min_y >= app.activeDocument.height) { min_y = app.activeDocument.height; }
if ( max_x >= app.activeDocument.width) { max_x = app.activeDocument.width; }
if ( max_y >= app.activeDocument.height) { max_y = app.activeDocument.height; }
//alert(min_x+'\n'+min_y+'\n'+max_x+'\n'+max_y);
//打组,求组的bounds
//合并图层求bounds
//前面取最小坐标,后面取最大坐标,如果坐标小于0则取0,如果坐标大于画布就取画布大小
app.activeDocument.crop([min_x,min_y,max_x,max_y], 0);
}
win.center();
win.show();