• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

Canvas Wrap Script

Community Expert ,
Mar 28, 2024 Mar 28, 2024

Copy link to clipboard

Copied

Several years ago, I posted a script on Adode Exchange for a script to do a canvas wrap. Lately a few have noticed that the inch setting doesn't work. Rather than trying to figure out how to post a new release, I'm just posting the script here, as it's a free script. Use at your own risk.

 

//Written by Chuck Uebele
//Copyright 2014 all rights reserved
//Use at your own rist.

#target photoshop

var oldPref = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS;

var flat = false; //Flattens the image 
var size = .75; //Size of canvas wrap depth
var useInch = true; //set false to use cm
var showUI = true; //set false to not have the user interface appear
var run = false; 
var cm = 1;



if(app.documents.length>0){
    if(showUI){
        var dlg = new Window('dialog','Canvas Wrap');
        dlg.gp1 = dlg.add('group');
            dlg.gp1.orientation = 'row'
            dlg.gp1.sTxt = dlg.gp1.add('statictext',undefined,'Canvas Wrap Size:');
            dlg.gp1.eTxt = dlg.gp1.add('edittext',undefined,size);
                dlg.gp1.eTxt.onChange = function(){
                    textToNum(0,20,this,'float')
                    }
            
            dlg.gp1.eTxt.size = [50,18];
            dlg.flat = dlg.add('checkbox',undefined,'Flatten Image');
            dlg.gp1.inch = dlg.gp1.add('radiobutton',undefined,'inches');
            dlg.gp1.cm = dlg.gp1.add('radiobutton',undefined,'cm');
            dlg.btnGp = dlg.add('group');
            dlg.btnGp.ok = dlg.btnGp.add('button',undefined,'Okay');
            dlg.btnGp.cancel = dlg.btnGp.add('button',undefined,'cancel');
            dlg.gp1.inch.value = true
            
            dlg.btnGp.ok.onClick = function(){
                if(!isNaN (Number(dlg.gp1.eTxt.text))){
                    size = Number(dlg.gp1.eTxt.text);
                    run = true
                    if(dlg.gp1.cm.value){useInch = false}
                    flat = dlg.flat.value
                    dlg.close()
                    }
                }
            
            dlg.btnGp.cancel.onClick = function(){
                run = false
                dlg.close()
                }
        
        dlg.show() 
        }
    else{run = true}
    }
else{alert('There is no open document')}

if(run){     
    if(useInch){cm = 1}
    else{cm = 2.54}
    var doc = activeDocument;
    var res = doc.resolution;
    var actSize = Math.round(size/cm * doc.resolution);
    doc.activeLayer = doc.layers[0];
    var wrapLayer = doc.artLayers.add();
    stamp ();
    
    wrapLayer = doc.activeLayer
    wrapLayer.name = 'wrap'
    makeSel (0, 0, doc.width, actSize)
    dup ('Hght')
    var dupLayer = doc.activeLayer
    dupLayer.translate (0, -actSize)
    merge ()
        
    makeSel (0, doc.height-actSize, doc.width, doc.height)
    dup ('Hght')
    dupLayer = doc.activeLayer
    dupLayer.translate (0, actSize)
    merge ()
    doc.resizeCanvas(doc.width,doc.height + 2* actSize) 
    
    
    makeSel (0, 0, actSize, doc.height)
    dup ('Wdth')
    dupLayer = doc.activeLayer
    dupLayer.translate (-actSize,0)
    merge ()
    
    makeSel (doc.width-actSize, 0, doc.width, doc.height)
    dup ('Wdth')
    dupLayer = doc.activeLayer
    dupLayer.translate (actSize,0)
    merge ()
    doc.resizeCanvas(doc.width + 2* actSize,doc.height)
    //doc.trim();
    if (flat){doc.flatten()}
    }



app.preferences.rulerUnits = oldPref

function stamp(){
    var idMrgV = charIDToTypeID( "MrgV" );
        var desc3 = new ActionDescriptor();
        var idDplc = charIDToTypeID( "Dplc" );
        desc3.putBoolean( idDplc, true );
    executeAction( idMrgV, desc3, DialogModes.NO );    
    }

function makeSel(lft,tp,rt,bt){
    var area = [[lft,tp],[rt,tp],[rt,bt],[lft,bt]]
    doc.selection.select(area)
    };//end function makeSelection
    

function dup(orient){
    var idCpTL = charIDToTypeID( "CpTL" );
    executeAction( idCpTL, undefined, DialogModes.NO );
    //===========================================
    var idTrnf = charIDToTypeID( "Trnf" );
        var desc7 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref4 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            var idOrdn = charIDToTypeID( "Ordn" );
            var idTrgt = charIDToTypeID( "Trgt" );
            ref4.putEnumerated( idLyr, idOrdn, idTrgt );
        desc7.putReference( idnull, ref4 );
        var idFTcs = charIDToTypeID( "FTcs" );
        var idQCSt = charIDToTypeID( "QCSt" );
        var idQcsa = charIDToTypeID( "Qcsa" );
        desc7.putEnumerated( idFTcs, idQCSt, idQcsa );
        var idOfst = charIDToTypeID( "Ofst" );
            var desc8 = new ActionDescriptor();
            var idHrzn = charIDToTypeID( "Hrzn" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc8.putUnitDouble( idHrzn, idPxl, 0.000000 );
            var idVrtc = charIDToTypeID( "Vrtc" );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc8.putUnitDouble( idVrtc, idPxl, 0.000000 );
        var idOfst = charIDToTypeID( "Ofst" );
        desc7.putObject( idOfst, idOfst, desc8 );
        var idHght = charIDToTypeID( orient );
        var idPrc = charIDToTypeID( "#Prc" );
        desc7.putUnitDouble( idHght, idPrc, -100.000000 );
        var idIntr = charIDToTypeID( "Intr" );
        var idIntp = charIDToTypeID( "Intp" );
        var idBcbc = charIDToTypeID( "Bcbc" );
        desc7.putEnumerated( idIntr, idIntp, idBcbc );
    executeAction( idTrnf, desc7, DialogModes.NO );

}

function merge(){
            var idMrgtwo = charIDToTypeID( "Mrg2" );
        var desc15 = new ActionDescriptor();
    executeAction( idMrgtwo, desc15, DialogModes.NO );
}

function textToNum(sMin,sMax,e,par){
	var sHold = sMin;
	
	if(par == 'int'){
		if(isNaN(parseInt(e.text))){
			alert('"' + e.text + '" is not a number\nEnter a value between ' + sMin + '-' + sMax );
			e.text = sMin};
		else{sHold = parseInt(e.text)
			if(sHold < sMin){
				rangeAlert();
				sHold = sMin;				
				};
			if(sHold > sMax){
				rangeAlert();
				sHold = sMax;
				};
			e.text = sHold;
			};	//end else
		};//end if par
	else{
		if(isNaN(parseFloat(e.text))){
			alert('"' + e.text + '" is not a number\nEnter a value between ' + sMin + '-' + sMax );
			e.text = sMin};
		else{sHold = parseFloat(e.text)
			if(sHold < sMin){
				rangeAlert();
				sHold = sMin;
				};
			if(sHold > sMax){
				rangeAlert();
				sHold = sMax;
				};
			e.text = sHold;
			};	//end else
		};//end else par
	function rangeAlert(){alert('Number range must be between ' + sMin + '-' + sMax)}; 
};//end function textToNum


TOPICS
Actions and scripting

Views

91

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

LATEST

Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines