• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

creating a unique text number in each copy of an image?

Community Beginner ,
Sep 18, 2021 Sep 18, 2021

Copy link to clipboard

Copied

I am making a 3D image that will contain 100 instgram like buttons. I am going to use these as sprintes in my 3D scene. I want the number of likes for each of the like button to have a unique number ranging from 0 to 299. I am guessing that this would require me to crate an action that will open a prexcistin image, assign a random number to a text field, flatten the image, then save the file in the png file format. I have never done scripting for photoshop but have done some basic coding in the past. Does anyone know where I might find examples of script or tutorials that I could modify for my purposes? If there is a plugin that does this also please let me know the name of it and where I might find it.

 likes.png

TOPICS
Actions and scripting

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Sep 19, 2021 Sep 19, 2021

Copy link to clipboard

Copied

I am not sure I understand your description. 

Why do you not want the editable number in the containing image but in a separate image? 

Even if so you could use Smart Objects. 

 

The randomization would require a Script, but please clarify the file and layer structure first. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 19, 2021 Sep 19, 2021

Copy link to clipboard

Copied

I want to generate a sequence of sprites for a 3D animation I am creating. Sprites are individual images/files that each look the same except for one small visual detail. In my case I want to create a sequences of sprites  that all look the same except that each one will have a unique number in the image. I have never done scripting on photoshop but here is how I image it would work in general terms; For each sprite the photoshop flle will have the same image in the background layer. I also imagine that there would be a layer1 that would contain a text field.  A script, plugin or action would populate the text field with a random number between 0-299, then flatten the image, then save the image as a png. The script would repeat this process so that I would have 300 individual pngs each with a unique number in the image. If you see the image I included in my intial post you can see how it has a number? I want to create several of those but each one should have a unique number.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 19, 2021 Sep 19, 2021

Copy link to clipboard

Copied

Please post the image or at least a screenshot including the Layers Panel. 

 

If the open image has a Type Layer as the active Layer this might work: 

 

// 2021, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is type layer;
if (theLayer.kind != "LayerKind.TEXT") {alert ("selected layer is not a type layer")}
else {
//
var theNumber = 30;
for (var m = 0; m < theNumber; m++) {
// replace numbers;
theLayer.textItem.contents = bufferNumberWithZeros(m,3);
//save psd;
myDocument.saveAs((new File(thePath+"/"+theName+"_"+bufferNumberWithZeros(m,3)+".psd")),psdOpts,true);
}
}
};
////// buffer number with zeros //////
function bufferNumberWithZeros (number, places) {
	var theNumberString = String(number);
	for (var o = 0; o < (places - String(number).length); o++) {
		theNumberString = String("0" + theNumberString)
		};
	return theNumberString
	};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2021 Sep 20, 2021

Copy link to clipboard

Copied

Do you have access to the full Creative Suite?

It might be easier to do in AE, for instance: How to create a Counter or Countdown in After Effects with Expressions

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2021 Sep 20, 2021

Copy link to clipboard

Copied

Another option would be a Photoshop text replacement Variable and dataset (AKA Data Driven Graphics) spreadsheet with the sequential numbers, they would be unique, either in series or random with a formula.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2021 Sep 20, 2021

Copy link to clipboard

Copied

I had overlooked the randomness in the Script I posted; here is an updated, if not exactly elegant, version, though for testing I reduced theNumber to 20. 

 

// 2021, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is type layer;
if (theLayer.kind != "LayerKind.TEXT") {alert ("selected layer is not a type layer")}
else {
// get numbers;
var theHighest = 300;
var theNumber = 20;
var theArray = new Array;
var resultArray = new Array;
for (var o = 1; o <= theHighest; o++) {
	theArray.push(bufferNumberWithZeros (o, 3));
	};
// random numbers;
for (var n = 0; n < theNumber; n++) {
var aRandom = Math.round(Math.random()*(theArray.length));
var thisNumber = theArray[aRandom];
resultArray.push(thisNumber);
theArray.splice(aRandom,1);
};
// replace number and save;
for (var m = 0; m < resultArray.length; m++) {
theLayer.textItem.contents = bufferNumberWithZeros(resultArray[m],3);
myDocument.saveAs((new File(thePath+"/"+theName+"_"+bufferNumberWithZeros(resultArray[m],3)+".psd")),psdOpts,true);
}
}
};
////// buffer number with zeros //////
function bufferNumberWithZeros (number, places) {
	var theNumberString = String(number);
	for (var o = 0; o < (places - String(number).length); o++) {
		theNumberString = String("0" + theNumberString)
		};
	return theNumberString
	};

 

 Screenshot 2021-09-20 at 17.50.27.pngScreenshot 2021-09-20 at 17.50.46.png

