首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InDesign脚本:导出到

InDesign脚本:导出到
EN

Stack Overflow用户
提问于 2014-01-08 16:14:17
回答 1查看 4.1K关注 0票数 0

我正在尝试编写一个脚本来执行数据合并,然后是查找/替换、页面添加,最后是导出。我可以让它执行合并并查找/替换所需的内容。手动添加新页面时,通常在“页面”窗口中选择第一页,然后单击底部的“添加新页”。这样做后的每一页都会转到2页。我不知道如何在脚本中这样做,我在下面所做的尝试是行不通的。它在文档末尾添加一个新页面。

代码语言:javascript
复制
app.activeDocument.pages.item(0).select();
app.activeDocument.pages.add(); 

合并之后,编辑变得非常慢,我添加的每个字母或delete.The只需要15-20个字母就可以像您期望的那样进行编辑,方法是将其导出为IDML格式,然后在InDesign中重新打开该文件。在通过javascript中的脚本进行导出时,我还没有找到多少信息。接下来我打算尝试的是:app.activeDocument.exportFile(ExportFormat.INDESIGN_MARKUP, newDoc, false);,但我不知道这样做是否有效。我对InDesign中的脚本非常陌生。我使用的是InDesign CS5.5,下面是到目前为止的整个脚本:

代码语言:javascript
复制
main();

function main()
{
    //Possibly let the user go find and choose a file
    //var mergeTemplate = File.openDialog();
    //var myDocument = app.open(mergeTemplate);

    //Open the template file to be used by the data merge.
    var myDocument = app.open(File("Macintosh HD/Users/Christian/Desktop/InDesign_Data_Merge/MMM14 Template_v1.indd"));
    //Load the data source
    var myDataSource = File("Macintosh HD/Users/Christian/Desktop/InDesign_Data_Merge/MMM v1.mer");

    myDocument.dataMergeProperties.selectDataSource(myDataSource);
    myDocument.dataMergeProperties.mergeRecords();

    //Save the document under a new name for later use.
    app.activeDocument.save(File("Macintosh HD/Users/Christian/Desktop/InDesign_Data_Merge/DataMerge_MMM.indd"));

    //Close the document, NOT saving original template,  so the original file is not destroyed or overwritten.
    myDocument.close(SaveOptions.no);

    //Find line break placeholder and replace with line break
    findReplace ("$$", "^n");
    //Find the tab placeholder and replace with tab
    findReplace ("##", "^t");

    //Select the first page of the document
    //app.activeDocument.pages.item(0).select();
    //Add another page to make the document print on both sides, like an open book
    //app.activeDocument.pages.add(); 

//export to IDML
//exportIDML();
}


function findReplace(findVal,replaceVal)
{
     // Clear the find/change text preferences.
    app.findTextPreferences = NothingEnum.NOTHING;
    app.changeTextPreferences = NothingEnum.NOTHING;

     // Set the find options
    app.findChangeTextOptions.caseSensitive = false;
    app.findChangeTextOptions.includeFootnotes = false;
    app.findChangeTextOptions.includeHiddenLayers = false;
    app.findChangeTextOptions.includeLockedLayersForFind = false;
    app.findChangeTextOptions.includeLockedStoriesForFind = false;
    app.findChangeTextOptions.includeMasterPages = false;
    app.findChangeTextOptions.wholeWord = false;

    // Search the document for the string findVal
    app.findTextPreferences.findWhat = findVal;
    // Change it to the string replaceVal
    app.changeTextPreferences.changeTo = replaceVal;
    // Perform the search-and-replace operation
    app.activeDocument.changeText();

}

function exportIDML()
{
    var newDoc = app.open(File("Macintosh HD/Users/Christian/Desktop/InDesign_Data_Merge/DataMerge_MMM.indd"));
    app.activeDocument.exportFile(ExportFormat.INDESIGN_MARKUP, newDoc, false);
}

编辑:,另一篇文章把我带到了jongware.mit.edu这个网站,但是我不知道如何开始搜索我需要的东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-10 00:14:13

添加页面和流动文本

而不是

代码语言:javascript
复制
app.activeDocument.pages.add(); 

您可以在Pages.add函数中使用一个方便的参数:

代码语言:javascript
复制
app.activeDocument.pages.add(LocationOptions.AT_END); 

