【问题标题】:InDesign Script: export to IDMLInDesign 脚本:导出到 IDML
【发布时间】:2014-01-08 16:14:17
【问题描述】:

我正在尝试编写一个脚本来执行数据合并,然后是查找/替换、页面添加,最后是导出。我可以让它执行所需的合并和查找/替换。当我手动添加新页面时,我通常选择页面窗口中的第一页,然后单击底部的添加新页面。这样做会使之后的每一页变为 2 页。我不知道如何在脚本中执行此操作,我在下面尝试的方法无效。它会在文档末尾添加一个新页面。

app.activeDocument.pages.item(0).select();
app.activeDocument.pages.add(); 

合并后,编辑变得非常慢,我添加或删除每个字母需要 15-20 个。我发现像您期望的那样对其进行编辑的唯一方法是将其导出为 IDML 格式,然后重新打开它InDesign 中的文件。我找不到太多关于通过 javascript 中的脚本导出的信息。我接下来打算尝试的是:app.activeDocument.exportFile(ExportFormat.INDESIGN_MARKUP, newDoc, false); 但我不知道这是否可行。我对 InDesign 中的脚本非常陌生。我正在使用 InDesign CS5.5,这是迄今为止的整个脚本:

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,但我不知道如何开始在这里搜索我需要的东西?

【问题讨论】:

  • 关于“模板”MMM14 Template_v1.indd 的小提示:如果您将其保存为“模板”(在“另存为”对话框中的“格式”下),或者只是将其扩展名更改为 indt,您将find ID 默认打开一个副本

标签: javascript scripting export adobe-indesign


【解决方案1】:

添加页面和流动文本

代替

app.activeDocument.pages.add(); 

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

app.activeDocument.pages.add(LocationOptions.AT_END); 

您不需要第二个参数“reference”,因为AT_END 已经准确地告诉 ID 新页面必须到达的位置(即,如果您想始终在第一个页面之后添加一个页面,您可以使用 @ at 为 987654332@,referenceapp.activeDocument.pages[0])。

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

newframe = newpage.textFrames.add(..)
startframe.nextTextFrame = newframe; // <- thread

不幸的是,只有向页面添加一个新的文本框架不会自动将其调整到其父页面的边距。你得到的只是左上角的一个很小的新框架。所以你必须读出页面大小(使用它的bounds 属性)和当前的margins,然后你可以调整框架的大小。如果您的边距被镜像,则另一个障碍等待着您:出于某种原因,MarginPreferences 对象不会“看到”您是在左侧页面还是右侧页面上。所以我使用page.index 来检查它是否奇数,如果是,则反转leftright 边距。

另一个可能的陷阱是当您的文本包含连续溢出项目时——无论您做什么,文本都无法适应页面。这可能发生在大表格、大图像或具有某些奇怪属性的文本(例如巨大的左缩进或不中断)应用于超过单行的文本时。如果未选中,此脚本将继续检查溢出,检测它,创建一个新页面和文本框,然后 溢出等等。快速检查是添加一个新的文本框架并检查它是否真的包含任何东西——连续溢出框架总是完全是空的。

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

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 forum 上提问。只有也许:也许你可以手动尝试一下。完成合并后,不要使用“保存”而是“另存为”,然后覆盖旧文件。 “另存为”清理并重新分配内存;我发现它有时会有所帮助。如果这不起作用,我不能推荐其他任何东西,事实上,导出到 IDML 并重新打开它。

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

首先,你不应该使用app.open——这就是它所说的,它打开以前保存的文件。你想在这里创建一个 new 文件; exportFile 中的参数to 指的是它将创建 的文件。使用您的代码,它将覆盖现有文件——除非 ID 注意到您已经打开它并因此抱怨。这不是我愿意测试其正确性的陈述。

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

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

var newDoc = new File(Folder.desktop+"/DataMerge_MMM.idml");
app.activeDocument.exportFile(ExportFormat.INDESIGN_MARKUP, newDoc);

免责声明

我手头没有 CS5,我用我的 CS6 进行了测试。如果您在“未知属性”及其类似方面遇到错误,请在早上回电,我会看看我能做些什么。

【讨论】:

  • 根据你所说的,我使用app.activeDocument.pages.add(LocationOptions.AFTER,app.activeDocument.pages[0]); 添加了附加页面,它给出了预期的结果。有关导出的信息和提示也很有效!我了解您的代码在做什么,但我不明白您为什么建议这样做?合并数据时,它会为每条记录创建一个新页面。有一些溢出的文本,但我运行 TextStitch 使文档像书一样流动,它可以解决这个问题。还没有弄清楚如何从脚本中运行它,尽管它并不那么重要。
  • @Jonathan:在数据合并期间新页面出现问题的想法一定在我脑海中浮现。但是,很高兴你能根据我的提示解决它!要在你的脚本完成后链接另一个脚本,你可以尝试app.doScript
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
相关资源
最近更新 更多