如何更改此 InDesign TOC 超链接脚本,使整个段落(而不仅仅是页码)充当超链接?

How to change this InDesign TOC hyperlinking script so that the entire paragraph, not just the page number, functions as a hyperlink?

我正在尝试改编一个脚本(如下)来创建一个 Table 列表,其中每个条目都在 pdf 中充当一个 hyperlink,点击后会带你到相应的 table 页面。下面的脚本目前通过识别字符样式来工作 - "TOC number" - 它仅应用于每个条目 [=paragraph] 中包含实际数字的那部分;脚本根据字符样式读取数字,并将数字转换为指向 InDesign 文件中该编号页面的 hyperlink。

(请注意,在 InDesign 中,实际页面是一个空白占位符。table(其中有数百个)是在 Excel 中创建的,并使用 "Replace pages..." 作为我们生产过程的最后一步。所有这些都是说:如果涉及使用 InDesign 的自动 TOC 功能,请保存您的建议;我们已经广泛使用这些功能,但是对于涉及 [=27 的 pdf 的过程步骤=] tables,这不是一个选择——我们想改编下面的脚本,它已经可以工作并且(几乎)满足了需要。)

为了说明,这里是 Table 列表中每个 entry/paragraph 的部分(用红色圈出),脚本当前将其制作成 hyperlink:

我们要做的是:我们希望将页码(再次用红色圈出)之前的整个 entry/paragraph 变成指向相应页码的可点击超级 link。

我想最简单的方法是更改​​脚本,使其定位页码,然后使用 GREP 查找前一段分隔符,并将该段落分隔符之前的所有内容都变成 link?或者,我为 Table 条目列表设计了段落样式,这样每个部分(table 编号、标题、省略号和页码)都有不同的字符样式,分隔符通过选项卡。因此,这可能是在脚本中定义每个页码之前的哪些文本应成为 link.

的一部分的另一种方式。

任何人都可以通过编辑此脚本来演示解决方案吗?它已经(几乎)起作用了!

/* Copyright 2016, Kasyan Servetsky
October 3, 2016
Written by Kasyan Servetsky
http://www.kasyan.ho.com.ua
e-mail: askoldich@yahoo.com */
//======================================================================================
var scriptName = "Make hyperlinks",
set, doc, swatchOK,
count = 0;

PreCheck();

