Script to stroke, change canvas size and place file name
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:
- In a file, select all and create a stroke, CMYK 100%Y, 2px, inside. Works
- Increase canvas size by 30px (1cm in 72dpi) on the top side.
- 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!" );
}
