Photoshop Script for getting size and position of layers
-
Hey,
I looked around on here and couldn't find anything like this, and figured it may help some people.
Maybe there is an easier way, but when I am creating themes I use tons of guides for position and sizing, which I imagine most people do. I also use those to figure my coordinates/size when making my XML file. I manually get the width / height and divide them from the total size, typically 1920 x 1080.
Again, if there is a better way, I was never aware, so I looked into a Photoshop Script that will return your size and coordinates. Then using that script, I altered it a bit to take the values and divide them by the document width and the document height and return as decimal values wrapped in <pos> and <size> tags.
So if you name your layers, and organize your PSD, this makes it a breeze to get the values.
It will take this:
and make this:
Layer 0 <pos>0 0</pos> <size>1 1</size> BORDER <pos>0.16145833333333 0.15740740740741</pos> <size>0.68177083333333 0.69351851851852</size> Layer 8 <pos>0.17239583333333 0.26388888888889</pos> <size>0.65520833333333 0.51203703703704</size> Layer 7 <pos>0.17239583333333 0.26388888888889</pos> <size>0.65520833333333 0.51203703703704</size> Layer 8 copy 2 <pos>0.17239583333333 0.26388888888889</pos> <size>0.65520833333333 0.51203703703704</size> TOP BAR <pos>0.1671875 0.16759259259259</pos> <size>0.665625 0.0962962962963</size> BOTTOM BAR <pos>0.1671875 0.26388888888889</pos> <size>0.665625 0.56851851851852</size> GAME BAR <pos>0.17239583333333 0.23425925925926</pos> <size>0.65520833333333 0.02407407407407</size> VIEW <pos>0.7921875 0.24166666666667</pos> <size>0.03020833333333 0.00925925925926</size> AVATAR <pos>0.80885416666667 0.78981481481481</pos> <size>0.01875 0.03333333333333</size>
I didn't properly set up my files, I was simply seeing if I could get this to work and it worked great.
Here is the code if anyone is interested, hopefully it can help save some time
(I did not write this code, I got it from here: https://forums.adobe.com/thread/1064028 and just reworked it a bit to suit my needs)To run it save as a JS file, and with your PSD files opened, go to
File > Scripts > Browse > [filename].js
#target photoshop main(); function main(){ if(!documents.length) return; var myWidth = app.activeDocument.width.toString().replace(' px', ''); var myHeight = app.activeDocument.height.toString().replace(' px', ''); var Info = getNamesPlusIDs(); var Name = app.activeDocument.name.replace(/\.[^\.]+$/, ''); var path = app.activeDocument.path; var file = new File(path + "/" + Name + "-coords.txt"); file.open("w", "TEXT", "????"); $.os.search(/windows/i) != -1 ? file.lineFeed = 'windows' : file.lineFeed = 'macintosh'; for(var a in Info) file.writeln(Info[a][0] + " <pos>" + Info[a][1] / myWidth + " " + Info[a][2] / myHeight + "</pos> <size>" + Info[a][3] / myWidth + " " + Info[a][4] / myHeight + "</size>"); file.close(); } function getNamesPlusIDs(){ var ref = new ActionReference(); ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1; var Names=[]; try{ activeDocument.backgroundLayer; var i = 0; }catch(e){ var i = 1; }; for(i;i<count;i++){ if(i == 0) continue; ref = new ActionReference(); ref.putIndex( charIDToTypeID( 'Lyr ' ), i ); var desc = executeActionGet(ref); var layerName = desc.getString(charIDToTypeID( 'Nm ' )); var Id = desc.getInteger(stringIDToTypeID( 'layerID' )); if(layerName.match(/^<\/Layer group/) ) continue; var vMask = desc.getBoolean(stringIDToTypeID('hasVectorMask' )); try{ var adjust = typeIDToStringID(desc.getList (stringIDToTypeID('adjustment')).getClass (0)); if(vMask == true){ adjust = false; var Shape = true; } }catch(e){var adjust = false; var Shape = false;} var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' ))); var isLayerSet =( layerType == 'layerSectionContent') ? false:true; var Vis = desc.getBoolean(stringIDToTypeID( 'visible' )); var descBounds = executeActionGet(ref).getObjectValue(stringIDToTypeID( "bounds" )); var X = descBounds.getUnitDoubleValue(stringIDToTypeID('left')); var Y = descBounds.getUnitDoubleValue(stringIDToTypeID('top')); var Wt = descBounds.getUnitDoubleValue(stringIDToTypeID('width')); var Ht = descBounds.getUnitDoubleValue(stringIDToTypeID('height')); if(Vis && !isLayerSet && !adjust) Names.push([[layerName],[X],[Y], [Wt], [Ht]]); }; return Names; };
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.