Skip to main content
Participant
May 16, 2017
Answered

Script to stroke, change canvas size and place file name

  • May 16, 2017
  • 3 replies
  • 1790 views

Hello to you all.

I am at least new to scripting and even more to Photoshop.

The case:

I am trying to use JavaScript to create a way to make a repeatable process in Photoshop.

What I want to do is the following:

  1. In a file, select all and create a stroke, CMYK 100%Y, 2px, inside. Works
  2. Increase canvas size by 30px (1cm in 72dpi) on the top side.
  3. Place the file name in the top-left corner of the canvas. Works

In case we can do it, it would be the best to use this in a a bunch of files.

Following what I have already done. Be gentle, it is a puzzle of internet, actions and me.

// this script is a variation of the script addTimeStamp.js that is installed with PH7

//

//==================== stroke 2px and canvas size 1cm top ==============

//

#target photoshop

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

function stroke2pxandcanvassize1cmtop() {

  // Set

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Chnl'), sTID("selection"));

    desc1.putReference(cTID('null'), ref1);

    desc1.putEnumerated(cTID('T   '), cTID('Ordn'), cTID('Al  '));

    executeAction(cTID('setd'), desc1, dialogMode);

  };

  // Stroke

  function step2(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putInteger(cTID('Wdth'), 2);

    desc1.putEnumerated(cTID('Lctn'), cTID('StrL'), cTID('Cntr'));

    desc1.putUnitDouble(cTID('Opct'), cTID('#Prc'), 100);

    desc1.putEnumerated(cTID('Md  '), cTID('BlnM'), cTID('Nrml'));

    var desc2 = new ActionDescriptor();

    desc2.putDouble(cTID('Cyn '), 0);

    desc2.putDouble(cTID('Mgnt'), 0);

    desc2.putDouble(cTID('Ylw '), 100);

    desc2.putDouble(cTID('Blck'), 0);

    desc1.putObject(cTID('Clr '), cTID('CMYC'), desc2);

    executeAction(cTID('Strk'), desc1, dialogMode);

  };

  step1();      // Set

  step2();      // Stroke

  };

//=========================================

//                    stroke2pxandcanvassize1cmtop.main

//=========================================

//

stroke2pxandcanvassize1cmtop.main = function () {

  stroke2pxandcanvassize1cmtop();

};

stroke2pxandcanvassize1cmtop.main();

// EOF

"stroke2pxandcanvassize1cmtop.jsx"

// EOF

  // Canvas Size

 

    var w = app.activeDocument.width;// the width of the document 

    var h = app.activeDocument.height; 

    app.activeDocument.resizeCanvas (w, h+40);   

//

//==================== Add File Name 20 pt Top-Left side ==============

//

if ( documents.length > 0 )

{

    var originalDialogMode = app.displayDialogs;

    app.displayDialogs = DialogModes.ERROR;

    var originalRulerUnits = preferences.rulerUnits;

    preferences.rulerUnits = Units.PIXELS;

   

    try

    {

        var docRef = activeDocument;

        // Now create a text layer at the front

        var myLayerRef = docRef.artLayers.add();

        myLayerRef.kind = LayerKind.TEXT;

        myLayerRef.name = "Filename";

       

        var myTextRef = myLayerRef.textItem;

       

        // strip the extension off

        var fileNameNoExtension = docRef.name;

        fileNameNoExtension = fileNameNoExtension.split( "." );

        if ( fileNameNoExtension.length > 1 ) {

            fileNameNoExtension.length--;

        }

        fileNameNoExtension = fileNameNoExtension.join(".");

           

        myTextRef.contents = fileNameNoExtension;

       

        // Size and position of text

        myTextRef.size = 45;

        myTextRef.position = new Array( 0, 35 );

       

    }

    catch( e )

    {

        // An error occurred. Restore ruler units, then propagate the error back

        // to the user

        preferences.rulerUnits = originalRulerUnits;

        app.displayDialogs = originalDialogMode;

        throw e;

    }

    // Everything went Ok. Restore ruler units

    preferences.rulerUnits = originalRulerUnits;

    app.displayDialogs = originalDialogMode;

}

else

{

    alert( "You must have a document open to add the filename!" );

}

This topic has been closed for replies.
Correct answer pixxxelschubser

