如何以编程方式保存选择?

How do I save a selection programmatically?

在 Adob​​e Illustrator 中,我可以 select 画板上的东西,然后转到“选择”>“保存选择”并为其命名。存储的 selection 也存储在 .ai 文件中,可以在重新打开文件时 selected。

使用 ExtendScript 也可以吗?

到目前为止,我只找到了一种方法 select app.activeDocument.selectObjectsOnActiveArtboard() 的所有内容,然后 cut/copy selection app.copy()app.cut() 并使用 app.paste() 将其粘贴到另一个文档中。但是我没有在文档中找到保存selection的选项。

基本上您可以运行脚本中的几乎任何菜单命令。

保存当前选择的脚本如下所示:

app.executeMenuCommand('Selection Hat 10');

但是会有对话框window为这个选择输入一个名称。

我不确定是否可以避免对话框 window 并通过脚本分配名称。

作为解决方法,您可以使用如下脚本将 selected 页面项目的 ID 保存在文本文件中(与文档在同一文件夹中):

// put IDs of all selected items into the array
var sel = app.selection;
var ids = [];
while (sel.length) ids.push(sel.shift().uuid);

// save the array in the file 'selection.txt'
var file = File(app.activeDocument.path + '/selection.txt');
file.open('w');
file.write(ids.toSource());
file.close();

然后您可以使用第二个脚本从文件和 select 所有页面项目加载 ID,如下所示:

// deselect all just in case
app.selection = null;

// load IDs from the file 'selection.txt'
var doc = app.activeDocument;
var file = File(doc.path + '/selection.txt');
var ids = $.evalFile(file);

// loop through all page items and select items that have the ID from the file
var pageItems = doc.pageItems;
for (var j = 0; j < pageItems.length; j++) {
    for (var i = 0; i < ids.length; i++) {
        if (pageItems[j].uuid == ids[i]) {
            pageItems[j].selected = true;
            ids.splice(i,1); // remove founded id from the array
        }
    }
}