Edit: The screenshots show the content of the Folder in Finder before and after running the Script. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 06, 2021 Oct 06, 2021

Copy link to clipboard

Copied

Thanks for taking the time to write this. I have not used JS with photoshop before. Once I figured out how to load the script into PS it worked instantly.  You know your stuff. Thanks again.

 

 

Right now the soluion produces 3 digit numbers 195, 238, 003, 067, etc. Is there are a way for it to produce 1, 2 and 3 digit number? e.g. 5, 10, 216.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 07, 2021 Oct 07, 2021

Copy link to clipboard

Copied

Try changing the line 

 

myDocument.saveAs((new File(thePath+"/"+theName+"_"+bufferNumberWithZeros(resultArray[m],3)+".psd")),psdOpts,true);

 

to 

 

myDocument.saveAs((new File(thePath+"/"+theName+"_"+resultArray[m]+".psd")),psdOpts,true);

 

 

Sorry, just looked at the filename.

Please also change the line 

theLayer.textItem.contents = bufferNumberWithZeros(resultArray[m],3);

to 

theLayer.textItem.contents = Number(resultArray[m]);

I can’t test right now, but let me know if this does not work out. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 25, 2021 Oct 25, 2021

Copy link to clipboard

Copied

I swapped out the code and I received the following error:

Error 2:reultArray is undefined
Line: 21
-> theLayer.textItem.contents = Number(resultArray[m]);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2021 Nov 17, 2021

Copy link to clipboard

Copied

LATEST

Does this work? 

// 2021, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is type layer;
if (theLayer.kind != "LayerKind.TEXT") {alert ("selected layer is not a type layer")}
else {
// get numbers;
var theHighest = 300;
var theNumber = 20;
var theArray = new Array;
var resultArray = new Array;
for (var o = 1; o <= theHighest; o++) {
	theArray.push(o);
	};
// random numbers;
for (var n = 0; n < theNumber; n++) {
var aRandom = Math.round(Math.random()*(theArray.length));
var thisNumber = theArray[aRandom];
resultArray.push(thisNumber);
theArray.splice(aRandom,1);
};
// replace number and save;
for (var m = 0; m < resultArray.length; m++) {
theLayer.textItem.contents = resultArray[m];
myDocument.saveAs((new File(thePath+"/"+theName+"_"+resultArray[m]+".psd")),psdOpts,true);
}
}
};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2021 Oct 26, 2021

Copy link to clipboard

Copied

A random number does not not have to be a unique number it just needs to  one number in  your set of numbers. The range 0 to 299 is 300 numbers. Even if you beat the odd and it generate each with  without generating a duplicate number. Your random number 301  is guaranteed to be a duplicated number. You could generated a UUID or date and time stamp .  The Way the Image Processor scripts create unique file names is to  add a unique sequential number suffix.  The advantage of this is the suffix serial numbers are in order and the range of numbers is not limited to a small number like 299.  It tales longer to generate the name but It will be unique in the output folder

///////////////////////////////////////////////////////////////////////////////
// Function: CreateUniqueFileName
// Usage: Given a folder, filename, and extension, come up with a unique file name
// using a numbering system
// Input: string for folder, fileName, and extension, extension contains the "."
// Return: string for the full path to the unique file
///////////////////////////////////////////////////////////////////////////////
function CreateUniqueFileName( inFolder, inFileName, inExtension ) {
	inFileName = inFileName.replace(/[:\/\\*\?\"\<\>\|]/g, "_");  // '/\:*?"<>|' -> '_'
	var uniqueFileName = inFolder + inFileName + inExtension;
	var fileNumber = 1;
	while ( File( uniqueFileName ).exists ) {
		uniqueFileName = inFolder + inFileName + "_" + fileNumber + inExtension;
		fileNumber++;
	}
	return uniqueFileName;
}

]

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines