3

我正在尝试编写一个脚本,将图层从一个文档复制到另一个文档。

var srcDocName = 0;
var destDocName = 1;
var layerNameOriginal = "Original";
var layerNameCopyTo = "Destination";

var destDoc = app.documents.item(destDocName);
var layerSrc = app.documents.item(srcDocName).layers.item(layerNameOriginal);

try {  
        layerSrc.duplicate(destDoc, ElementPlacement.INSIDE);
    }
    catch(e) {  
        alert(e)    
    }

显然这在 Photoshop 中有效,但在 InDesign 中无效。多年来,我一直在尝试为 InDesign 脚本找到一些体面的文档。但我能找到的只是 CS 脚本指南,它没有多大用处。

http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/InDesignCS5_ScriptingGuide_JS.pdf

如果有人能指出我对对象模型的良好参考,我将不胜感激。

4

3 回答 3

2

经过一番谷歌搜索,我终于找到了答案:

 var sourceLayer = app.documents[0].layers.itemByName("Layer1");
 var destLayer = app.documents[1].layers[0];

 sourceLayer.pageItems.everyItem().duplicate(destLayer);

我还遇到了jongware,它似乎是直接从 Adob​​e CS 中提取的完整对象引用。

于 2013-05-10T12:07:25.587 回答
0

您可以使用此脚本:https ://redokun.com/blog/indesign-copy-entire-layer-one-file-another

底层实现基本相同,但我们添加了一个 UI,因此无需在每次更改图层名称时都编辑脚本。

编辑:我们被告知上面的解决方案不适用于线程文本框架,所以我重新编写了脚本。新的实现要复杂得多,但它现在支持线程 TF。

于 2016-10-24T16:37:37.347 回答
0

要扩展 Loopo 提供的解决方案,并让您能够将所有图层从一个文档复制到另一个文档...

main();


function main()
{
    var source = GetSourceDocument();
    if(source == -1)
    {
        return;
        }
    var target = GetTargetDocument ();
    if(target == -1)
    {
        return;
        }
    if(target == source)
    {
        return;
        }
    copyLayersOver(source, target);
    }

function GetSourceDocument()
{
    var returnVal = -1;
    var oldPrefs = app.scriptPreferences.userInteractionLevel;
    app.scriptPreferences.userInteractionLevel=UserInteractionLevels.INTERACT_WITH_ALL;

    var dialog = app.dialogs.add({name:"Document to Copy From", canCanel: true, label:"DocumentToCopyFrom"});
    var col1 = dialog.dialogColumns.add();
    var StringList= [];
    for(var i = 0; i<app.documents.length; i++)
    {
        StringList.push("[" + app.documents[i].index + "] " + app.documents[i].name);
        }
    var ddl = col1.dropdowns.add({id:"SourceDocDDL", stringList: StringList});
    if(dialog.show() == true)
    {
        returnVal = ddl.stringList[ddl.selectedIndex].split("]")[0].substr(1);
        }
    else
    {
        returnVal -1;
        }
    dialog.destroy();
    app.scriptPreferences.userInteractionLevel = oldPrefs;
    return returnVal;
    }

function GetTargetDocument()
{
    var returnVal = -1;
    var oldPrefs = app.scriptPreferences.userInteractionLevel;
    app.scriptPreferences.userInteractionLevel=UserInteractionLevels.INTERACT_WITH_ALL;

    var dialog = app.dialogs.add({name:"Document to Copy To", canCanel: true, label:"DocumentToCopyTo"});
    var col1 = dialog.dialogColumns.add();
    var StringList= [];
    for(var i = 0; i<app.documents.length; i++)
    {
        StringList.push("[" + app.documents[i].index + "] " + app.documents[i].name);
        }
    var ddl = col1.dropdowns.add({id:"SourceDocDDL", stringList: StringList});
    if(dialog.show() == true)
    {
        returnVal = ddl.stringList[ddl.selectedIndex].split("]")[0].substr(1);
        }
    else
    {
        returnVal -1;
        }
    dialog.destroy();
    app.scriptPreferences.userInteractionLevel = oldPrefs;
    return returnVal;
    }

function copyLayersOver(source, target)
{
    var sourceDocument = app.documents[source];
    var targetDocument = app.documents[target];
    var sourceLayers = sourceDocument.layers;

    //Match the number of pages
    while(targetDocument.pages.length <  sourceDocument.pages.length)
    {
        targetDocument.pages.add();
        }
    //copy the layers over
    for(var i= 0; i < sourceLayers.length; i++)
    {
        var names = targetDocument.layers.everyItem().name;
        var merge = false;
        for(var y = 0; y < names.length; y++)
        {
            if(names[y] == sourceLayers[i].name)
            {
                merge = true;
                break;
                }
            }
        if(merge)
        {
            var targetLayer = targetDocument.layers.add();
            targetLayer.name = "temp";
            sourceLayers[i].pageItems.everyItem().duplicate(targetLayer);
            targetDocument.layers.itemByName(sourceLayers[i].name).merge(targetLayer);
            }
        else
        {
            var targetLayer = targetDocument.layers.add();
            targetLayer.name = sourceLayers[i].name;
            targetLayer.layerColor = sourceLayers[i].layerColor;
            sourceLayers[i].pageItems.everyItem().duplicate(targetLayer);
            }

        }
    }
于 2019-07-12T18:21:39.420 回答