What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Reply to thread

This example will select a random line from a text file.

The code assumes that there is a text file on the Desktop called poem.txt

If a document is not open a new document will be created.

The random line of text will be put on a new text  layer.


[code]

#target photoshop

function main(){

var txtFile = File(Folder.desktop + "/poem.txt"); //text file to get a line from

if(!txtFile.exists){

    alert("Cann't find Poem.txt on the desktop!");

    return;

    }

txtFile.open('r'); //open the text file

var  data = txtFile.read(); // read all the text file into a var

txtFile.close(); // close the text file

data = data.split('\n'); //create an array with all lines from the text file

var randomLine='';

while (randomLine.length < 3){ //make sure it is not a blank line

randomLine = randomData();

}

///////////////// ok now have a random line /////////

if(!documents.length){ //create a new document if one is not open

app.documents.add(new UnitValue( 800, 'px' ), new UnitValue( 600, 'px' ), 72, "Random Test");

}

var Percent = 90;  //length of text to width of document

var Black = new SolidColor();

Black.rgb.hexValue = '000000'; //set colour of text

var newTextLayer = activeDocument.artLayers.add(); //add a new layer

newTextLayer.kind = LayerKind.TEXT;

newTextLayer.textItem.kind = TextType.POINTTEXT;

newTextLayer.textItem.color = Black;

newTextLayer.textItem.font = "Georgia"; //font name (must be the postscript name)

newTextLayer.textItem.size = 10;

newTextLayer.textItem.contents = randomLine; //this will be the random line

var startRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS; // set units to pixels

var doc = activeDocument; //make a referance to the open document

var LB = doc.activeLayer.bounds; //get the layer bounds

var docHeight = doc.height; //doc height

var docWidth = doc.width; //doc width

var LWidth = Math.abs(LB[2].value) - Math.abs(LB[0].value);   

var percentageWidth = ((docWidth/LWidth)*Percent);

doc.activeLayer.resize(percentageWidth,percentageWidth,AnchorPosition.MIDDLECENTER); //resize text

align('AdCH'); align('AdCV'); //center the text Horizontal and vertical

function randomData(){

var randomDat =data[Math.round (Math.random()* (data.length - 1))];

return randomDat;

}

}

main();

function align(method) {

activeDocument.selection.selectAll();

   var desc = new ActionDescriptor();

           var ref = new ActionReference();

           ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

       desc.putReference( charIDToTypeID( "null" ), ref );

       desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );

    try{

   executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );

   }catch(e){}

   activeDocument.selection.deselect();

};


[/code]


What is our favorite program/app? (Hint - it begins and ends with the letter P)
Back
Top