Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
【Photoshop】レイヤーの書き出しツール
/*================================================================================
# LayerExporter for Photoshop
# レイヤーをpngで書き出すスクリプト
================================================================================*/
#target photoshop
//================================================================================
const SCRIPT_TITLE = "LayerExporter";
const EDIT_TEXT_WIDTH_MINSIZE = 100;
const EDIT_TEXT_HEIGHT_MINSIZE = 30;
//================================================================================
var mFolderPath;
//================================================================================
// 実行部
app.activeDocument.suspendHistory(SCRIPT_TITLE,"Run()");
//================================================================================
// メイン処理
function Run() {
// 単位設定を保持+Pixelに変換
var myRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
SettingWindow();
grpSelectType.selectType.selection = 0;
// 保存されていたら
if (activeDocument.saved) {
// 書き出し先の初期値に現在のパスを指定
mFolderPath = activeDocument.path;
grpFilePath.filePath.text = mFolderPath;
}
// 参照ボタンが押された時
grpFilePath.btnReference.onClick = function () {
FolderReference ();
// 再描画
refresh();
}
// 実行ボタンが押されたら
grpSystemButton.btnApply.onClick = function () {
if (SettingCheck ()) {
LayerExport();
}
}
dlg.show();
// 単位設定を元に戻す
preferences.rulerUnits = myRulerUnits;
}
//================================================================================
// ウィンドウの設定
function SettingWindow () {
// --ウィンドウの生成
dlg = new Window ("dialog", SCRIPT_TITLE);
dlg.minimumSize = [200, 100];
dlg.margins = [10, 10, 10, 10];
// --Panel > Setting
dlg.pnlSetting = dlg.add("panel", undefined,"Setting");
dlg.pnlSetting.alignment = "left";
// --Setting
grpFilePath = dlg.pnlSetting.add("group");
grpFilePath.alignment = "left";
grpFilePath.title = grpFilePath.add("statictext", undefined, "書き出し先:");
grpFilePath.filePath = grpFilePath.add("edittext",undefined, mFolderPath);
grpFilePath.filePath.minimumSize = [EDIT_TEXT_WIDTH_MINSIZE, undefined];
grpFilePath.filePath.maximumSize = [EDIT_TEXT_WIDTH_MINSIZE, EDIT_TEXT_WIDTH_MINSIZE];
grpFilePath.btnReference = grpFilePath.add("button",undefined, "参照", { name:"reference"});
grpSelectType = dlg.pnlSetting.add("group");
grpSelectType.alignment = "left";
grpSelectType.sText = grpSelectType.add("statictext", undefined, "対象レイヤーを選択:");
grpSelectType.selectType = grpSelectType.add("dropdownlist", undefined, ["Select Layers", "All Layers"]);
grpTrim = dlg.pnlSetting.add("group");
grpTrim.cBoxTrim = grpTrim.add("checkbox", undefined, "トリミングを行う");
grpTrim.cBoxTrim.minimumSize = [200, undefined];
grpTrim.alignment = "left";
grpSerialNum = dlg.pnlSetting.add("group");
grpSerialNum.cBoxSerialNum = grpSerialNum.add("checkbox", undefined, "連番追加");
grpSerialNum.cBoxSerialNum.minimumSize = [200, undefined];
grpSerialNum.alignment = "left";
// --システムボタン
grpSystemButton = dlg.add("group");
grpSystemButton.btnApply = grpSystemButton.add("button",undefined, "実行", { name:"apply"});
grpSystemButton.btnApply.minimumSize = [EDIT_TEXT_WIDTH_MINSIZE, EDIT_TEXT_HEIGHT_MINSIZE];
grpSystemButton.btnCancel = grpSystemButton.add("button",undefined, "閉じる", { name:"cancel"});
grpSystemButton.btnCancel.minimumSize = [EDIT_TEXT_WIDTH_MINSIZE, EDIT_TEXT_HEIGHT_MINSIZE];
}
//================================================================================
// レイヤーのエクスポート処理
function LayerExport() {
try {
var targetLayerList = [];
var copyVisibleList = [];
var selection = grpSelectType.selectType.selection.text;
if (selection == "All Layers") {
targetLayerList = activeDocument.layers;
} else {
targetLayerList = GetSelectedLayers (targetLayerList);
}
// 選択レイヤーの表示状態を保存
for (var i = 0; i < targetLayerList.length; i++) {
copyVisibleList.push(targetLayerList[i].visible);
}
LayerEnable (targetLayerList);
for (var i = 0; i < targetLayerList.length; i++) {
var path = mFolderPath + "/" ;
// 連番追加するか?(同一名回避用)
if (grpSerialNum.cBoxSerialNum.value) {
// 0埋め3桁
var num = ("00" + i).slice(-3) + "_";
path = path + num;
}
path = path + targetLayerList[i].name;
// 名前に「.png」があると保存時に「.png.png」などになるので削除
path = path.replace(".png", "");
//レイヤーの表示状態を制御
if (i >= 1) {
targetLayerList[i-1].visible = false;
}
targetLayerList[i].visible = true;
SavePNG(path);
// トリミングするか?
if (grpTrim.cBoxTrim.value) {
// ファイルオープンの際は、拡張子まで名前が必要
Trimming (path + ".png");
}
}
// 表示状態を元に戻す
for (var i = 0; i < targetLayerList.length; i++) {
targetLayerList[i].visible = copyVisibleList[i];
}
alert("書き出しが完了しました。", "完了通知");
} catch(e) {
alert (e);
}
}
//================================================================================
// 対象レイヤーのリストを全て非表示に
function LayerEnable(targetLayers) {
for (var i = 0; i < targetLayers.length; i++) {
targetLayers[i].visible = false;
}
}
//================================================================================
// PNG形式で保存
function SavePNG(path) {
var saveFileObj = new File(path);
var pngOpt = new PNGSaveOptions(); //PNGの設定
pngOpt.interlaced = false; //インターレースなし
activeDocument.saveAs (saveFileObj, pngOpt, true, Extension.LOWERCASE)
}
//================================================================================
// フォルダの選択処理
function FolderReference() {
try {
mFolderPath = Folder.selectDialog("★フォルダを指定してください");
grpFilePath.filePath.text = mFolderPath;
} catch (e) {
alert(e);
}
}
//================================================================================
// 設定のエラーチェック処理
function SettingCheck() {
if (mFolderPath == null) {
alert("フォルダが選択されていません", "エラー通知");
return false;
}
return true;
}
//================================================================================
// トリミング処理
function Trimming(path) {
var path02 = path;
// 最初に「/」や「\」があるとファイルオープンの際にコケるので回避
if (path02[0] == '/' || path02[0] == '\\') {
path02.substr(1);
}
var fileObj = new File(path);
app.open(fileObj);
// アルファでトリミング
activeDocument.trim(TrimType.TRANSPARENT, true, true, true, true);
SavePNG (path);
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
//================================================================================
// 選択レイヤーを取得
function GetSelectedLayers(){
var selectList = [];
var idGrp = stringIDToTypeID( "groupLayersEvent" );
var descGrp = new ActionDescriptor();
var refGrp = new ActionReference();
refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
descGrp.putReference(charIDToTypeID( "null" ), refGrp );
executeAction( idGrp, descGrp, DialogModes.ALL );
var myActiveLayer = app.activeDocument.activeLayer;
for (var i = 0; i < myActiveLayer.layers.length; i++) {
selectList.push(myActiveLayer.layers[i])
}
Undo();
return selectList;
}
//================================================================================
// Undo処理
function Undo() {
executeAction(charIDToTypeID("undo"), undefined, DialogModes.NO);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment