InDesign - Javascript - 复制对象、粘贴到位、将新对象与原始选择分组
InDesign - Javascript - Copy Object, Paste in Place, Group New Object with Original Selection
为清晰起见进行了编辑
我已经为 InDesign 创建了一个脚本来改进我的工作流程。
我select一张图片,然后运行脚本,执行如下:
- 复制 selected 图像(用于步骤 #3)。
- 对 selected 图像应用“倍增”效果。
- 就地粘贴原始图像(没有乘法效果)。
- 查找特定的 photoshop 路径名称('X'、'FCP' 或 'x')。
- 应用它找到的第一个路径并警告已 selected 或警告“不存在有效路径”如果 none 可用。
- 脚本以粘贴的图像结束 select编辑。
一切正常;但是,我想 select 两个图像(具有剪切路径的图像和具有乘法效果的图像)并将它们分组。我正在研究如何 select 两张图片。
这是原始代码(我打算让 Yuri 对复制和粘贴命令进行编辑):
app.menuActions.item("$ID/Copy").invoke();
var myProperties = {
blendMode : BlendMode.MULTIPLY,
opacity : 100
};
for (i=0; i<app.selection.length; i++)
{
try {
app.selection[i].images[0].transparencySettings.blendingSettings.properties = myProperties;
}catch(e){}
}
app.menuActions.item("$ID/Paste in Place").invoke();
for (j=0; j<app.selection.length; j++)
{
try {
var myPathName = app.selection[0].images[0].clippingPath.photoshopPathNames;
var myPath = app.selection[0].images[0].clippingPath;
function working() {
for (var i = 0; i < myPathName.length; i++) {
if (myPathName[i] === "X") {
myPath.appliedPathName = myPathName[i];
alert ("X path activated");
return i;
} else if (myPathName[i] === "FCP") {
myPath.appliedPathName = myPathName[i];
alert ("FCP path activated");
return i;
} else if (myPathName[i] === "x") {
myPath.appliedPathName = myPathName[i];
alert ("x path activated");
return i;
}
}
alert ("No valid path exists");
}
working();
} catch (e) {}
}
Apologies if it's not the cleanest code. I started learning javascript in my free time and this is one of the first scripts I wrote.
还是不确定我是否理解正确。如果您需要将某些项目添加到当前选择中,可以通过以下方式完成:
var item1 = app.selection[0]; // some selected item
app.paste(); // paste item which is selected now
item1.select(SelectionOptions.ADD_TO) // add the item1 to current selection
app.menuActions.itemByName("$ID/Group").invoke(); // group selected items
编辑以将 app.selected
更改为 app.selection
因为 selected
返回错误
在 Yuri 的指导下,我解决了我的问题。我不得不稍微重新排序这个过程,以说明我是如何添加到选择中的(也许我没有,但它对我来说很有意义并且它有效!)。这是任何感兴趣的人的最终代码。
//look for clipping path 'X', 'FCP', or 'x' and apply it
for (j=0; j<app.selection.length; j++)
{
try {
var myPathName = app.selection[0].images[0].clippingPath.photoshopPathNames;
var myPath = app.selection[0].images[0].clippingPath;
function working() {
for (var i = 0; i < myPathName.length; i++) {
if (myPathName[i] === "X") {
myPath.appliedPathName = myPathName[i];
alert ("X path activated");
return i;
} else if (myPathName[i] === "FCP") {
myPath.appliedPathName = myPathName[i];
alert ("FCP path activated");
return i;
} else if (myPathName[i] === "x") {
myPath.appliedPathName = myPathName[i];
alert ("x path activated");
return i;
}
}
alert ("No valid path exists");
}
working();
} catch (e) {}
}
//copy image with clipping path
app.copy();
//remove clipping path
app.selection[0].images[0].clippingPath.clippingType = ClippingPathType.NONE;
//apply Multiply blend mode
var myProperties = {
blendMode : BlendMode.MULTIPLY,
opacity : 100
};
for (i=0; i<app.selection.length; i++)
{
try {
app.selection[i].images[0].transparencySettings.blendingSettings.properties = myProperties;
}catch(e){}
}
//paste in place and add to current selection
var clippedImage = app.selection[0];
app.pasteInPlace();
clippedImage.select(SelectionOptions.ADD_TO)
//group selected images
app.menuActions.itemByName("$ID/Group").invoke();
发生了什么,它是如何工作的? 用户选择图像并运行脚本
- 它搜索匹配特定字符串的剪切路径。
- 用户会收到应用了哪个路径或未找到匹配项的警告。
- 图像已复制(用于第 6 步)。
- 路径已删除。
- 已应用多重混合模式。
- 步骤 3 中复制的图像已粘贴到位(带剪切路径)。
- 粘贴的图像选择被添加到当前选择(具有多重混合模式的图像)。
- 所选内容已分组。