我正在使用ExtentdScript处理Indesign文档链接的元数据信息。
我想使用将该对象转换为字符串,JSON.stringify
但是当我使用它时,出现错误提示:
can't execute script in target engine
。
如果我linkObjStr = JSON.stringify(linksInfObj);
从下面的代码中删除,则一切正常。
JSON.stringify
在ExtendScript中等效于什么,或者还有其他可能来显示linksInfObj
其适当的内容[object object]
?
for (var i = 0, len = doc.links.length; i < len; i++) {
var linkFilepath = File(doc.links[i].filePath).fsName;
var linkFileName = doc.links[i].name;
var xmpFile = new XMPFile(linkFilepath, XMPConst.FILE_INDESIGN, XMPConst.OPEN_FOR_READ);
var allXMP = xmpFile.getXMP();
// Retrieve values from external links XMP.
var documentID = allXMP.getProperty(XMPConst.NS_XMP_MM, 'DocumentID', XMPConst.STRING);
var instanceID = allXMP.getProperty(XMPConst.NS_XMP_MM, 'InstanceID', XMPConst.STRING);
linksInfObj[linkFileName] = {'docId': documentID, 'insId': instanceID};
linkObjStr = JSON.stringify(linksInfObj);
alert('Object' + linksInfObj, true); // I am getting [Object Object] here
alert('String' + linkObjStr, true);
}
ExtendScript不包含JSON对象及其关联的解析方法,即JSON.parse()
和JSON.stringify()
。它也没有提供任何其他内置功能来解析JSON。
考虑利用polyfill提供JSON功能,例如Douglas Crockford创建的JSON-js。
您需要做的是:
从Github存储库下载名为json2.js的JavaScript文件,并将其保存在与文件相同的位置/文件夹中.jsx
。
然后,在当前.jsx
文件的顶部,通过添加以下代码行,您将需要#include
该json2.js
文件:
#include "json2.js";
这类似于您如何利用该import
语句在现代JavaScript(ES6)中包含模块。
一个路径名来的位置json2.js
,如果你决定将文件保存在不同的位置/文件夹不是您可以提供.jsx
文件。
通过包含json2.js
在.jsx
文件中,您现在将具有有效的JSON方法;JSON.parse()
和JSON.stringify()
。
以下ExtendScript(.jsx
)是一个工作示例,该示例生成JSON以指示与当前InDesign文档(.indd
)相关联的所有链接。
example.jsx
#include "json2.js";
$.level=0;
var doc = app.activeDocument;
/**
* Loads the AdobeXMPScript library.
* @returns {Boolean} True if the library loaded successfully, otherwise false.
*/
function loadXMPLibrary() {
if (!ExternalObject.AdobeXMPScript) {
try {
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
} catch (e) {
alert('Failed loading AdobeXMPScript library\n' + e.message, 'Error', true);
return false;
}
}
return true;
}
/**
* Obtains the values f XMP properties for `DocumentID` and `instanceID` in
* each linked file associated with an InDesign document (.indd). A returns the
* information formatted as JSON,
* @param {Object} doc - A reference to the .indd to check.
* @returns {String} - The information formatted as JSON.
*/
function getLinksInfoAsJson(doc) {
var linksInfObj = {};
linksInfObj['indd-name'] = doc.name;
linksInfObj.location = doc.filePath.fsName;
linksInfObj.links = [];
for (var i = 0, len = doc.links.length; i < len; i++) {
var linkFilepath = File(doc.links[i].filePath).fsName;
var linkFileName = doc.links[i].name;
var xmpFile = new XMPFile(linkFilepath, XMPConst.FILE_INDESIGN, XMPConst.OPEN_FOR_READ);
var allXMP = xmpFile.getXMP();
// Retrieve values from external links XMP.
var documentID = allXMP.getProperty(XMPConst.NS_XMP_MM, 'DocumentID', XMPConst.STRING);
var instanceID = allXMP.getProperty(XMPConst.NS_XMP_MM, 'InstanceID', XMPConst.STRING);
// Ensure we produce valid JSON...
// - When `instanceID` or `documentID` values equal `undefined` change to `null`.
// - When `instanceID` or `documentID` exist ensure it's a String.
instanceID = instanceID ? String(instanceID) : null;
documentID = documentID ? String(documentID) : null;
linksInfObj.links.push({
'name': linkFileName,
'path': linkFilepath,
'docId': documentID,
'insId': instanceID
});
}
return JSON.stringify(linksInfObj, null, 2);
}
if (loadXMPLibrary()) {
var linksJson = getLinksInfoAsJson(doc);
$.writeln(linksJson);
}
输出:
运行上面的脚本会将JSON格式的类似以下示例的日志记录到控制台:
{
"indd-name": "foobar.indd",
"location": "/path/to/the/document",
"links":[
{
"name": "one.psd",
"path": "/path/to/the/document/links/one.psd",
"docId": "5E3AE91C0E2AD0A57A0318E078A125D6",
"insId": "xmp.iid:0480117407206811AFFD9EEDCD311C32"
},
{
"name": "two.jpg",
"path": "/path/to/the/document/links/two.jpg",
"docId": "EDC4CCF902ED087F654B6AB54C57A833",
"insId": "xmp.iid:FE7F117407206811A61394AAF02B0DD6"
},
{
"name": "three.png",
"path": "/path/to/the/document/links/three.png",
"docId": null,
"insId": null
}
]
}
您会注意到,上述JSON输出的结构与给定示例中的结构不同。主要区别在于您使用链接文件名作为属性/键名,例如以下示例:
有问题的JSON结构的示例
{
"one.psd": {
"docId": "5E3AE91C0E2AD0A57A0318E078A125D6",
"insId": "xmp.iid:0480117407206811AFFD9EEDCD311C32"
},
"two.jpg": {
"docId": "EDC4CCF902ED087F654B6AB54C57A833",
"insId": "xmp.iid:FE7F117407206811A61394AAF02B0DD6"
}
...
}
像此示例那样生成JSON并不理想,因为如果要拥有两个同名链接,则只会报告其中一个。一个对象内不能有两个具有相同名称的属性/键。
编辑:
作为对OP的评论的回应:
嗨,RobC,除了使用之外
#include 'json2.js'
,还有其他方法可以将外部js文件包含在JSX文件中吗?
有以下两种替代方法:
您可以利用$.evalFile()
。例如,替换#include "json2.js";
为以下两行:
var json2 = File($.fileName).path + "/" + "json2.js";
$.evalFile(json2);
注意:此示例假定json2.js
与您的计算机位于同一文件夹中。.jsx
另外,如果您想完全避免其他json2.js
文件的存在。您可以在文件顶部添加IIFE(立即调用函数表达式).jsx
。然后将json2.js文件的内容复制并粘贴到其中。例如:
(function () {
// <-- Paste the content of `json2.js` here.
})();
注:如果代码大小是那么关心考虑涅槃的内容json2.js
粘贴到IIFE之前。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句