使用方法将对象转换为Javascript中的字符串(对于Photoshop)

时间:2013-12-03 15:09:18

标签: javascript object serialization photoshop

我有一个我在Photoshop脚本中使用的全局对象。它有如下属性和方法:

var Obj = {};
Obj.str = "this is a string";
Obj.swissKnife = new SwissKnife(); // custom object with it's own methods
Obj.swissKnife.aMethod = function() { return "this is a method"; };

我需要将整个对象转换为字符串。

在Photoshop中,为了显示调色板(非模态)对话框,允许用户继续与应用程序交互(我需要它们在继续之前使用选框工具选择区域),您必须发回消息通过BridgeTalk到Photoshop。这样做时,您要发送的代码必须是字符串,如下所示:

var title = "Select image";
var message = "Select image to protect with marquee tool, then click continue.";
var bt = new BridgeTalk();
bt.target = "photoshop";
bt.body = "var w = new Window('palette', '" + title + "', [0, 0, 400, 100]); \
                w.add('statictext', [5,5,390,50], '" + message + "'); \
                okButton = w.add('button', [5,40,205,90], 'GO!'); \
                w.center(); \
                okButton.onClick = function() { \
                    w.close(); \
                    obj = " + obj + "; \
                    #include \"/c/script.jsx\"; \
                }; \
                w.show();";
bt.send();

我需要将对象obj(包含其所有属性和方法)提供给我加载的下一个脚本/c/script.jsx。所以我需要一种方法来将整个obj对象评估为一个字符串,以便写入obj =声明。

1 个答案:

答案 0 :(得分:0)

在这里查看xtools库的Stdlib.js中的objectToXML方法:http://ps-scripts.sourceforge.net/xtools.html