脚本教程:交换对象

Affinity 的第一个脚本。选择两个对象,运行脚本,然后对象交换位置。它很短,适合一次性阅读完毕,但它涉及几乎每个脚本都会执行的四项操作:查找文档、阅读选择、构建命令并执行命令。

脚本功能当前处于 Beta 版。Affinity 的某些部分仍不支持脚本,在未来的更新中将添加其他功能。

整个脚本

JavaScript

'use strict';

// Swaps the positions of two selected objects, keeping their centers.

const { CompoundCommandBuilder, DocumentCommand } = require('/commands.js');
const { Document } = require('/document.js');
const { Transform } = require('/geometry.js');
const { Selection } = require('/selections.js');

function main() {
  const doc = Document.current;
  if (!doc) {
    alert("This script requires an open document");
    return;
  }
  const nodes = doc.selection.nodes.toArray();
  if (nodes.length != 2) {
    alert("Please select exactly two objects");
    return;
  }
  const [a, b] = nodes.map(n => ({ node: n, c: n.getSpreadBaseBox().centre }));
  const builder = CompoundCommandBuilder.create()
    .addCommand(
      DocumentCommand.createTransform(Selection.create(doc, a.node),
      Transform.createTranslate(b.c.x - a.c.x, b.c.y - a.c.y))
    )
    .addCommand(
      DocumentCommand.createTransform(Selection.create(doc, b.node),
      Transform.createTranslate(a.c.x - b.c.x, a.c.y - b.c.y))
    );
  doc.executeCommand(builder.createCommand());
}

main();

分步

1. 导入所需内容

JavaScript

const { CompoundCommandBuilder, DocumentCommand } = require('/commands.js');
const { Document } = require('/document.js');
const { Transform } = require('/geometry.js');
const { Selection } = require('/selections.js');

Affinity 的脚本库被分为模块,您仅拉入您使用的部分。以 / 开头的路径会解析为内置库,因此这些行可以适用于任何脚本,无论其位于何处。

  • 文档是访问打开的文档的方式。
  • 选取项会环绕一组对象,以便命令知道该操作什么。
  • 变换构建移动。
  • DocumentCommand和CompoundCommandBuilder是进行更改的方式。下文将提供更多信息。

2.获取当前文档

JavaScript

const doc = Document.current;
if (!doc) {
  alert("This script requires an open document");
  return;
}

Document.currentnull。首先选中它意味着脚本失败并显示一条简单消息,而不是异常。警报可在任何位置使用,并显示一个简单的消息框。

3. 阅读选择

JavaScript

const nodes = doc.selection.nodes.toArray();
if (nodes.length != 2) {
  alert("Please select exactly two objects");
  return;
}

页面上的所有内容都是节点:形状、曲线、文本、组、图像。doc.selection.nodes以集合的形式提供所选对象, toArray()将其转换为纯 JavaScript 数组,以便您可以使用length 、 map等。交换仅对两个对象有意义,因此其他操作到此为止。

4. 查找每个对象的中心

JavaScript

const [a, b] = nodes.map(n => ({ node: n, c: n.getSpreadBaseBox().centre }));

getSpreadBaseBox()在跨页坐标(页面的共享坐标空间)中返回对象的边界矩形,而不论对象如何旋转或嵌套。矩形有一个方便的中心点。我们将每个节点保留在其中心旁边,以便下一步可以读取两者。

5.将移动描述为命令

JavaScript

const builder = CompoundCommandBuilder.create()
  .addCommand(
    DocumentCommand.createTransform(Selection.create(doc, a.node),
    Transform.createTranslate(b.c.x - a.c.x, b.c.y - a.c.y))
  )
  .addCommand(
    DocumentCommand.createTransform(Selection.create(doc, b.node),
    Transform.createTranslate(a.c.x - b.c.x, a.c.y - b.c.y))
  );

这是 Affinity 脚本的核心。脚本不会直接访问对象。它们构建描述更改的命令,然后将它们交给文档执行。因此,每个脚本都可撤销和预览。

每一行执行三项操作:

  1. Selection.create(doc, a.node)环绕一个对象,以便命令知道其目标。
  2. Transform.createTranslate(dx, dy)通过两个中心之间的差异构建移动。对象 A 移动“B 减 A”,对象 B 移动“A 减 B”。
  3. DocumentCommand.createTransform(selection, transform)将这些转换为命令。

这两个命令放入CompoundCommandBuilder中,它将它们捆绑到一个命令中。没有它,交换将是“历史记录”面板中的两个单独的步骤,并需要两次撤销。

6. 执行

JavaScript

doc.executeCommand(builder.createCommand());

createCommand()完成复合,并executeCommand运行它。对象移动,历史记录面板增加一个条目,使用⌘Z (Mac)/ Ctrl + Z (Windows) 可以将其恢复。

7. 运行脚本

JavaScript

main();

上面的所有内容仅定义了main ;最后一行调用它。将工作包装在函数中而不是将其编写在顶层,可以使早期退出变得简单,因为在没有文档或选择了错误数量的对象时,从 主 返回 会干净利落地结束脚本。

尝试使用脚本

  1. 绘制相距一定距离的两个形状,最好大小不同,以便交换很明显。
  2. 选择两者并运行脚本。每个形状的中心都会放置其他形状的中心。
  3. 按一次⌘Z (Mac)/ Ctrl + Z (Windows)。借助复合命令,两个对象只需一个步骤即可返回其原始位置。

扩展脚本的想法

  • 交换两个以上。围绕 N 个对象的环旋转位置:每个对象移动到下一个对象的中心。
  • 交换拐角而不是中心。替换.centre使用.topLeft对齐左上角而不是中心。
  • 也交换大小。使用Transform.createScale将每个对象按其他对象的宽度和高度比缩放,与翻译结合使用。

另请参阅:

你如何评价此文章为你提供的帮助?