'Photoshop scripting (.jsx): push string into an Array only if it doesn't exist there yet
I have a unique signature being generated for each iteration, which I want to eventually save on a .txt file.
My signature is quite simple, using 0-9 and A-Z, being 14 chars long.
I want to push it to an Array only if that Array doesn't contain it yet. Here is what I tried to do:
var uniqueSignature = []
do {
var uniqueSignature = generateSignature()
if (!signatureArray.includes(uniqueSignature)){ //checks if the Array does not contain the signature.
signatureArray.push(uniqueSignature) //pushes the signature into the array.
}
} while (!signatureArray.includes(uniqueSignature)) //Keep doing so until the signature can be inserted (meaning is unique)
function generateSignature(){
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var signatureLength = 14
var genSignature = ""
for (var i = 0; i <= signatureLength; i++) {
var randomChars = Math.floor(Math.random() * chars.length)
genSignature += chars.substring(randomChars, randomChars +1)
}
currentSignature = genSignature
}
//[... eventually will write the array to a .txt files]
When I try to run it on Adobe Photoshop > Browse > Load script, I get this error:

I've also tried to replace .includes for .indexOf, pushing it only if signatureArray.indexOf(uniqueSignature) == -1, but got the same error.
Any ideas? I know that jsx/Photoshop has some limitations when it comes to code (like it won't let me use ${var} and such). Is that the thing here?
If so, is there any other way for me to achieve this push after verification?
Solution 1:[1]
I've managed to bypass this by working with a String instead of an Array:
var currentSignature
var z = 0
do {
generateSignature()
if (signatureString.indexOf(currentSignature) == -1){
signatureString = signatureString + currentSignature + '\n'
z = 1
}
} while (z = 0)
In the future, I can pass a .join('\n') if I need to convert the string (file content) into an Array again.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
