FrameRef 内置 Luau 脚本系统,用于自动化画板工作。你可以用它进行一次性清理、批量编辑、带有独立菜单操作的可复用插件,或通过本地连接驱动 FrameRef 的外部工具。
脚本默认启用。主要入口是 Scripts 菜单,其中包含:
| 操作 | 说明 |
|---|---|
| Script Console | 打开多标签页脚本编辑器。 |
| Run Script File… | 运行 .luau 或 .lua 文件,而无需打开控制台。 |
| Reload Scripts | 从磁盘重新加载所有脚本插件。 |
| Open Scripts Folder | 打开用户插件所在的文件夹。 |
| Export Scripting Stubs… | 写出 frameref.d.luau 以支持编辑器补全。 |
| MCP Integration… | 配置本地 MCP 服务器。 |
使用 Scripts → Script Console 或 Ctrl + Alt + J 打开控制台。它是一个常规的 FrameRef 窗口,因此你可以移动、调整大小、最大化它,或让它一直停在画板旁边。
控制台提供:
- 多个编辑器标签页,可以同时打开多个脚本。
- Luau 语法高亮和补全,包括类型标注。
- 模板,用于常见任务。
- 最近脚本,可快速重新打开之前用过的文件。
- 输出面板,用于显示
print、错误、返回值和计时。 - 快速帮助面板,提供精简的 API 参考。
- 空白字符显示、编辑器缩放,以及启动之间的会话恢复。
编辑器快捷键
Section titled “编辑器快捷键”| 操作 | 默认快捷键 |
|---|---|
| New Script | Ctrl + N |
| Open Script | Ctrl + O |
| Save Script | Ctrl + S |
| Save Script As | Ctrl + Shift + S |
| Close Script Tab | Ctrl + W |
| Run Script | F5 |
| Run Selection | Shift + F5 |
| Clear Output | Ctrl + L |
| Quick Help | Ctrl + Shift + H |
| Toggle Output Panel | Ctrl + Shift + O |
这些快捷键可以在 Preferences → Script Editor 中更改。
脚本针对活动画板运行,并使用全局 frameref 对象。脚本可以返回任何可 JSON 序列化的值。print(...) 消息和返回值会显示在输出面板中。
统计活动画板上的图像数量:
local count = 0for _, item in frameref.items.all() do if item.type == "image" then count = count + 1 endend
return { images = count }为每个选中元素添加标签:
local items = frameref.selection.items()for _, item in items do item:addTags({ "reviewed" })end
return { updated = #items }按类型、标签、标签文本或 UID 查找元素:
local references = frameref.items.find({ type = "image", tags = { "character" }, limit = 50,})
for _, item in references do item.rating = 5end每次控制台运行都会作为一个可撤销步骤应用到画板。即使脚本编辑了许多元素,仍然只需一次 Ctrl + Z 即可撤销。插件可以通过 frameref.undo.begin("Label") 和 frameref.undo.commit() 显式地将自己的工作分组。
frameref.input.form 会显示一个模态表单并返回一个 Future:
local values = frameref.input.form({ title = "Create note", text = "Where should the note go?", fields = { { key = "x", label = "X", type = "int", default = 0 }, { key = "y", label = "Y", type = "int", default = 0 }, },}):await()
if not values then returnend
frameref.items.createNote({ x = values.x, y = values.y })API 概览
Section titled “API 概览”frameref 表按命名空间组织。有关每个函数、参数、返回值和对象属性,请参阅脚本 API 参考。
| 命名空间 | 功能 |
|---|---|
frameref.boards |
列出打开的画板、获取活动画板,以及激活另一个画板。 |
frameref.selection |
读取和更改选择。 |
frameref.items |
查找、创建、导入、复制、删除、编组、排列、对齐和分布元素。 |
frameref.actions |
列出已注册的操作,运行它们,并更改它们的快捷键。 |
frameref.undo |
开始、提交或取消撤销组,以及驱动撤销/重做。 |
frameref.events |
订阅画板和选择事件。仅限插件。 |
frameref.async |
延迟工作或在延迟后运行回调。仅限插件。 |
frameref.storage |
为每个插件存储 JSON 数据。仅限插件。 |
frameref.settings |
读取和写入白名单中的首选项。 |
frameref.project |
查看并保存当前项目。 |
frameref.clipboard |
复制、剪切和粘贴元素。 |
frameref.image |
将元素捕获为图像。 |
frameref.input |
显示表单并等待结果。 |
frameref.search |
在活动画板中搜索元素和操作。 |
frameref.log、frameref.notify |
写入输出面板或显示通知。 |
API 返回的元素是实时句柄,而不是副本。可以直接读写属性,或对需要撤销命令的操作使用方法:
local item = frameref.items.get("<uid>")if item == nil then error("Item not found")end
item.label = "Hero pose"item.rating = 4item:addTags({ "approved" })item:moveBy(20, 0)每个元素都暴露基础属性 uid、type、label、caption、tags、rating、x、y、rotation、scale、width、height、z、opacity、locked、visible 和 sourceURL。某些类型还会暴露自己的属性,例如视频和音频元素上的 muted,或形状上的 currentShape;按类型列出的完整列表请参阅元素属性。常用方法包括 addTags、removeTags、delete、duplicate、select、frame、moveBy、moveTo、rotateBy、rotateAround、scaleBy、scaleAround、capture、awaitReady、isValid 和 snapshot。
frameref.boards.list() 返回每个打开的画板,因此脚本可以跨标签页工作:
for _, board in frameref.boards.list() do local snapshot = board:snapshot() print(snapshot.title, #snapshot.items)end
frameref.boards.active():save()画板属性为只读:id、title、path、readOnly 和 dirty。方法包括 items、selection、snapshot、save、saveAs、undo、redo、frame 和 activate。
for _, result in frameref.search("cover") do print(result.kind, result.title, result.description) if result.kind == "navigation" then result:activate() endend
frameref.actions.run("action_select_all")插件将脚本转换为带有名称、快捷键和可选事件订阅的可复用操作。插件是脚本文件夹中的一个文件夹:
MyPlugin/ plugin.json main.luau helper.luauplugin.json 描述插件:
{ "id": "com.example.myplugin", "name": "My Plugin", "version": "1.0.0", "api": 1, "main": "main.luau", "permissions": ["scene.read", "scene.write"], "actions": [ { "id": "tag-selection", "title": "Tag Selection", "tooltip": "Add the review tag to every selected item", "shortcut": "Ctrl+Alt+T" } ]}id必须唯一。api是插件 API 版本。请使用1。main指向入口文件。permissions控制插件可以执行的操作。scene.write允许更改画板;*授予所有权限。没有scene.write的插件以只读方式运行。actions会成为插件的 Scripts 菜单项和快捷键操作。
main.luau 返回一个操作函数表:
local helper = require("helper")
return { actions = { ["tag-selection"] = function(ctx) local items = frameref.selection.items() for _, item in items do item:addTags({ "review" }) end
frameref.notify(string.format("Tagged %d item(s)", #items)) end, },}ctx 参数包含 pluginId、actionId、pluginName 和 dataDir。require("helper") 会加载相对于插件文件夹的 Luau 文件。模块会为插件环境缓存。
事件、异步工作与存储
Section titled “事件、异步工作与存储”插件可以订阅事件,并在多次运行之间保留数据:
local subscription = frameref.events.on("selection.changed", function(boardId) frameref.log("Selection changed on " .. boardId)end)
frameref.storage.set("lastRun", os.date())frameref.storage.save()
frameref.async.delay(2.0, function() frameref.events.off(subscription)end)可用的事件包括 board.activated、board.opened、board.closed、board.saved、selection.changed、scene.changed、undo.changed 和 project.changed。
Future 和回调在异步与 Future 中有详细介绍,包括何时使用 :await(),以及何时使用 :andThen() 和 :catch()。
信任与搜索路径
Section titled “信任与搜索路径”新插件首次被发现时会请求权限。提示中会列出所请求的权限,并且 FrameRef 会记住清单的 SHA-256 哈希。如果清单发生变化,插件会再次请求。
插件从以下位置加载:
- 用户脚本文件夹(Scripts → Open Scripts Folder)。
- Preferences → Scripting → Script search paths 中的任何额外文件夹。
之前已批准的插件可以在启动时自动加载。可通过 Load trusted plugins on startup 关闭此行为。
自动化服务器
Section titled “自动化服务器”自动化服务器为编辑器和其他工具提供本地 WebSocket JSON-RPC 端点。它默认禁用。
在 Preferences → Scripting → Enable automation server 中启用它。将端口保留为 0 可随机选择一个回环端口,也可以设置固定端口。
服务器启动时,FrameRef 会写入一个发现文件:
| 平台 | 路径 |
|---|---|
| Windows | %APPDATA%\Polygonflow\FrameRef\automation\server.json |
| macOS | ~/Library/Application Support/Polygonflow/FrameRef/automation/server.json |
该文件包含端口、每个会话的 Bearer 令牌、进程 ID,以及协议和应用版本。连接到 ws://127.0.0.1:<port>,并在 Authorization: Bearer <token> 请求头或 ?token= 查询参数中发送令牌。
| 方法 | 说明 |
|---|---|
api.info |
应用版本、API 版本、活动画板、打开的画板,以及是否允许场景编辑。 |
script.run |
从源字符串或配置的脚本搜索路径中的文件运行 Luau。日志会以 script.log 通知的形式流式传输。 |
script.cancel |
中断当前脚本。 |
stubs.get |
返回生成的 frameref.d.luau 存根。 |
scene.snapshot |
返回活动画板的快照。 |
远程脚本默认只读。启用 Allow remote scripts to edit the board 后,它们才能更改画板。MCP 集成使用相同的设置,因此启用后两者都可以写入。
服务器仅绑定到 127.0.0.1。带有浏览器 Origin 请求头的请求会被拒绝,并且每个连接都需要 Bearer 令牌。
连接到 ws://127.0.0.1:<port>,发送 Bearer 令牌,并使用 JSON-RPC 消息:
{"jsonrpc": "2.0", "id": 1, "method": "api.info"}{ "jsonrpc": "2.0", "id": 2, "method": "script.run", "params": { "source": "return { items = #frameref.items.all() }", "filename": "remote-count" }}请求运行期间,脚本日志会以 script.log 通知的形式到达。
FrameRef 可以生成 Luau 定义文件,用于编辑器补全和类型检查:
- 打开 Scripts → Export Scripting Stubs…
- 选择一个文件夹。FrameRef 会在其中写入
frameref.d.luau。 - 将编辑器的 Luau 语言服务器指向该文件。
存根包含 frameref 命名空间、元素和画板句柄、事件、Future,以及实时的操作 ID 列表。
- 脚本在沙箱中运行。一次性脚本不能使用
require、io、load,也不能任意访问文件和网络。插件可以使用require加载自己文件夹内的模块。 - 自动化服务器和 MCP 服务器仅监听回环地址,并使用每个会话的令牌进行身份验证。
- 远程写入在你启用之前处于禁用状态。
- 插件信任与清单哈希绑定。更改清单需要重新批准。
- 每次运行都有超时限制。默认值为 10 秒;允许的范围是 0.5 到 120 秒。
- 每次运行都是一个可撤销步骤,包括插件操作。
| 问题 | 解决方法 |
|---|---|
| Scripts 菜单操作被禁用 | 检查 Preferences → Scripting → Enable scripting。 |
| 插件未出现 | 确认该文件夹包含可读取的 plugin.json,并且 main 文件存在。然后使用 Scripts → Reload Scripts。 |
| 插件无法更改画板 | 在其清单权限中添加 scene.write,并批准更新后的插件。 |
| 脚本因超时停止 | 缩小工作范围,为 items.find 添加筛选条件,或在首选项中提高超时时间。 |
| 自动化客户端无法连接 | 启用自动化服务器,检查发现文件,并使用当前令牌。 |