您不需要第二个参数"reference",因为AT_END已经精确地告诉ID新页面必须出现在哪里(也就是说,如果您希望总是在第一个页面之后添加一个页面,那么您将使用AFTER进行at和app.activeDocument.pages[0]作为引用)。

然而,只有添加了一个页面的。要使文本流到上面,您需要手动添加一个新的文本框架和“线程”(链接),这是上一页中的一个:

代码语言:javascript
复制
newframe = newpage.textFrames.add(..)
startframe.nextTextFrame = newframe; // <- thread

不幸的是,只有向页面添加新的文本帧才会自动将其大小调整到父页的页边距。你得到的只是左上角的一个小小的新框架。因此,您必须读取页面大小(使用其bounds属性)和当前margins,然后可以调整框架的大小。如果您的页边距是镜像的话,另一个问题正在等待您:由于某些原因,MarginPreferences对象不会“查看”您是在左边还是右边的页面。因此,我使用page.index检查它是否奇怪,如果是的话,反转leftright边距。

另一个可能的陷阱是当您的文本包含持续的Overset项时--无论您做什么,文本都将不适合页面。这可能发生在一个大的表,一个大的图像,或一些奇怪的属性的文本,如一个巨大的左缩进或不中断应用于更多的文本,而不是单行。如果不选中,这个脚本将继续检查溢出,检测它,创建一个新的页面和文本框架,然后它也会溢出等等。快速检查是添加一个新的文本帧,并检查它是否包含任何内容--一个不断溢出的框架将始终是完全空的。

有了这一点,下面是您可以使用的部分,而不是您自己的注释代码。它假设要添加的文本位于当前文档的最后一页(索引-1)上的覆盖文本框架中。它还假定此文本框架是该页(!)上唯一的文本框架。

代码语言:javascript
复制
startframe = app.activeDocument.pages[-1].textFrames[0];
while (startframe.overflows)
{
    newpg = app.activeDocument.pages.add(LocationOptions.AT_END);
    newframe = newpg.textFrames.add();
    if (newpg.index & 1)
        newframe.geometricBounds = [newpg.bounds[0]+newpg.marginPreferences.top,
            newpg.bounds[1]+newpg.marginPreferences.left,
            newpg.bounds[2]-newpg.marginPreferences.bottom,
            newpg.bounds[3]-newpg.marginPreferences.right];
    else
        newframe.geometricBounds = [newpg.bounds[0]+newpg.marginPreferences.top,
            newpg.bounds[1]+newpg.marginPreferences.right,
            newpg.bounds[2]-newpg.marginPreferences.bottom,
            newpg.bounds[3]-newpg.marginPreferences.left];
    startframe.nextTextFrame = newframe;
    startframe = newframe;
//  Check for Continuous Overflow!
    if (startframe.contents.length == 0)
    {
        alert ("Continuous Overflow detected, cannot continue");
        break;
    }
}

出口

这是一个恼人的问题,在数据合并后ID会减慢。我想不起来以前是否报告过这一点;您可能需要在Adobe InDesign论坛上询问。只有可能:也许您可以手动尝试这个。完成合并后,不要直接使用“保存”,而是使用“另存为”,并覆盖旧文件。“另存为”清理并重新分配内存;我发现它有时会有所帮助。如果这样做不起作用,我就不能推荐其他的东西了,实际上,就是导出到IDML并重新打开它。

您向IDML导出的尝试是公平的,但是它包含了一些错误!

首先,您不应该使用app.open --这是按它说的做的,它打开一个先前保存的文件。您希望在这里创建一个新文件;exportFile中的参数引用它将要创建的文件。使用您的代码,它将覆盖一个现有的文件--除非ID通知,否则您已经打开了它,因此会对此抱怨。我不愿意检验是否正确。

其次,您需要正确地将其文件扩展名设置为.idml :)

第三个次要点:Folder对象为桌面文件夹提供了一个方便的快捷方式。

代码语言:javascript
复制
var newDoc = new File(Folder.desktop+"/DataMerge_MMM.idml");
app.activeDocument.exportFile(ExportFormat.INDESIGN_MARKUP, newDoc);

免责声明

我手头没有CS5,我用CS6进行了测试。如果您在“未知属性”及其类似项上有错误,请在早上再打电话给我,让我看看我能做些什么。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21000895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档