Hi Spadau,

try this:

// strokeEnlargeCanvasSizeAndPlaceFilename.jsx

// adds a 2px yellow stroke inside, enlarges canvas size 1cm on the top side and places the file name at upper left corner

// https://forums.adobe.com/thread/2316273

// regards pixxxel schubser

if ( documents.length > 0 ) {

var aDoc = activeDocument;

var strtRulerUnits = preferences.rulerUnits;

preferences.rulerUnits = Units.PIXELS;

try {

    aDoc.activeLayer = aDoc.layers[0];

    strokeInside2pxYellow ();

    } catch (e) {

        aDoc.artLayers.add ();

        selectCanvas ();

        strokeInside2pxYellow ();

        }

    var w = app.activeDocument.width;

    var h = app.activeDocument.height;

    var factor = Math.round ( aDoc.resolution/2.54 ); // 1cm

    app.activeDocument.resizeCanvas ( w, h+factor, AnchorPosition.BOTTOMCENTER );

try {

    var fileNameNoExtension = aDoc.name.split (".");

    if ( fileNameNoExtension.length > 1 ) { fileNameNoExtension.length--; }

    fileNameNoExtension = fileNameNoExtension.join (".");

    var txtLay = aDoc.artLayers.add ();

    txtLay.kind = LayerKind.TEXT;

    var txt = txtLay.textItem;

    var posY = 35; //35 = position (72ppi)

    txt.position = [0, (factor*posY/28)]; // 28 = 1cm (72ppi);

    txt.size = 45;

    txt.contents = fileNameNoExtension;

    } catch (e) { preferences.rulerUnits = strtRulerUnits; }

    preferences.rulerUnits = strtRulerUnits;   

    }

function cTID(s) { return app.charIDToTypeID(s); };

function selectCanvas () {

    (ref1 = new ActionReference()).putProperty ( cTID('Chnl'), cTID('fsel') );

    (desc1 = new ActionDescriptor()).putReference ( cTID('null'), ref1 );

    desc1.putEnumerated ( cTID('T   '), cTID('Ordn'), cTID('Al  ') );

    executeAction ( cTID('setd'), desc1, DialogModes.NO );

}

function strokeInside2pxYellow () {

    (desc1 = new ActionDescriptor()).putInteger( cTID('Wdth'), 2 );

    desc1.putEnumerated( cTID('Lctn'), cTID('StrL'), cTID('Insd') );

    desc1.putUnitDouble( cTID('Opct'), cTID('#Prc'), 100 );

    desc1.putEnumerated( cTID('Md  '), cTID('BlnM'), cTID('Nrml') );

    (desc2 = new ActionDescriptor()).putDouble( cTID('Cyn '), 0 );

    desc2.putDouble( cTID('Mgnt'), 0 );

    desc2.putDouble( cTID('Ylw '), 100 );

    desc2.putDouble( cTID('Blck'), 0 );

    desc1.putObject( cTID('Clr '), cTID('CMYC'), desc2)

    executeAction( cTID('Strk'), desc1, DialogModes.NO);

}

This snippet should works with several resolutions. If necessary, then simply add a loop through your files.

Have fun

3 replies

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
May 17, 2017

Hi Spadau,

try this:

// strokeEnlargeCanvasSizeAndPlaceFilename.jsx

// adds a 2px yellow stroke inside, enlarges canvas size 1cm on the top side and places the file name at upper left corner

// https://forums.adobe.com/thread/2316273

// regards pixxxel schubser

if ( documents.length > 0 ) {

var aDoc = activeDocument;

var strtRulerUnits = preferences.rulerUnits;

preferences.rulerUnits = Units.PIXELS;

try {

    aDoc.activeLayer = aDoc.layers[0];

    strokeInside2pxYellow ();

    } catch (e) {

        aDoc.artLayers.add ();

        selectCanvas ();

        strokeInside2pxYellow ();

        }

    var w = app.activeDocument.width;

    var h = app.activeDocument.height;

    var factor = Math.round ( aDoc.resolution/2.54 ); // 1cm

    app.activeDocument.resizeCanvas ( w, h+factor, AnchorPosition.BOTTOMCENTER );

try {

    var fileNameNoExtension = aDoc.name.split (".");

    if ( fileNameNoExtension.length > 1 ) { fileNameNoExtension.length--; }

    fileNameNoExtension = fileNameNoExtension.join (".");

    var txtLay = aDoc.artLayers.add ();

    txtLay.kind = LayerKind.TEXT;

    var txt = txtLay.textItem;

    var posY = 35; //35 = position (72ppi)

    txt.position = [0, (factor*posY/28)]; // 28 = 1cm (72ppi);

    txt.size = 45;

    txt.contents = fileNameNoExtension;

    } catch (e) { preferences.rulerUnits = strtRulerUnits; }

    preferences.rulerUnits = strtRulerUnits;   

    }

