Thursday, 19 February 2015

Copy The Current Selection Bounds To X, Y, Width, Height To AS3 code

This JSX script for Photoshop allows you copy to the clipboard current layer properties to AS3 code in the format:
&selectionBounds.x = &layerx; &selectionBounds.y = &layery; // &selectionBounds.width = &layerwidth; // &selectionBounds.height = &layerheight;

Instructions on how to use this script:

  1. Save the script as SelectionBoundsToAS3.jsx
  2. Open Photoshop
  3. Select a layer in your PSD
  4. Go to File > Scripts > Browse > Select the script file (SelectionBoundsToAS3)

if ( app.documents.length > 0 ) {
 
preferences.rulerUnits = Units.PIXELS;
var layerRef = app.activeDocument.selection;
var layerName = "selectionBounds";
 
var x = layerRef.bounds[0].value;
var y = layerRef.bounds[1].value;
var width = layerRef.bounds[2].value - x;
var height = layerRef.bounds[3].value - y;
 
var clp = "";
clp += "\t\t" + layerName + ".x = " + x +";\r";
clp += "\t\t" + layerName + ".y = " + y +";\r";
clp += "\t\t// " + layerName + ".width = " + width +";\r";
clp += "\t\t// " + layerName + ".height = " + height +";\r";
 
copyTextToClipboard(clp);
} else {
 alert("You must have one opened document at least!");
}
 
function copyTextToClipboard(txt){
    const keyTextData = app.charIDToTypeID('TxtD');
    const ktextToClipboardStr = app.stringIDToTypeID( "textToClipboard" );
    var textStrDesc = new ActionDescriptor();
    textStrDesc.putString( keyTextData, txt );
    executeAction( ktextToClipboardStr, textStrDesc, DialogModes.NO );
} 

No comments:

Post a Comment