如何在cocoascript(草图)中正确组合“点符号”和“括号符号”?

时间:2015-08-21 07:31:02

标签: cocoascript sketchapp

在草图documentation中,声明点和括号符号可以相互混合。它甚至可以example

[[context.document currentPage] deselectAllLayers];

不幸的是,如果通过“运行自定义脚本”命令执行此代码在草图中不起作用并产生错误:

SyntaxError: Unexpected identifier 'currentPage'. Expected either a closing ']' or a ',' following an array element..
Plugin “untitled script”, line 2.
»   [context.document currentPage]; «Error in command untitled script Script at path (null) does not contain a handler function named: onRun
Script executed in 0.023666s

可以通过添加其他()

来避免这种情况
[[(context.document) currentPage] deselectAllLayers];

为什么会这样?是否有任何文档可用于如何混合大括号和点符号?这是一些错误还是预期的行为?

1 个答案:

答案 0 :(得分:2)

在我看来这是一个错误,但在Sketch文档中。除了你展示的这个案例,我找不到任何其他的例子,其中点和括号符号在同一个语句中一起使用,没有括号。

例如,有关选择的文档页面告诉您使用以下代码取消选择所有内容:

#countdown-container {
    width: 500px;
}

点击此链接,在清除选择标题下查看:http://bohemiancoding.com/sketch/support/developer/02-common-tasks/01.html

即使他们的示例插件也不会混合使用这两种符号,您可以在此处看到:https://github.com/BohemianCoding/ExampleSketchPlugins/blob/master/Hello%20World/Hello%20World.sketchplugin/Contents/Sketch/script.cocoascript

在该示例中,var doc = context.document [[doc currentPage] deselectAllLayers] 在被用于大括号之前也被分配给新变量。