function cTID(s) { return app.charIDToTypeID(s); };

function selectCanvas () {

    (ref1 = new ActionReference()).putProperty ( cTID('Chnl'), cTID('fsel') );

    (desc1 = new ActionDescriptor()).putReference ( cTID('null'), ref1 );

    desc1.putEnumerated ( cTID('T   '), cTID('Ordn'), cTID('Al  ') );

    executeAction ( cTID('setd'), desc1, DialogModes.NO );

}

function strokeInside2pxYellow () {

    (desc1 = new ActionDescriptor()).putInteger( cTID('Wdth'), 2 );

    desc1.putEnumerated( cTID('Lctn'), cTID('StrL'), cTID('Insd') );

    desc1.putUnitDouble( cTID('Opct'), cTID('#Prc'), 100 );

    desc1.putEnumerated( cTID('Md  '), cTID('BlnM'), cTID('Nrml') );

    (desc2 = new ActionDescriptor()).putDouble( cTID('Cyn '), 0 );

    desc2.putDouble( cTID('Mgnt'), 0 );

    desc2.putDouble( cTID('Ylw '), 100 );

    desc2.putDouble( cTID('Blck'), 0 );

    desc1.putObject( cTID('Clr '), cTID('CMYC'), desc2)

    executeAction( cTID('Strk'), desc1, DialogModes.NO);

}

This snippet should works with several resolutions. If necessary, then simply add a loop through your files.

Have fun

SpadauAuthor
Participant
May 18, 2017

I 've made some correction on text positioning and size. It works like charm!

If you are interested in some more work, paid this time, please let me now. I can use you with so many ways! 

pixxxelschubser
Community Expert
Community Expert
May 20, 2017

You're welcome.

JJMack
Community Expert
Community Expert
May 16, 2017

As I wrote search the web for Photoshop scripts like Frameshop and others read download and read their code. Learn from it.

https://www.google.com/search?num=50&newwindow=1&biw=1377&bih=807&q=Photoshop+frameshop&oq=Photoshop+frameshop&gs_l=serp…

If it is in binary format like fameshop now is find an other thee all will not be binary.

https://www.google.com/search?num=50&newwindow=1&biw=1377&bih=758&q=photoshop+image+framing+script+exif+description&oq=P…

JJMack
JJMack
Community Expert
Community Expert
May 16, 2017

What bother me about you description

Increase canvas size by 30px (1cm in 72dpi) on the top side.
Place the file name in the top-left corner of the canvas.

Increasing the canvas  on top by 30 PX is no big deal.  If the document does not have a 72DPI resolution the canvas size increase will not be what you wrote  in CM if the document was an 11x14 300  print resolution document and you change its resolution to 72DPI it will now be a 45"x58" 72DPI Poster size image.   The Font size use also needs to fit within in the canvas added.  I did not look at the code because of your description. Your code may be perfect I do not know.

There are several Photoshop script posted on the web that do thing like this Add frames mats and text.  Download some and read the code if you are just starting to use Photoshop scripting. You need to know well how Photoshop works to be able to script it. See how other deal with things like resolution and font size.

JJMack
SpadauAuthor
Participant
May 16, 2017

You are right in everything you said except of that it maybe my code works.. it doesn't.

We take as granted that my files will be at 72 dpi. In case there is a change in this, i could manually or someday automatically change it.

The text part i got it from another script and it works, i can set its height and position...

The problem is that i can t find code for changing the canvas size according to original size. (+30px on height)

Of course, the way i have build this code makes it messy, slow and difficult to read. Maybe i ll be able to optimise it in the future..

Thank you for your prompt response!