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!

Actions Random text action?


slivski

Member
Messages
10
Likes
0
I am new to the forums, hello! :wave::wave::wave:

In my first post I'd like to ask a question. I am just learning about PS actions and am wondering if there is a way to randomize text.
I've found a way to randomize colors which works by using a script, but how would one go about applying the same/similar method to letters and numbers?
http://www.bigresource.com/Tracker/Track-photoshop-S0bvnlVV/


If anyone has any ideas on this please respond!
Thanks in advance!
 
Last edited:
All you need to do is select a random array item IE:

Code:
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var nums ="0123456789";
alert(alphabet[Math.round (Math.random()* 25)]);
alert(nums[Math.round (Math.random()* 9)]);
 
Would it be possible to record an action within an action. i.e. press record, play an action, and stop recording to have both actions saved as one?
 
Yes you can record running other actions or if you are running a script you can run any amount of actions you like...

Code:
app.doAction("actionName", "actionSet");
 
I have absolutely 0 script experience and I am completely lost now, which is something I should have mentioned from the beginning lol...

How would I go about setting the script up, I mean which program/files do I use?
 
Here is the basics...

First, where do you find documentation?
Answer: On your own computer, it gets installed with your version of Photoshop.
In the following folders:
PC: C:\Program Files\Adobe\Adobe Photoshop CS#\Scripting Guide
MAC: <hard drive>/Applications/Adobe Photoshop CS#/ Scripting Guide
Here you will find :-
Photoshop Scripting Guide.pdf
AppleScript Reference Guide.pdf
JavaScript Reference Guide.pdf
VisualBasic Reference Guide.pdf
On a PC you can script using VisualBasic or JavaScript
On a Mac you can script using AppleScript or JavaScript
As you can see JavaScript is Cross Platform works on both PC and Mac so all the following will reference JavaScript.
To create/edit your scripts Adobe has kindly supplied you with an editor/debugger
This is called ExtendScript Toolkit)
This utility can be found in the relevant folder:-
PC: C:\Program Files\Adobe\Adobe Utilities
MAC: <hard drive>/Applications/Utilities/Adobe Utilities
Now for the ubiquitous Hello World program
Code:
 alert(“Hello World”);

Now you will need to run the code. There are a few ways this can be done.
From Extendscript Toolkit (With Photoshop selected from the dropdown box).
Save the script to anywhere you like or use the preferred folders
PC: C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts
MAC: <hard drive>/Applications/Adobe Photoshop CS#/ Presets/Scripts
Once the script has been saved, in Photoshop File – Scripts and select you script (if you have saved it in the preferred folder) or File – Scripts – Browse (to where you have saved it)
An action can be created to call a script or a keyboard shortcut via
Edit – Keyboard Shortcuts – File – Scripts
It is even possible to write a javaScript resource so that you script will show in the Filter or Help Menu.
For a few more javaScript examples look no further than your own machine:-
PC: C:\Program Files\Adobe\Adobe Photoshop CS#\Scripting Guide\Sample Scripts\JavaScript
MAC: <hard drive>/Applications/Adobe Photoshop CS#/ Scripting Guide/Sample Scripts/JavaScript
 
:banghead:

2 hours spent researching and learning javascript, and I'm right back to where I started :/. I know you would be going out of your way to do this, but do you think you'd be able to do the script for me? I'm sure it's actually really easy to do this, but I just can't wrap my head around it :/
 
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();
};
 
Wow. Kinda speechless right now, truly appreciate all your help.

I do have a question however before I get started I'm assuming that anything written after "//" is instructions for me, such as "// read all the text file into a var". Do these lines have to be erased before applying the script or are they fine the way they are?
 
Yes everything after // is a comment and can be left, could be useful if you want to alter anything.
 
Would there be a way to place the text in a specific location instead of the general middlecenter/bottomright?

Also, to re-size the text I'm assuming I have to use this line, but how does it work exactly?
doc.activeLayer.resize(percentageWidth,percentageWidth,AnchorPosition.MIDDLECENTER); //resize text

EDIT:
Nevermind, I figured out a way of doing it by simply recording the action of me resizing and placing the text in the desired location.

EDIT 2:
I've noticed that while running the script some of the words repeat. Is there a way to make sure each word shows up only once?
 
Last edited:
There are a couple of ways to stop the same selection but it depends on how the script is structured.
One way is to remove the word/line from the input file. The other way is to create an array of words used.
Both options will eventually end up with nothing to select.

More information would be required to help.
What are you trying to do?
Where is the source of the words?
Etc. Etc...
One example using JPGs is here..
http://www.ps-scripts.com/bb/viewtopic.php?f=9&t=4556&sid=cfd338e95838c6f88426542c6a9cd46a
 
I'm trying to watermark a large set of images with words (using an online random word generator to generate the words, which are saved in a text file on the desktop), but the words can't repeat themselves after they've been used. The watermark goes in the same place with the same settings every time, and then the image is saved in to an output folder.

I'm currently using the script you have set up for me on the first page, but if there is another way this could work I'd greatly appreciate the help
 
Here is the same code but modified to remove the selected line from the input file.
So backup the input file!
Of course this will only work until you run out of words/lines.

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 Item = Math.round (Math.random()* (data.length - 1));
var randomDat =data[Item];
data.splice(Item,1);
data = data.join('\n');
txtFile.open('w');
txtFile.write(data);
txtFile.close();
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();
};
 

Back
Top