Copy link to clipboard
Copied
Hi all.
I'm trying to write a script that will get all the existing Custom Text variables in a document and add or update from a list as appropriate.
I've got the read from file / add new / update fuctions working ok, but I'm stuck on checking if the custom text variable currently exists.
// get existing varibales
var currentVars = [];
currentVars = get_variables();
// Throws Error : Error Code# 24: currentVars.includes is not a function
if (currentVars.includes("MyVariable")) {
// run update function
$.writeln('MyVariable exists');
}
else {
// run add new function
$.writeln('MyVariable does not exist');
}
function get_variables () {
var arr = [];
var v = app.activeDocument.textVariables;
for (var i = 0; i < v.length; i++) {
if (v[i].variableType == VariableTypes.customTextType) {
arr.push (v[i].name);
}
}
return arr;
};
The debugger is showing the currentVars as an array and I can see all the custom text variable names are indexed items eg. 0: MyVariable
Any idaeas where I'm going wrong with this?
Array.includes is not part of Extendscript. You need to roll your own includes/contains function. Something like:
Array.prototype.includes = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) { return true; }
}
return false;
}
An example of the third approach of using join method and a regex is mentioned below
var a = ["hello1", "sample", "hello", "array"]
var searchStr = "hello"
var r = new RegExp("(,|^)" + searchStr + "(,|$)")
alert(r.test(a.join()))
P.S.:- Code edited, the regex made more robust
-Manan
Copy link to clipboard
Copied
Array.includes is not part of Extendscript. You need to roll your own includes/contains function. Something like:
Array.prototype.includes = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) { return true; }
}
return false;
}
Copy link to clipboard
Copied
Hi @emittiv,
As the error suggests array does not have any includes method hence you can't use it. There are multiple ways you can handle it.
-Manan
Copy link to clipboard
Copied
An example of the third approach of using join method and a regex is mentioned below
var a = ["hello1", "sample", "hello", "array"]
var searchStr = "hello"
var r = new RegExp("(,|^)" + searchStr + "(,|$)")
alert(r.test(a.join()))
P.S.:- Code edited, the regex made more robust
-Manan