如何在 Indesign 脚本中使用 Array.reduce() 等高阶函数?
How can I use higher order functions like Array.reduce() in an Indesign script?
我已经开始了一个项目,我需要使用 Adobe Indesign 和 ExtendScript 以编程方式从一系列 INDD 文件中提取一些数据。在这些程序中用于编写脚本的 Javascript 版本不支持我习惯使用的任何高阶函数(Array.reduce()
、Array.forEach()
、Array.map()
、等等...).
有没有办法将此功能添加到 ExtendScript 中?我觉得我在一个有四英尺高的天花板的房间里走来走去。
使用 Polyfill
ExtendScript 似乎支持纯 Javascript 对象的原型(但 not Indesign DOM objects), so it is possible to use a polyfill to add missing functionality. Polyfill code can be found on MDN on the page for the method in question under "Polyfill". Here's an example: MDN Array.prototype.reduce() Polyfill。有许多方法的 polyfill,包括 Array.map()
、Array.indexOf()
、Array.filter()
, 和 Array.forEach()
.
要实现代码,只需在与脚本相同的文件夹中创建一个适当命名的文件(即 polyfill.js
或 reduce.js
)。将 MDN 中的 polyfill 代码复制到您刚刚创建的文件中,如下所示:
// Production steps of ECMA-262, Edition 5, 15.4.4.21
// Reference: http://es5.github.io/#x15.4.4.21
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(callback /*, initialValue*/) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.reduce called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var t = Object(this), len = t.length >>> 0, k = 0, value;
if (arguments.length == 2) {
value = arguments[1];
} else {
while (k < len && !(k in t)) {
k++;
}
if (k >= len) {
throw new TypeError('Reduce of empty array with no initial value');
}
value = t[k++];
}
for (; k < len; k++) {
if (k in t) {
value = callback(value, t[k], k, t);
}
}
return value;
};
}
然后在脚本开头添加以下行,适当地替换文件名:
#include 'polyfill.js';
行尾的分号未包含在 Adobe 文档中,但我发现有时如果不使用 ExtendScript 会抛出错误,尤其是当您多次使用 #include
时。
我改用underscore.js。
Underscore.js http://underscorejs.org/
#include '/path-to/underscore.js'
var _ = this._;
在脚本开头添加此代码段。
谢谢
毫克。
我已经开始了一个项目,我需要使用 Adobe Indesign 和 ExtendScript 以编程方式从一系列 INDD 文件中提取一些数据。在这些程序中用于编写脚本的 Javascript 版本不支持我习惯使用的任何高阶函数(Array.reduce()
、Array.forEach()
、Array.map()
、等等...).
有没有办法将此功能添加到 ExtendScript 中?我觉得我在一个有四英尺高的天花板的房间里走来走去。
使用 Polyfill
ExtendScript 似乎支持纯 Javascript 对象的原型(但 not Indesign DOM objects), so it is possible to use a polyfill to add missing functionality. Polyfill code can be found on MDN on the page for the method in question under "Polyfill". Here's an example: MDN Array.prototype.reduce() Polyfill。有许多方法的 polyfill,包括 Array.map()
、Array.indexOf()
、Array.filter()
, 和 Array.forEach()
.
要实现代码,只需在与脚本相同的文件夹中创建一个适当命名的文件(即 polyfill.js
或 reduce.js
)。将 MDN 中的 polyfill 代码复制到您刚刚创建的文件中,如下所示:
// Production steps of ECMA-262, Edition 5, 15.4.4.21
// Reference: http://es5.github.io/#x15.4.4.21
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(callback /*, initialValue*/) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.reduce called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var t = Object(this), len = t.length >>> 0, k = 0, value;
if (arguments.length == 2) {
value = arguments[1];
} else {
while (k < len && !(k in t)) {
k++;
}
if (k >= len) {
throw new TypeError('Reduce of empty array with no initial value');
}
value = t[k++];
}
for (; k < len; k++) {
if (k in t) {
value = callback(value, t[k], k, t);
}
}
return value;
};
}
然后在脚本开头添加以下行,适当地替换文件名:
#include 'polyfill.js';
行尾的分号未包含在 Adobe 文档中,但我发现有时如果不使用 ExtendScript 会抛出错误,尤其是当您多次使用 #include
时。
我改用underscore.js。
Underscore.js http://underscorejs.org/
#include '/path-to/underscore.js'
var _ = this._;
在脚本开头添加此代码段。
谢谢
毫克。