Created
March 2, 2022 06:48
-
-
Save s2kw/1fd8cfd409b3f812f906c87b36954498 to your computer and use it in GitHub Desktop.
レイヤーのサイズ情報をJSONで入出力できるようにしたコード。Photoshop用。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#target photoshop | |
#include json2.jsx; | |
/// class 的なやつにしたいが…くそ!くそ!!! | |
function CreateByJSON( jsonString ){ | |
var o = new Object(); | |
try{ | |
var obj = JSON.parse( RemoveSpecificCharacterFromJSON( jsonString )); | |
o.x = obj.x; | |
o.y = obj.y; | |
o.width = obj.width; | |
o.height = obj.height; | |
}catch(error){ | |
alert(error); | |
} | |
o = AddFunctions(o); | |
return o; | |
}; | |
function CreateByParams( _x, _y, _height, _width ){ | |
var o = new Object(); | |
o.x = _x; | |
o.y = _y; | |
o.width = _width; | |
o.height = _height; | |
o = AddFunctions(o); | |
return o; | |
}; | |
function CreateByLayer( layer ){ | |
var o = new Object(); | |
var currentBounds = layer.bounds; | |
o.x = currentBounds[0].as('px'); | |
o.y = currentBounds[1].as('px'); | |
o.width = currentBounds[2].as('px') - currentBounds[0].as('px'); | |
o.height = currentBounds[3].as('px') - currentBounds[1].as('px'); | |
o = AddFunctions(o); | |
return o; | |
}; | |
function AddFunctions(o){ | |
// JSON形式で出力 | |
o.JSONText = function(){ | |
return "{\"x\":"+o.x+",\"y\":"+o.y+",\"width\":"+o.width+",\"height\":"+o.height+"}"; | |
}; | |
o.JSONObj = function(){ | |
return JSON.parse( RemoveSpecificCharacterFromJSON( o.JSONText() )); | |
}; | |
o.Bounds = function(){ | |
return "[" + o.x + "," + o.y + "," + o.width + "," + o.height + "]"; | |
}; | |
o.IsJustFit = function(layer){ | |
var layerObj = CreateByLayer(layer); | |
if (o.x != layerObj.x) return false; | |
if (o.y != layerObj.y) return false; | |
if (o.width != layerObj.width) return false; | |
if (o.height != layerObj.height) return false; | |
return true; | |
} | |
o.ApplyToLayer = function(layer){ | |
var layerObj = CreateByLayer(layer); | |
// alert( "currentLayer:" + layerObj.JSONText()); | |
// alert("layerObj.width / obj.width::" + layerObj.width + " / " + obj.width); | |
var scaleX = ( parseFloat(o.width) / parseFloat(layerObj.width)) * 100; | |
// 高さ | |
// alert("layerObj.height / obj.height::" +layerObj.height + "/" + obj.height); | |
var scaleY = (parseFloat(o.height) / parseFloat(layerObj.height)) * 100; | |
// alert("scaleX:"+scaleX + " scaleY:"+scaleY); | |
var deltaX = (o.x - layerObj.x) +'px'; | |
var deltaY = (o.y - layerObj.y) +'px'; | |
// alert(deltaX + ":" + deltaY); | |
// スケール | |
layer.resize(scaleX, scaleY, AnchorPosition.MIDDLECENTER); | |
layer.translate( deltaX, deltaY); | |
// 結果 | |
// alert( "delta:" + deltaX + ":" + deltaY + "\n,,,," + o.JSONText() +" => "+ CreateByLayer(layer).JSONText()); | |
// フィットするまで再帰 | |
if( !o.IsJustFit(layer) )o.ApplyToLayer(layer); | |
}; | |
return o; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment