// ScriptName: OM_CreateCropLayer // 2017/09/08 v1.0.0 Release // 2017/09/14 v1.0.1 Add 指定倍数の座標位置に関する記述を追加。 #target Photoshop; app.bringToFront(); // Config Settings var scriptName = "OM_CreateCropLayer"; var docRef = app.activeDocument; var docName = decodeURI(docRef.name); // ドキュメントの単位を記憶 var ref_ruler = app.preferences.rulerUnits; // ドキュメントの単位をピクセルに変換 preferences.rulerUnits = Units.PIXELS; // ドキュメントの幅、高さ、解像度を記憶 var ref_width = docRef.width; var ref_height = docRef.height; var ref_resolution = docRef.resolution; // ドキュメントの解像度を72に変換 docRef.resizeImage(ref_width, ref_height, 72); // Alignment Settings var sizeAlignment = 4; var positionAlignment = {}; positionAlignment.x = 1; //無効化する場合は「1」 positionAlignment.y = 1; //無効化する場合は「1」 try { var layer = docRef.activeLayer; var layerName = layer.name.replace('#Crop_', ''); } catch (e) { ; } try { if (checkSelection()) { layer = docRef.artLayers.add(); docRef.selection.fill(app.foregroundColor); var layerBounds = layer.bounds; var hisState = docRef.historyStates; if (hisState[hisState.length - 2].name == "Layer Order") { docRef.activeHistoryState = hisState[hisState.length - 4]; } else { docRef.activeHistoryState = hisState[hisState.length - 3]; } } else { var layerBounds = layer.bounds; } var x1 = (layerBounds[0]); var y1 = (layerBounds[1]); var x2 = (layerBounds[2]); var y2 = (layerBounds[3]); x1 = Math.floor(x1 / positionAlignment.x) * positionAlignment.x; y1 = Math.floor(y1 / positionAlignment.y) * positionAlignment.y; var w = x2 - x1; var h = y2 - y1; w = Math.ceil(w / sizeAlignment) * sizeAlignment; h = Math.ceil(h / sizeAlignment) * sizeAlignment; x2 = x1 + w; y2 = y1 + h;4/4 createShapeLayer(y1, x1, y2, x2); docRef.activeLayer.name = "#Crop_" + layerName; try { docRef.activeLayer.move(layer, ElementPlacement.PLACEATEND); } catch (e) { docRef.activeLayer.move(layer, ElementPlacement.PLACEAFTER); } executeAction(sTID("newPlacedLayer"), undefined, DialogModes.NO); docRef.activeLayer.opacity = 50; } catch (e) { alert("「選択ツール」でエリアを選択してから実行してください。"); } finally { // ドキュメントの単位、幅、高さ、解像度を元に戻す app.preferences.rulerUnits = ref_ruler; docRef.resizeImage(ref_width, ref_height, ref_resolution); } function checkSelection() { var flag = true; try { docRef.selection.bounds; } catch (e) { flag = false; } return flag; }; function createShapeLayer(top, left, bottom, right) { var dese = new ActionDescriptor(); var dese2 = new ActionDescriptor(); var dese3 = new ActionDescriptor(); var dese4 = new ActionDescriptor(); var dese5 = new ActionDescriptor(); var ref = new ActionReference(); ref.putClass(sTID("contentLayer")); dese.putReference(cTID("null"), ref); dese4.putDouble(cTID("Rd "), 0); dese4.putDouble(cTID("Grn "), 254); dese4.putDouble(cTID("Bl "), 0); dese3.putObject(cTID("Clr "), cTID("RGBC"), dese4); dese2.putObject(cTID("Type"), sTID("solidColorLayer"), dese3); dese5.putUnitDouble(cTID("Top "), cTID("#Pxl"), top); dese5.putUnitDouble(cTID("Left"), cTID("#Pxl"), left); dese5.putUnitDouble(cTID("Btom"), cTID("#Pxl"), bottom); dese5.putUnitDouble(cTID("Rght"), cTID("#Pxl"), right); dese2.putObject(cTID("Shp "), cTID("Rctn"), dese5); dese.putObject(cTID("Usng"), sTID("contentLayer"), dese2); executeAction(cTID("Mk "), dese, DialogModes.NO); }; function cTID(s) {return app.charIDToTypeID(s);} function sTID(s) {return app.stringIDToTypeID(s);}