Skip to main content
Inspiring
January 27, 2012
Answered

Script To Generate Parallel Lines in Photoshop

  • January 27, 2012
  • 3 replies
  • 1793 views

How can I generate parallel lines so that each line is spaced uniformly as it fills up the entire document? In the simple script below, I used the underscore to create the line. My problem seems to be that the succeeding lines (layers) are always aligned to the first layer in the iteration. How can that be fixed? Thanks in advanced.

// This script will create parallel lines which can be used for designing pages with lines

var defaultRulerUnits = preferences.rulerUnits;

preferences.rulerUnits = Units.PIXELS;

bgLine = new SolidColor();

      bgLine.rgb.red = 255;

      bgLine.rgb.green = 255;

      bgLine.rgb.blue = 255;

      backgroundColor = bgLine;

var newDocumentRef = documents.add(600,1000, 72.0, "Parallel Lines Generator");

newDocumentRef = null;

var textArray = [

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________",

            "_____________________________________________________________________________________"

] ;

//if additional lines are needed, it can be just added to the array

var AD = activeDocument ;

for(a=1;a<=textArray.length;a++){

      var TextLayer = AD.artLayers.add();

      TextLayer.kind = LayerKind.TEXT;

      TextLayer.opacity = 100;

     

       var txtRef = TextLayer.textItem;

            txtRef.font = "Arial";

            txtRef.contents = textArray[a-1];

            txtRef.size = 12;

            var textPosition = [30,  20]; // 30 is the margin and 20 is distance from top. The next text line should be 20 + 20 (can be any uniform increment desired)

            txtRef.position = textPosition;  // I cannot seem to get the next layer move down by a uniform increment, let us say 20 pixels

}

/*

// Merge all the Text Layers and leave the entire file unflattened

    var background = AD.layers[AD.layers.length -1];

    if (background.isBackgroundLayer) {

        background.visible = false;

        AD.mergeVisibleLayers();

        background.visible = true;

    }

// At this point, I like to rename the merged text layer with a layer name "Parallel Lines" before saving it as psd file.

*/

preferences.rulerUnits = defaultRulerUnits;

This topic has been closed for replies.
Correct answer Paul Riggott

And here is another alternative....

#target Photoshop
app.bringToFront();

function main(){
if(!documents.length) return;
var win = new Window( 'dialog', 'Draw Lines' );
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win.orientation='row';
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"});
win.g1 = win.p1.add('group');
win.g1.orientation = "row";
win.title = win.g1.add('statictext',undefined,'Draw Lines');
win.title.alignment="fill";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.g5 =win.p1.add('group');
win.g5.orientation = "row";
win.g5.alignment='fill';
win.g5.spacing=10;
win.g5.st1 = win.g5.add('statictext',undefined,'Number of lines Vertical');
win.g5.st1.preferredSize=[150,20];
win.g5.et1 = win.g5.add('edittext',undefined,'0');
win.g5.et1.preferredSize=[50,20];
win.g5.et1.onChanging = function() {
  if (this.text.match(/[^\-\.\d]/)) {
    this.text = this.text.replace(/[^\-\.\d]/g, '');
  }
};
win.g10 =win.p1.add('group');
win.g10.orientation = "row";
win.g10.alignment='fill';
win.g10.st1 = win.g10.add('statictext',undefined,'Number of lines Horizontal');
win.g10.st1.preferredSize=[150,20];
win.g10.et1 = win.g10.add('edittext',undefined,'0');
win.g10.et1.preferredSize=[50,20];
win.g10.et1.onChanging = function() {
  if (this.text.match(/[^\-\.\d]/)) {
    this.text = this.text.replace(/[^\-\.\d]/g, '');
  }
};
win.g15 =win.p1.add('group');
win.g15.orientation = "row";
win.g15.alignment='fill';
win.g10.st1 = win.g15.add('statictext',undefined,'Line Width');
win.g10.dd1 = win.g15.add('dropdownlist');
for(var t =1;t<21;t++){
    win.g10.dd1.add('item',t);
    }
win.g10.dd1.selection=0;
win.g100 =win.p1.add('group');
win.g100.orientation = "row";
win.g100.alignment='fill';
win.g100.bu1 = win.g100.add('button',undefined,'Add Lines');
win.g100.bu1.preferredSize=[110,25];
win.g100.bu2 = win.g100.add('button',undefined,'Cancel');
win.g100.bu2.preferredSize=[110,25];

win.g100.bu1.onClick=function(){
win.close(0);
var horz = Number(win.g10.et1.text);
var vert = Number(win.g5.et1.text);
var lineWidth = win.g10.dd1.selection.index + 1;
var crossHatch = activeDocument.artLayers.add();
crossHatch.name = "Draw Lines";
var spaceH = activeDocument.height.as('px')/(horz+1);
var spaceV = activeDocument.width.as('px')/(vert+1);
var spcH = spaceH;
var spcV = spaceV;
for(var a =0;a<horz;a++){
   lineHorzVert(spcH,'H',lineWidth);
   spcH +=spaceH;
}
for(var v =0;v<vert;v++){
    lineHorzVert(spcV,'V',lineWidth);
    spcV +=spaceV;
}
   
function lineHorzVert(space,HV,strokeWidth) {
if(HV.toLowerCase() == 'h'){
    HV = 'Sngr'; LT = 'Top ';
    }else{
         HV = 'Sngc'; LT = 'Left';
        }
var Black = new SolidColor();
Black.rgb.hexValue = '000000';
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
    desc.putReference( charIDToTypeID('null'), ref );
        var desc2 = new ActionDescriptor();
        desc2.putUnitDouble( charIDToTypeID(LT), charIDToTypeID('#Pxl'), space );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID(HV), desc2 );
    try{
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
    activeDocument.selection.stroke (Black, strokeWidth, StrokeLocation.CENTER, ColorBlendMode.NORMAL, 100, false);
    activeDocument.selection.deselect();
    }catch(e){}
}
}
win.center();
win.show();
}
main();