//===================================== FUNCTIONS  ======================================
function Main() {
    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = "\d+";
    app.findGrepPreferences.appliedCharacterStyle = "TOC number";
    var foundItems = doc.findGrep(true);
    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

    if (foundItems.length == 0) ErrorExit("No page numbers were found in the current document.", true);

    for (var f = 0; f < foundItems.length; f++) {
        try {
            var sourceTextRef = foundItems[f].insertionPoints[0].paragraphs[0].texts[0];  

            if (sourceTextRef.fillColor != swatchOK) {
                MakeHyperlink(sourceTextRef);
            }
        }
        catch(err) {
            $.writeln(err.message + ", line: " + err.line);
        }
    }

    var report = count + " hyperlink" + ((count == 1) ? " was" : "s were") + " created.";
    alert("Finished. " + report, scriptName);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function MakeHyperlink(sourceTextRef) {
    var source, destination, hyperlink,
    pageNum = sourceTextRef.contents,
    obj = GetPage(pageNum);

    if (obj != null) {
        source = doc.hyperlinkTextSources.add(sourceTextRef);
        destination = obj.docDest.hyperlinkPageDestinations.add(obj.page);

        var name = "Page_" + pageNum;

        if (!doc.hyperlinks.itemByName(name).isValid) {
            hyperlink = doc.hyperlinks.add(source, destination, {name: name});
        }
        else {
            hyperlink = doc.hyperlinks.add(source, destination, {name: name + "_" + String(Math.random()).replace(/^0\./, "")});
        }

        if (hyperlink.isValid) {
            count++;
            sourceTextRef.fillColor = swatchOK;
        }
    }
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetPage(pageNum) {
    var obj = null;

    for (var i = 0; i < app.documents.length; i++) {
        if (app.documents[i].pages.itemByName(pageNum).isValid) {
            obj = {page: app.documents[i].pages.itemByName(pageNum), docDest: app.documents[i]};
            break;
        }
    }

    return obj; 
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function CheckSwatches() {
    if (doc.swatches.itemByName("===== OK =====") == null) {
        swatchOK = doc.colors.add({name : "===== OK =====", model : ColorModel.PROCESS, space : ColorSpace.RGB, colorValue : [0, 0, 0]});
    }
    else {
        swatchOK = doc.swatches.itemByName("===== OK =====");
    }
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function PreCheck() {
    if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
    doc = app.activeDocument;
    if (!app.activeDocument.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
    CheckSwatches();
    Main();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
    alert(error, scriptName, icon);
    exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------

下面是我自己想出来的解决办法。我的问题是我试图对一个变量 (sourceTextRef) 做太多事情。一旦我定义了仅包含页码的第二个变量 (tabPnum) — 与 sourceTextRef 不同,我将其重新定义为包含找到每个页码的整个段落 — 然后我传递了那个新的MakeHyperlink 函数的参数,脚本有效:

//======================================================================================
var scriptName = "TOC links",
set, doc, swatchOK,
count = 0;

PreCheck();

//===================================== FUNCTIONS  ======================================
function Main() {
    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = "\d+";      
    app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.item("publications").paragraphStyleGroups.item("TOC & Acknowledgements").paragraphStyles.item("TOC Table/Figure entry");
    var foundItems = doc.findGrep(true);
    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;

    if (foundItems.length == 0) ErrorExit("No page numbers were found in the current document.", true);

    for (var f = 0; f < foundItems.length; f++) {
        try {
            var tabPnum = foundItems[f];      // foundItems[f] returns the page number . 

            var sourceTextRef = foundItems[f].words[0].lines[0].paragraphs[0];   // returns the full paragraph.

$.writeln(sourceTextRef.contents);

            if (sourceTextRef.fillColor != swatchOK) {
                MakeHyperlink(sourceTextRef, tabPnum);  // added tabPnum here (necessary for passing parameter to function below).
            }
        }
        catch(err) {
            $.writeln(err.message + ", line: " + err.line);
        }
    }

    var report = count + " hyperlink" + ((count == 1) ? " was" : "s were") + " created.";
    alert("Finished. " + report, scriptName);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function MakeHyperlink(sourceTextRef, tabPnum) { //added parameter tabPnum as argument here)
    var source, destination, hyperlink, 
    pageNum = tabPnum.contents,   //  changed this line so that pageNum is defined by tabPnum. 

    obj = GetPage(pageNum);

    if (obj != null) {
        source = doc.hyperlinkTextSources.add(sourceTextRef);
        destination = obj.docDest.hyperlinkPageDestinations.add(obj.page);

        var name = "Page_" + pageNum;

        if (!doc.hyperlinks.itemByName(name).isValid) {
            hyperlink = doc.hyperlinks.add(source, destination, {name: name});
        }
        else {
            hyperlink = doc.hyperlinks.add(source, destination, {name: name + "_" + String(Math.random()).replace(/^0\./, "")});
        }

        if (hyperlink.isValid) {
            count++;
            sourceTextRef.fillColor = swatchOK;
        }
    }
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetPage(pageNum) {
    var obj = null;

    for (var i = 0; i < app.documents.length; i++) {
        if (app.documents[i].pages.itemByName(pageNum).isValid) {
            obj = {page: app.documents[i].pages.itemByName(pageNum), docDest: app.documents[i]};
            break;
        }
    }

    return obj; 
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function CheckSwatches() {
    if (doc.swatches.itemByName("===== OK =====") == null) {
        swatchOK = doc.colors.add({name : "===== OK =====", model : ColorModel.PROCESS, space : ColorSpace.CMYK, colorValue : [0, 0, 0, 100]});

    }
    else {
        swatchOK = doc.swatches.itemByName("===== OK =====");
    }
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function PreCheck() {
    if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
    doc = app.activeDocument;
    if (!app.activeDocument.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
    CheckSwatches();
    Main();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
    alert(error, scriptName, icon);
    exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------