Copy link to clipboard
Copied
Hi guys,
i'm an Applescript guy trying to translate an AS script in JS.
So far, it's working with one pb though: i want to use the array.filter method in my script but it doesn't work.
I've replaced it via a simple loop and it work, but as it seems it could be done via the use of a polyfill, i tried that too, without success.
I guess i have done something wrong somewhere and i hoped someone here could help me find it!
What i've done so far:
- copy the polyfill for that method from this page on MDN,
- creating a new file on ExtendScript*, paste the code, save the file ("polyfills.jsx"**) in the same folder as my script.
- added at the beginning of my script "#include 'polyfills.jsx';"
* I don't know if it's relevant, but the selected engine when saving the file was "Adobe InDesign CC2017 (12.064)", the same used by my script.
** I couldn't use the .jsxinc extension as when i do that, it create a "polyfills.jsxinc.jsx" file.
When i launch my script in ExtendScript Toolkit, it throws an error on the line where i use the filter method : 'xxx.filter is not a function'.
So the polyfill doesn't seem to work.
The code where i use the filter method:
var my_textFrames = doc.textFrames;
var my_filtered_textFrames = my_textFrames.filter(function (element) {
return (element.label.indexOf('tableau') !== -1);
});
Thank for your help!
config:
OS X 10.11.6
InDesign CC 2017 12.0.0.81
ExtendScript Toolkit 4.0.0.1
doc.textFrames is not an ARRAY but a collection.
you should convert it to an array:
var my_textFrames = doc.textFrames.everyItem().getElements();
Copy link to clipboard
Copied
doc.textFrames is not an ARRAY but a collection.
you should convert it to an array:
var my_textFrames = doc.textFrames.everyItem().getElements();
Copy link to clipboard
Copied
Ok i'll try that, thanks for the quick reply!
Copy link to clipboard
Copied
Hi Vincent,
you have to be very careful using the right string for filtering with prototyped method filter() as presented at MDN.
Example
I know, it's a bit dump, but I like to show a point here.
Category 1 | Have some text frames labeled:
"SelectThis"
Category 2 | Others should be labeled:
"DoNotSelectThis"
Example line in your filter function:
return (element.label.indexOf("SelectThis") !== -1);
would return all two categories of labeled text frames.
So I would suggest the more lame approach with a different tool set to get the labeled text frames:
var doc = app.documents[0];
var textFramesArray = doc.textFrames.everyItem().getElements();
var textFramesArrayLength = textFramesArray.length;
var n = 0;
var regExp = RegExp("^SelectThis$"); // A softened version would be: RegExp("^SelectThis$", "i")
var resultArrayOfTextFrames = [];
for(n=0;n<textFramesArrayLength;n++)
{
var currentItem = textFramesArray
;
if(currentItem.label.match(regExp))
{
resultArrayOfTextFrames[resultArrayOfTextFrames.length++] =
textFramesArray
; };
};
Or this alternative in the for loop using method test() on a RegExp.
// ALTERNATIVE for match():
if(regExp.test(currentItem.label))
{
resultArrayOfTextFrames[resultArrayOfTextFrames.length++] =
textFramesArray
; };
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe,
thanks for the reply.
In the context of my script, i think my test is ok as there is only one text frame in the document that have a label which contain that string, so there is no ambiguity in this case.
But still, i'll look carefully at your alternative methods: my AS>JS journey is only beginning and i need every advise and code exemples i could get 😉
Regards,
Vincent
Copy link to clipboard
Copied