3 replies

Paul Riggott
Paul RiggottCorrect answer
Inspiring
January 28, 2012

And here is another alternative....

#target Photoshop
app.bringToFront();

function main(){
if(!documents.length) return;
var win = new Window( 'dialog', 'Draw Lines' );
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win.orientation='row';
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"});
win.g1 = win.p1.add('group');
win.g1.orientation = "row";
win.title = win.g1.add('statictext',undefined,'Draw Lines');
win.title.alignment="fill";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.g5 =win.p1.add('group');
win.g5.orientation = "row";
win.g5.alignment='fill';
win.g5.spacing=10;
win.g5.st1 = win.g5.add('statictext',undefined,'Number of lines Vertical');
win.g5.st1.preferredSize=[150,20];
win.g5.et1 = win.g5.add('edittext',undefined,'0');
win.g5.et1.preferredSize=[50,20];
win.g5.et1.onChanging = function() {
  if (this.text.match(/[^\-\.\d]/)) {
    this.text = this.text.replace(/[^\-\.\d]/g, '');
  }
};
win.g10 =win.p1.add('group');
win.g10.orientation = "row";
win.g10.alignment='fill';
win.g10.st1 = win.g10.add('statictext',undefined,'Number of lines Horizontal');
win.g10.st1.preferredSize=[150,20];
win.g10.et1 = win.g10.add('edittext',undefined,'0');
win.g10.et1.preferredSize=[50,20];
win.g10.et1.onChanging = function() {
  if (this.text.match(/[^\-\.\d]/)) {
    this.text = this.text.replace(/[^\-\.\d]/g, '');
  }
};
win.g15 =win.p1.add('group');
win.g15.orientation = "row";
win.g15.alignment='fill';
win.g10.st1 = win.g15.add('statictext',undefined,'Line Width');
win.g10.dd1 = win.g15.add('dropdownlist');
for(var t =1;t<21;t++){
    win.g10.dd1.add('item',t);
    }
win.g10.dd1.selection=0;
win.g100 =win.p1.add('group');
win.g100.orientation = "row";
win.g100.alignment='fill';
win.g100.bu1 = win.g100.add('button',undefined,'Add Lines');
win.g100.bu1.preferredSize=[110,25];
win.g100.bu2 = win.g100.add('button',undefined,'Cancel');
win.g100.bu2.preferredSize=[110,25];

win.g100.bu1.onClick=function(){
win.close(0);
var horz = Number(win.g10.et1.text);
var vert = Number(win.g5.et1.text);
var lineWidth = win.g10.dd1.selection.index + 1;
var crossHatch = activeDocument.artLayers.add();
crossHatch.name = "Draw Lines";
var spaceH = activeDocument.height.as('px')/(horz+1);
var spaceV = activeDocument.width.as('px')/(vert+1);
var spcH = spaceH;
var spcV = spaceV;
for(var a =0;a<horz;a++){
   lineHorzVert(spcH,'H',lineWidth);
   spcH +=spaceH;
}
for(var v =0;v<vert;v++){
    lineHorzVert(spcV,'V',lineWidth);
    spcV +=spaceV;
}
   
function lineHorzVert(space,HV,strokeWidth) {
if(HV.toLowerCase() == 'h'){
    HV = 'Sngr'; LT = 'Top ';
    }else{
         HV = 'Sngc'; LT = 'Left';
        }
var Black = new SolidColor();
Black.rgb.hexValue = '000000';
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID('Chnl'), charIDToTypeID('fsel') );
    desc.putReference( charIDToTypeID('null'), ref );
        var desc2 = new ActionDescriptor();
        desc2.putUnitDouble( charIDToTypeID(LT), charIDToTypeID('#Pxl'), space );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID(HV), desc2 );
    try{
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
    activeDocument.selection.stroke (Black, strokeWidth, StrokeLocation.CENTER, ColorBlendMode.NORMAL, 100, false);
    activeDocument.selection.deselect();
    }catch(e){}
}
}
win.center();
win.show();
}
main();

apmaui808Author
Inspiring
January 30, 2012

This is exactly the script I am looking for. Thanks a lot. Now I can make probably guest sign-books or customized notebooks with special images underneath...

c.pfaffenbichler
Community Expert
Community Expert
January 28, 2012

And why not simply create a Pattern? (Edit: And then use a Pattern Layer. )

JJMack already mentioned Selections, Paths would be another option for creating a Layer of lines.

apmaui808, could you please elaborate what exactly you are trying to achieve?

JJMack
Community Expert
Community Expert
January 28, 2012

If you want to draw lines why are you using text, Why not just add an empty layer get the document width and hight and loop through the document hieght  I=0  while I is <  height by some spacing factor  in each iterration make a new line selection at height i document wide and stroke the line selection..

JJMack